Socket
Before we talk about socket, we should probably know how the internet's workflow.
When we send message, we actually send a data package to some servers. And those servers will send the those data package to to specific ports according to the data package description. And the software in that port will work on our data package and send back the information we need. Some ports we always use:
- 80: http default port
- 443: https default port
- 25565: Minecraft default port
- 23: Telnet (part of TCP/IP)
- 21 : FTP
- ...
int socket(int domain, int type, int protocol):
return a file descriptor or -1
on error
domain
: set the rule ofprotocol
type
: the type of socketsprotocol
: use0
to tell system use default
The domain:
AF_INET:
Address family internet (same asPF_INET
)PF_INET
: Protocol family internet (same asAF_INET
)- ...
There are many types sockets:
SOCK_DGRAM
: Datagram socketsSOCK_STREAM
: Stream sockets(TCP), connection-oriented sockets, no loss guaranteed and ordered deliverySOCK_RAW
: Raw sockets- ...
And we have a structure to store socket information:
struct sockaddr_in{
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
}
The above socket address is one of the socket address we can use in sockaddr
by (struct sockaddr) &addr_in
sin_family
: same as the socket familysin_port
: the port will be listen to, we will usehtons(int portsNum)
to refer this value wherehtons
stands hosts-to-network-shortsin_addr
: server will usually set as any. for examplesin_addr.s_addr = INADDR_ANY
, client will set it the server addresssin_zero
: will usually set as 0 bymemset
For .sin_addr
we can use inet_pton
function to convert IPV4 and IPV6 addresses from text to binary form.\
int inet_pton(int af, const char *src, void *dst):
return 1 on successful convert or -1 on error or 0 if text does not contain a valid network address.
bind system call
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen):
return 0 if bind success or -1
on error
listen system call
int listen(int sockfd, int backlog)
: return 0 success or -1
on error
backlog
the max number of partial connections the server can hold
accept system call
int accept(int sockfd, struct sockaddr *addr, socklen_t *addlen)
: return a new socket file descriptor use to communicate with the client, or -1
on error
addr
: we should create a newstruct scokaddr_in
for client address to store its dataaddlen
: thesizeof(addr)
accept will blocking system 'till connections
connect system call
int connect(int sockfd, const struct sockaddr *address, socklen_t addrlen):
return a new socket file descriptor use to communicate with the server, or -1
on error
address
: usegetaddrinfo
to writeresult
and castresult->ai_addr
intosockaddr
addrlen
: thesizeof(address
)`
int getaddrinfo(char *host, char *service, stuct addrinfo *hints, struct addrinfo **result):
host
: the domain nameservice
: for page we don't carehints
: for page we don't careresult
: use a newstruct addrinfo
to store the server info