Socket (socket), similar to file descriptor, three kinds
1, streaming Sockets (SOCK_STREAM): can provide a reliable, connection-oriented traffic flow, it uses the TCP protocol. TCP guarantees the correctness and sequencing of data transmission.
2. Datagram Sockets (SOCK_DGRAM): Defines a non-connected service that transmits data through separate messages, is unordered, and is not guaranteed to be reliable and error-free, using Datagram Protocol (UDP).
3, the original socket (SOCK_RAW): Directly based on the IP protocol.
Network address
The struct sockaddr is used to record network addresses:
struct SOCKADDR
{
U_short sa_family;//protocol family, using "af_xxx" form, such as: Af_inet (IP protocol family)
Char SA_DATA[14];//14 byte-specific protocol address
}
But the most common is the SOCKADDR_IN record network address
struct SOCKADDR_IN
{
Short int sin_family;//Protocol family
unsigned short int sin_port;/* port number */
struct IN_ADDR sin_addr;//protocol specific address
unsigned char sin_zero[8];/* fill 0*/
}
struct in_addr{
union{
struct{
unsigned char s_b1,s_b2,s_b3,s_b4;
}s_un_b;
struct{
unsigned short s_w1,s_w2;
}s_un_w;
unsigned long s_addr;
}s_un_w;
}IN_ADDR;
Bigendian on the network (low byte first transmission)
Socket programming functions
Socket: Creating sockets
Bind: Used to bind IP address and port number to socket
Connect: This function is used to establish a connection to the server
Listen: Set maximum connection requirements that the server can handle
Accept: waits for a socket connection request from a user Client
Send: Sending data
Recv: Receiving data