Basic concepts of C++ client and server

1. C/S mode (client/server): the client is responsible for interacting with users, collecting user information, and sending requests to the server through the network

 

2. B/S mode (browser/server): the client uses the same browser, and requests to the web server through the web browser, the web server operates the database and sends the results back to the client

 

3. The framework diagram of the relationship between the client and the server

 

 

4. Server :

①Create socket int socket(int domain, int type, int protocol);

domain : what address type to use

AF_INET (IPv4 network socket type)

AF_INET6 (IPv6)

AF_UNIX (Unix system local communication)

type : Set the communication protocol type

SOCK_STREAM: TCP protocol

( Mainly used for data download, file transfer, applications with high reliability requirements )

SOCK_DGRAM: UDB protocol

(For connectionless, low reliability, fast transmission speed, used in video conferencing, chat and other applications with low reliability and fast transmission speed)

SOCK_RAW:IP

(The network layer protocol provides a standard for network address identification)

protocol : used to specify the number of the transmission protocol used by the socket

Generally set to 0

②绑定 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Initial setting of IP and port

    struct  sockaddr_in ser_add;

        ser_add.sin_family = AF_INET ;

      ser_add.sin_port = htons(8099); //Port number

     ser_add.sin_addr.s_addr = htonl(INADDR_ANY);

 

③Monitor int listen(int sockfd, int backlog);

backlog: length setting of the listening queue

SOMAXCONN 128

Extended knowledge of SOMAXCONN parameters

 

④Wait for int accept(int fd, sockaddr * restrict_addr, socklen_t * restrict_addr_len) client:

When the server and the client are not successfully connected, they cannot use the socket descriptor to communicate, that is, the socket is in a blocking state at this time, and data can be transmitted only when the connection is successful.

accept

 

5. Client:

①Create socket: int socket(int domain, int type, int protocol);

 

②连接:  int connect(int fd, const sockaddr * addr, socklen_t len);

 

 

6. Solve the problem that the client cannot connect to the server:

  1. Check whether the IP address and port are wrong
  2. Check if it can be pinged
  3. Check whether the port can be connected statement: ncc IP address port number  
  4. Whether the port allows access

7. Different types of transmission

When defining the structure, assign a type and judge the type of the received data.

Guess you like

Origin blog.csdn.net/m0_49036370/article/details/113879981