function Flow diagram:
Create: int socket (int domain,int type,int protocol);
Domain--> protocol domain:
Af_inet ———— IPV4.
Af_inet6 ———— IPV6.
Af_local ———— UNIX domain.
Type--> Type:
sock_stream--streaming socket, TCP protocol to support the socket (provide connection-oriented, reliable data transmission services, data by byte stream, in order to send and receive, to ensure that in the transmission process without loss, no redundancy).
Spck_dgram ——— datagram sockets, the UDP protocol supports this socket (provides connectionless service, data dispatch and delivery is unordered, cannot guarantee the accurate arrival of data).
Sock_ram ———— raw socket, often used to detect new protocols. Allows direct access to protocols or physical networks that are below the transport layer.
Protocol--> Protocol:
0 ———— automatically according to type matching protocol.
Ipproto_tcp.
Ipproto_udp.
return value:
>0 is the socket descriptor;-1 is a failure. OFF: int close (int fd); int shutdown (int sockfd,int howto);
Sockfd--socket sockets.
Howto
Shut_rd ——— value is 0; Closes the read of the connection.
SHUT_WR ——— value is 1; Closes the write of the connection.
The shut_rdwr--value is 2; the read and write of the connection is closed. Binding: int bind (int socket,const struct sockaddr* address,socklen_t address_len);
Sockets ————— socket Descriptor.
Address ————— addresses and port numbers.
Address_len ——— The length of the address buffer.
The return value —————— 0 is a success; Socket_error is a failure. Listening: int listen (int sockfd,int backlog);
The socket descriptor ————— the sockets listener.
Backlog ———— The maximum number of connections queued.
The return value ————— 0 is success;-1 is a failure. Connection: int connect (int sockfd,const struct sockaddr* addr,socklen_t,addrlen);
SOCKFD ———— The socket descriptor for the client.
Addr ————— The socket address of the server.
Addrlen ———— The length of the socket address of the server.
The return value ————— 0 for success L-1 failed. Accepted: int accept (int sockfd,struct socketaddr* addr,socklen_t* addrlen);
SOCKFD ———— The socket descriptor for the server, or listen for the socket descriptor.
Addr ————— The socket address of the client.
Addrlen ———— The length of the socket address for the client.
The return value ———— non-1 is the connection descriptor;-1 is the failure. Send: ssize_t write (int fd,const void* buf,size_t len);
FD ———— file descriptor.
BUF the data written ————.
Len ———— writes the data length.
The return value ——— >0 is the actual number of bytes written; <0 error. ssize_t Send (int sockfd,const void* buf,size_t len,int flags);
flags--is usually 0.
return value and other--ditto. ssize_t sendto (int sockdf,const void* buf,size_t len,int flags,const struct sockaddr* dest_addr,socklen_t);
DEST_ADDR ——— the destination socket address.
Addrlen ———— The target socket address length.
return value and other--ditto. Receive: ssize_t read (int fd,void* buf,size_t len);
BUF ———— Read the file.
The return value--0 to the end of the file; >0 the number of bytes actually read; <0 error. ssize_t recv (int sockfd,void* buf,size_t len,int flags);
Flags —————— are usually 0.
return value and other--ditto. ssize_t recvfrom (int sockdf,void* buf,size_t len,int flags,const-struct sockaddr*) src_addr,socklen_t ——— the destination socket address. Addrlen ————— The target socket address length. return value and other--ditto. Structure body: struct SOCKADDR:
struct SOCKADDR {
unsigned short sa_family;
Char sa_data[14];
struct SOCKADDR_IN:
struct sockaddr_in{short
sin_family;
unsigned short sin_port;
struct IN_ADDR sin_addr;
unsigned char sin_zero[8];
struct IN_ADDR:
struct IN_ADDR {
in_addr_t s_addr;
};
in_addr_t:
typedef unsigned int in_addr_t;
struct SOCKADDR_IN6 (IPV6):
struct sockaddr_in6{
//ipv6 is a fixed 24-byte length
uint8_t sin6_len;
Address cluster type, for Af_inet6
sa_family_t sin6_family;
16-bit port number, network byte sequence
in_port_t sin6_port;
32-bit Flow label
uint32_t sin6_flowinfo;
128-bit IP address
struct in6_addr sin6_addr;
}
Special address settings: The Address (0.0.0.0): Host all IP, multiple network card and use. IPv4:
IN_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
IPv6:
IN6_ADDR.SIN6_ADDR = In6addr_any;
Loopback address (127.0.0.1): Local virtual interface, no network card available, also do not need a network card. Used to check the local network protocol. IPv4:
IN_ADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_loopback);
IPv6:
IN6_ADDR.SIN6_ADDR = In6addr_loopback;
Broadcast address: Restricted broadcast address--255.255.255.255 subnet broadcast address--xxx.xxx.xxx.255 full subnet broadcast ——— xxx.xxx.255.255 IPv4:
IN_ADDR.SIN_ADDR.S_ADDR = htonl (inaddr_broadcast);
IPv6: None.
Code: Tcp_cliect:
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <sys/socket.h> void Show
_info (int connfd) {struct sockaddr_in local_addr;
Bzero (&local_addr,sizeof (LOCAL_ADDR));
socklen_t Local_addr_len = sizeof (LOCAL_ADDR);
GetSockName (CONNFD, (struct sockaddr*) &local_addr,&local_addr_len);
printf ("Client local%s:%d\n", Inet_ntoa (LOCAL_ADDR.SIN_ADDR), Ntohs (Local_addr.sin_port));
struct sockaddr_in peer_addr;
Bzero (&peer_addr,sizeof (PEER_ADDR));
socklen_t Peer_addr_len = sizeof (PEER_ADDR);
Getpeername (CONNFD, (struct sockaddr*) &peer_addr,&peer_addr_len);
printf ("Clinet peer%s:%d\n", Inet_ntoa (PEER_ADDR.SIN_ADDR), Ntohs (Peer_addr.sin_port)); int main (int argc,char* argv[]) {if (4!= argc) {printf ("usage:%s <ip> < #port > <message>\n", Argv[0])
;
return 1;
int CONNFD = socket (af_inet,sock_stream,0);
if ( -1 = connfd) {perror ("Socket err");
return 1;
} struct sockaddr_in remote_addr; Bzero (; remote_addr,sizeof (REMOTE_ADDR));
remote_addr.sin_family = af_inet;
REMOTE_ADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Remote_addr.sin_port = htons (atoi (argv[2));
if ( -1 = = Connect (connfd, (struct sockaddr*) &remote_addr,sizeof (REMOTE_ADDR))) {perror ("Connect err");
return 1;
} show_info (CONNFD);
Write (Connfd,argv[3],strlen (argv[3]) +1);
printf ("Client send:%s\n", argv[3]);
Char Buf[bufsiz];
Bzero (Buf,bufsiz);
if ( -1 = = Read (Connfd,buf,bufsiz)) {perror ("read err");
return 1;
printf ("Client recv:%s\n", buf);
Close (CONNFD); } Tcp_server:
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <sys/socket.h> void Show
_info (int connfd) {struct sockaddr_in local_addr;
Bzero (&local_addr,sizeof (LOCAL_ADDR));
socklen_t Local_addr_len = sizeof (LOCAL_ADDR);
GetSockName (CONNFD, (struct sockaddr*) &local_addr,&local_addr_len);
printf ("Server local%s:%d\n", Inet_ntoa (LOCAL_ADDR.SIN_ADDR), Ntohs (Local_addr.sin_port));
struct sockaddr_in peer_addr;
Bzero (&peer_addr,sizeof (PEER_ADDR));
socklen_t Peer_addr_len = sizeof (PEER_ADDR);
Getpeername (CONNFD, (struct sockaddr*) &peer_addr,&peer_addr_len);
printf ("Server Peer%s:%d\n", Inet_ntoa (PEER_ADDR.SIN_ADDR), Ntohs (Peer_addr.sin_port));
int main (int argc,char* argv[]) {if (3!= argc) {printf ("usage:%s <ip> < #port >\n", argv[0]);
return 1;
int LISTENFD = socket (af_inet,sock_stream,0);
if ( -1 = listenfd) {perror ("LISTENFD open err");
return 1;
printf ("Socket create ok\n");
int flag = 1;SetSockOpt (listenfd,sol_socket,so_reuseaddr,&flag,sizeof (flag));
struct sockaddr_in local_addr;
Bzero (&local_addr,sizeof (LOCAL_ADDR));
local_addr.sin_family = af_inet;
LOCAL_ADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Local_addr.sin_port = htons (atoi (argv[2));
if ( -1 = = Bind (LISTENFD, (struct sockaddr*) &local_addr,sizeof (local_addr)) {perror ("bind err");
return 1;
printf ("Bind ok\n");
if ( -1 = = Listen (listenfd,10)) {perror ("Listen err");
return 1;
printf ("Listen ok\n");
struct sockaddr_in remote_addr;
Bzero (&remote_addr,sizeof (REMOTE_ADDR));
socklen_t Remote_addr_len = sizeof (REMOTE_ADDR);
int CONNFD = Accept (LISTENFD, (struct sockaddr*) &remote_addr,&remote_addr_len);
if ( -1 = connfd) {perror ("Accept err");
return 1;
} show_info (CONNFD);
printf ("Accept%s:%d\n", Inet_ntoa (REMOTE_ADDR.SIN_ADDR), Ntohs (Remote_addr.sin_port));
Char Buf[bufsiz];
Bzero (Buf,bufsiz);
if ( -1 = = Read (connfd,buf,bufsiz-1)) {perror ("read err"); ReTurn 1;
printf ("Server recv:%s\n", buf);
if ( -1 = = Write (Connfd,buf,strlen (BUF) +1)) {perror ("Write err");
return 1;
printf ("Server send:%s\n", buf);
Close (CONNFD);
Close (LISTENFD); }Results: