There are too many explanations of specific functions. Analyze them by yourself according to the program.
You can refer to this article:
https://blog.csdn.net/qq_41687938/article/details/119102328?spm=1001.2014.3001.5501
https://blog.csdn.net/weixin_42193813/article/details/105666316
catalogue
1.2 introduction to socket interface
1.3 process of using socket in Linux system
2, TCP communication based on socket
2.1 TCP communication flow based on socket technology
2.2 TCP communication flow diagram based on socket technology
2.3 TCP socket programming example
3, UDP communication based on socket
3.1 # UDP communication flow diagram based on socket technology
3.2 UDP communication flow based on socket technology
3.2.1 the server process is mainly divided into the following six parts.
3.2.2 client process of UDP protocol
3.3 UDPSocket client server communication example
1, socket
1.1 socket overview
socket is an abstract layer between application layer and transport layer. It abstracts the complex operation of TCP/IP layer into several simple interfaces. The supply layer calls the implemented processes to communicate in the network.
Socket originated from UNIX. Under the idea that everything in UNIX is a file philosophy, socket is an implementation of "open read / write close" mode. The server and client maintain a "file" respectively. After establishing a connection and opening, they can write content to their own file for the other party to read or read the other party's content, and close the file at the end of communication.
1.2 introduction to socket interface
socket(): create socket
bind(): bind the socket to the local address and port, which is usually called by the server
listen(): dedicated to TCP, enabling the listening mode
accept(): TCP only. The server is waiting for the client to connect. It is generally blocked
connect(): dedicated to TCP. The client actively connects to the server
send(): TCP dedicated, sending data
recv(): TCP dedicated, receiving data
sendto(): UDP private, sending data to the specified IP address and port
recvfrom(): UDP dedicated, which receives data and returns the IP address and port of the remote end of the data
close(): close the socket
1.3 process of using socket in Linux system
Method of program running under Linux: (assuming that the server program (service.c) and client program (client. C) have been written)
(two terminals need to be turned on)
1. Compile the two files respectively (execute the following two commands respectively)
gcc service.c -o service gcc client.c -o client
2. Run the server first
./service
3. Run the client in another terminal
./client
4. Execution completed!
2, TCP communication based on socket
2.1 TCP communication flow based on socket technology
Server (for passive connection, you need to create your own address information)
Create a socket -- socket()
Bind IP address, port and other information to socket -- bind()
Listening socket -- listen()
Wait for the connection request from the client -- accept()
Send and receive data - send() and recv(), or read() and write()
Close network connection - close()
client
Create a socket -- socket()
Connect server - connect()
Receive and send data - send() and recv(), or read() and write()
Close network connection - close()
2.2 TCP communication flow diagram based on socket technology
2.3 TCP socket programming example
2.3.1 server (service.c)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/in.h> int main(){ //Create socket int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //Bind socket to IP and port struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); //Each byte is filled with 0 serv_addr.sin_family = AF_INET; //Use IPv4 address serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Specific IP address serv_addr.sin_port = htons(1258); //port bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); //Enter the listening state and wait for the user to initiate a request listen(serv_sock, 20); //Receive client requests struct sockaddr_in clnt_addr; socklen_t clnt_addr_size = sizeof(clnt_addr); int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size); //Send data to client char str[] = "Hello World!"; write(clnt_sock, str, sizeof(str)); //Close socket close(clnt_sock); close(serv_sock); return 0; }
2.3.2 client (client.c)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> int main(){ //Create socket int sock = socket(AF_INET, SOCK_STREAM, 0); //Make a request to the server (specific IP and port) struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); //Each byte is filled with 0 serv_addr.sin_family = AF_INET; //Use IPv4 address serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Specific IP address serv_addr.sin_port = htons(1258); //port connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); //Read the data returned by the server char buffer[40]; read(sock, buffer, sizeof(buffer)-1); printf("Message form server: %s\n", buffer); //Close socket close(sock); return 0; }
3, UDP communication based on socket
3.1 # UDP communication flow diagram based on socket technology
In the programming framework of UDP protocol, the difference between the client and the server is that the server must use the bind() function to bind the listening local UDP port, while the client can send it directly to a port address of the server address without binding. The block diagram is shown in the figure.
3.2 UDP communication flow based on socket technology
3.2.1 the server process is mainly divided into the following six parts.
(1) Create a socket file descriptor and use the function socket() to generate a socket file descriptor.
(2) Set the server IP address and port, and initialize the network address structure to be bound.
(3) Bind the IP address, port and other information, and use the bind() function to bind the socket file descriptor to an address.
(4) Receive the client's data circularly, and use the recvfrom() function to receive the client's network data.
(5) Send data to the client and use the sendto() function to send data to the server host.
(6) Close the socket and use the close() function to release resources. Client process of UDP protocol
3.2.2 client process of UDP protocol
The client process of UDP protocol is divided into five parts.
(1) Create socket file descriptor, socket();
(2) Set the server IP address and port, struct sockaddr;
(3) Send data to the server, sendto();
(4) Receive data from the server, recvfrom();
(5) Close the socket, close().
3.3 UDPSocket client server communication example
3.3.1 server (service.c)
#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<errno.h> #include<string.h> #include<stdlib.h> #define SERV_PORT 3000 int main() { int sock_fd; //Socket sub description symbol int recv_num; int send_num; int client_len; char recv_buf[20]; struct sockaddr_in addr_serv; struct sockaddr_in addr_client;//Server and client addresses sock_fd = socket(AF_INET,SOCK_DGRAM,0); if(sock_fd < 0){ perror("socket"); exit(1); } else{ printf("sock sucessful\n"); } //Initialize server disconnect address memset(&addr_serv,0,sizeof(struct sockaddr_in)); addr_serv.sin_family = AF_INET;//protocol family addr_serv.sin_port = htons(SERV_PORT); addr_serv.sin_addr.s_addr = htonl(INADDR_ANY);//Any local address client_len = sizeof(struct sockaddr_in); /*Binding socket*/ if(bind(sock_fd,(struct sockaddr *)&addr_serv,sizeof(struct sockaddr_in))<0 ){ perror("bind"); exit(1); } else{ printf("bind sucess\n"); } while(1){ printf("begin recv:\n"); recv_num = recvfrom(sock_fd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&addr_client,&client_len); if(recv_num < 0){ printf("bad\n"); perror("again recvfrom"); exit(1); } else{ recv_buf[recv_num]='\0'; printf("recv sucess:%s\n",recv_buf); } printf("begin send:\n"); send_num = sendto(sock_fd,recv_buf,recv_num,0,(struct sockaddr *)&addr_client,client_len); if(send_num < 0){ perror("sendto"); exit(1); } else{ printf("send sucessful\n"); } } close(sock_fd); return 0; }
3.3.2 client (client.c)
#include<stdio.h> #include<string.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define DEST_PORT 3000 #define DSET_IP_ADDRESS "192.168.1.103" int main() { int sock_fd;/*Socket file descriptor*/ int send_num; int recv_num; int dest_len; char send_buf[20]={"hello tiger"}; char recv_buf[20]; struct sockaddr_in addr_serv;/*Server address*/ sock_fd = socket(AF_INET,SOCK_DGRAM,0);//Create socket //Initialize server address memset(&addr_serv,0,sizeof(addr_serv)); addr_serv.sin_family = AF_INET; addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS); addr_serv.sin_port = htons(DEST_PORT); dest_len = sizeof(struct sockaddr_in); printf("begin send:\n"); send_num = sendto(sock_fd,send_buf,sizeof(send_buf),0,(struct sockaddr *)&addr_serv,dest_len); if(send_num < 0){ perror("sendto"); exit(1); } else{ printf("send sucessful:%s\n",send_buf); } recv_num = recvfrom(sock_fd,recv_buf,sizeof(recv_buf),0,(struct sockaddr *)&addr_serv,&dest_len); if(recv_num <0 ){ perror("recv_from"); exit(1); } else{ printf("recv sucessful\n"); } recv_buf[recv_num]='\0'; printf("the receive:%s\n",recv_buf); close(sock_fd); return 0; }
3.4 UDP programming notes:
1. UDP messages may be lost or duplicated
2. UDP messages may be out of order
3. UDP lacks flow control
4. UDP protocol data message truncation
5. recvfrom returns 0, which does not mean that the connection is closed, because udp is connectionless.
6. ICMP asynchronous error
7,UDP connect
8. Determination of UDP outgoing interface
9. Possible problems with UDP packets that are too large
Reference link:
https://blog.csdn.net/lell3538/article/details/53335472
https://blog.csdn.net/dongyanxia1000/article/details/80743691
https://blog.csdn.net/weixin_42193813/article/details/105771978