In many projects, the sending and receiving of information are needed, so some information is collected on the Internet, and simple text, files, pictures and BASE64 encoding are realized. (TCP/IP, IP protocol is not detailed here, focusing on the implementation of functions)
Socket Foundation
Socket is a network API that can be used to develop network programs. Socket interfaces provide a method of communication between processes, enabling processes on the same or different hosts to transmit bidirectional information in the same specification. The process can communicate with each other by calling socket interface, and socket interface realizes the actual communication work by utilizing the network communication protocol function of lower layer and system call.
In order to communicate between processes, the network programming interface should be invoked first. The request information received and sent by the process by the socket negative is delivered up or down through the lower network communication protocol service interface (TCP/IP), so the socket interface is the interface from the application layer to the transport layer.
_IPv4 socket address structure
IPv4 socket address structure is also known as "network socket address structure". Its name is sockaddr_in. Its structure is defined as follows:
- sin_family: Internet address family, in IPv4 is AF_INET
- sin_port: is the port number, stored in network byte order
- sin_addr: A structure in which members store IP addresses
- sin_zero: Not yet used, but always set it to zero
TCP socket programming
_Connection-oriented communication based on TCP/IP protocol can be realized by using TCP socket programming, which is divided into server and client. The main implementation process is as follows:
Client
Loading Initial Sockets
WSADATA wsaData;//Initialization socket char buff[2048]; char buffs2[100]; memset(buff, 0, sizeof(buff)); memset(buffs2, 0, sizeof(buffs2)); if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)//The first parameter represents the version of winsock, which in this case is Winsock version 2.2. { printf("Initialization Winsock fail"); return 0; } SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET;//Representing TCP/IP Protocol Family // htons function: converting a 16-bit unsigned short integer data from host arrangement to network arrangement, the function of htonl is just the opposite. addrSrv.sin_port = htons(8888);//Port number addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//IP address, sin_addr stores IP address, using in_addr data structure //Create sockets /* af:A family of addresses, usually AF_INET type:Socket type, SOCK_STREAM represents the creation of a stream-oriented socket. For SOCK_DGRAM, it means creating a connectionless data packet socket. Create the original socket for SOCK_RAW protocol:The protocol used for sockets does not specify that it can be set to 0 The return value is a socket SOCKET socket(int af,int type,int protocol); */ SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);//Protocol, socket type, if (SOCKET_ERROR == sockClient){ printf("Socket() error:%d", WSAGetLastError()); return 0; }
Send a connection request to the server
/* connect Function: The function that the client socket sends the connection request. The first parameter is the client socket. The second parameter is a structure pointer, which includes the address and ip of the connection host. The third parameter is the length of the buffer. */ connect(sockClient, (struct sockaddr*)&addrSrv, sizeof(addrSrv));
_3. Send data to server
/* The first parameter is socket. The second parameter is the receiving data buffer. The third parameter is the length of the buffer. The fourth parameter is the way the function is called. */ send(sockClient, buffs, sizeof(buffs), 0);
_4. Receiving data
/* recv Function: Receive data. The first parameter is socket. The second parameter is the receiving data buffer. The third parameter is the length of the buffer. The fourth parameter is the way the function is called. */ recv(sockClient, buff, sizeof(buff), 0); printf("%s\n", buff);
5. Close sockets
closesocket(sockClient);
Server side
Create sockets for monitoring
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0); SOCKADDR_IN addrSrv; addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(port); //Port number above 1024 /** * INADDR_ANY A specified address is an address of 0.0.0. This address actually represents an uncertain address, or "all addresses" or "any address". Generally speaking, it is defined as zero in each system. */ addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
2. Binding sockets to specified ports and addresses
/* The first parameter is socket, and the second parameter is a structure pointer, which contains port and IP address information. The third parameter represents the buffer length. It should be noted that the second parameter is represented in the API as const struct sockaddr FAR* */ bind(sockSrv, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN));
3. Set socket to listen mode (server-side socket-specific)
/* Server socket must be set to monitor mode in order to connect with server resume. There are two parameters, the first parameter is socket, and the second parameter is the length of the queue waiting to connect to the maximum. */ listen(sockSrv, 10);
4.accept() function, waiting for user request to arrive
/* accept Function: The server socket receives the connection request from the client and the connection is successful. Returns a socket that can send and receive data at the server. The first parameter is socket. The second parameter is the sockaddr_in structure pointer containing the IP information of the client port. The third parameter is the length of the receiving parameter addr. */ accept(sockSrv, (SOCKADDR *)&addrClient, &len);
5. Exchange recv (), send () with client information
recv(sockConn, recvBuf, sizeof(recvBuf), 0); send(sockConn, sendbuf, sizeof(sendbuf), 0);
6. Close sockets
closesocket(sockSrv);