preface : Each interface on the Internet must have a unique internet address (ip address ), adopt IP Address to identify the network interface on the host , And then access the host .IP The address is divided into ipv4 and ipv6.
Many applications are available TCP perhaps UDP To transmit data ,16 Bit port number to represent different applications .

One ,TCP
TCP: Transmission control protocol , Provide reliable communication connection for applications , It is suitable for transmitting a large number of data at one time
UDP: User packet protocol , Provide connectionless communication , And there is no reliable guarantee for the transport package , It is suitable for transmitting a small amount of data at a time .

TCP: full duplex

Two , socket

Socket is a data structure in the kernel of operating system , It is a gateway for nodes in the network to communicate with each other , It's a network process ID. network address (IP) Only this computer can be identified , You don't know which process to communicate with , The port number is required , A port number can only be assigned to one process .
therefore , use ip The address and port number can uniquely identify a network process in the whole network .
Network programming is through socket To carry out .socket It's a special one IO Interface , It can not only realize the communication between the processes of the local machine , Communication between processes on different machines can also be realized .
A complete socket { agreement , Local address , Local port number , Remote address , Remote port number }

socket Information data structure
be careful :
1, Port number and ip Addresses are stored in network byte order , Instead of host byte order , Therefore, the port number and ip The address is converted into byte order
2, Port number :htons function
3,IP address :inet_aton function
#include<sys/socket.h> #include<netnnet/in.h> #include<arpa/inet.h>
TCP Network programming

server The server

1, establish socket signal communication
#include<sys/socket.h> #include<sys/types.h> int sockfd=socket(AF_INET,
SOCK_STREAM,0)//ipv4 agreement ,TCP agreement
2, Binding address bind
#include<sys/types.h> #include<sys/socket.h> struct sockaddr_in saddr; saddr.
sin_family=AF_INET;// use ipv4 agreement saddr.sin_port=htons(8888);// Indicates the port number saddr.sin_addr.
s_addr=inet_addr("");// express ip address bind(sockfd,(struct sockaddr*)&saddr,
sizeof(struct sockaddr_in));
3,listen monitor , Waiting for the client to connect
#include<sys/socket.h> listen(sockfd,3);//3 Indicates that it can be connected 3 Clients
4,accept Accept request

accept Function is used to accept connection requests from a remote computer , Establish a communication connection with the client . When accept Function receives a connection , A new one will be returned socket descriptor , The new descriptor is used for subsequent data transmission and reading . The original descriptor can continue to listen for connections from other clients .
#include<sys/types.h> #include<sys/socket.h> struct sockaddr_in caddr;// client
memset(&caddr,0,sizeof(struct sockaddr); int len=sizeof(struct sockaddr); int
sfd=accept(sockfd,(struct sockaddr*)&caddr,&len);
client client


2,connect Connecting to the server
#include<sys/types.h> #include<sys/socket.h> struct sockaddr_in saddr;// Request to connect to server
memset(&saddr,0,sizeof(struct sockaddr)); saddr.sin_family=AF_INET; saddr.
sin_port=htons(8888);// Service port number saddr.sin_addr.s_addr=inet_addr("");
// The server ip address connect(sockfd,(struct sockaddr*)&saddr,sizeof(struct sockaddr));
The server


Concurrent server
Multi process , Multithreading

On the server accept after , Then create the thread pthread

Client exits first , The server receives empty messages all the time

Create two threads , Receive and send

The client sends information to the server , The server then transmits the information to the client

The client must first create a thread to receive the information from the server , And then send messages

Server : Two threads , One reading , One write

©2019-2020 Toolsou All rights reserved,
Final review of database : Summary of comprehensive application questions Laplance operator ( Second derivative ) Simple learning of computer composition principle pyqt Button call python program _PyQt: Link button to function in program How much can you go up once you change jobs ? Today, I saw the ceiling of job hopping python in str Function usage _python in str Usage Summary of built-in functions MySQL trigger web The server nginx---linux Installation and deployment C++ Chapter V polymorphism exercises :( It's coming to an end )python Check built-in functions , How to check python Built in function