Linux socket programming: simple client and server, linuxsocket
What is socket?
Socket originated from Unix, and one of the basic philosophies of Unix/Linux is "Everything is a file". You can use the "open-> read/write-> close" mode to operate. In fact, socket is an implementation of this mode. socket is a special file, and some socket functions are operations (read/write IO, open, and close) on it)
Socket is used for network communication. Network Communication generally refers to process communication between different hosts. For example, qq on my computer can communicate with qq on your computer, both send data between processes.
Identify a process with a pid locally. in the network, the network layer of the TCP/IP protocol"IP address"Can uniquely identify the host in the network, transport layer"Protocol + PortCan uniquely identify the application (process) in the host ). (IP address, protocol, Port) can identify the network process, and process Communication in the network can use this sign to interact with other processes.
Socket Server programming steps:
1. Create a socket and use the socket function
2. bind a socket: bind
3. Listening Suite word: listen
4. accept: receives the connection from the client. The three handshakes occur in this phase. This function returns a new socket.
5. process the business
Socket Client:
1. Create a socket: socket
2. connect to the server: connect
3. Process Business
Certificate ----------------------------------------------------------------------------------------------------------------------------------
Server Source Code:
1 # include <stdio. h> 2 # include <sys/types. h> 3 # include <sys/socket. h> 4 # include <stdlib. h> 5 # include <string. h> 6 # include <arpa/inet. h> 7 # include <unistd. h> 8 9 int main (int argc, char * argv []) 10 {11 int sockfd =-1; 12 int bindres =-1; 13 int listenres =-1; 14 15 sockfd = socket (AF_INET, SOCK_STREAM, 0); 16 if (-1 = sockfd) {17 perror ("sock created"); 18 exit (-1 ); 19} 20 21 struct sockaddr_in server; 22 memset (& server, 0, sizeof (struct sockaddr_in); 23 server. sin_family = AF_INET; 24 server. sin_port = 6666; 25 server. sin_addr.s_addr = htonl (INADDR_ANY); 26 27 bindres = bind (sockfd, (struct sockaddr *) & server, sizeof (server); 28 if (-1 = bindres) {29 perror ("sock bind"); 30 exit (-1); 31} 32 33 listenres = listen (sockfd, SOMAXCONN); 34 if (-1 = listenres) {35 perror ("sock listen"); 36 exit (-1); 37} 38 39 struct sockaddr_in peerServer; 40 int acceptfd =-1; 41 socklen_t len = sizeof (peerServer); 42 acceptfd = accept (sockfd, (struct sockaddr *) & peerServer, & len); 43 if (-1 = acceptfd) {44 perror ("sock accept"); 45 exit (-1); 46} 47 48 char recvBuf [1024]; 49 while (1) {50 memset (recvBuf, 0, sizeof (Signature); 51 int recvBytes = read (acceptfd, recvBuf, sizeof (recvBuf); 52 fputs (recvBuf, stdout); 53 write (acceptfd, signature, recvBytes ); 54} 55 56 close (sockfd); 57 close (acceptfd); 58 59 return 0; 60}View Code
Client source code:
1 # include <stdio. h> 2 # include <sys/types. h> 3 # include <sys/socket. h> 4 # include <stdlib. h> 5 # include <string. h> 6 # include <arpa/inet. h> 7 # include <unistd. h> 8 9 int main (int argc, char * argv []) 10 {11 int sockfd =-1; 12 13 sockfd = socket (AF_INET, SOCK_STREAM, 0 ); 14 if (-1 = sockfd) {15 perror ("sock created"); 16 exit (-1); 17} 18 19 struct sockaddr_in server; 20 memset (& server, 0, size Of (struct sockaddr_in); 21 server. sin_family = AF_INET; 22 server. sin_port = 6666; 23 server. sin_addr.s_addr = inet_addr ("127.0.0.1"); 24 25 int res =-1; 26 res = connect (sockfd, (struct sockaddr *) & server, sizeof (server )); 27 if (-1 = res) {28 perror ("sock connect"); 29 exit (-1); 30} 31 32 char sendBuf [1024] = {0 }; 33 char recvBuf [1024] = {0}; 34 while (fgets (sendBuf, sizeof (sendBuf), Stdin )! = NULL) {35 write (sockfd, sendBuf, sizeof (sendBuf); 36 read (sockfd, recvBuf, sizeof (recvBuf); 37 fputs (recvBuf, stdout ); 38 memset (sendBuf, 0, sizeof (sendBuf); 39 memset (recvBuf, 0, sizeof (recvBuf); 40} 41 42 close (sockfd); 43 44 return 0; 45}View Code remarks:
1. socket function parameters: When protocol is 0, the default protocol corresponding to the type is automatically selected.
2. What is the meaning of setting sin_addr to INADDR_ANY?
0.0.0.0 indicates the local IP address, that is, all the IP addresses of the local device. Some machines have more than one Nic, which means all Nic IP addresses when multiple NICs exist. For example, if a computer has three NICs connected to three networks, the computer has three IP addresses. If an application needs to listen to a port, which NIC address port does he want to listen? If you bind a specific IP address, you can only listen to the port of the NIC where the IP address you set is located. The other two NICs cannot listen to the port. If I need to listen to all three NICs, so we need to bind three ip addresses, which means we need to manage three Sockets for data exchange. Isn't that complicated? So when INADDR_ANY occurs, you only need to bind INADDR_ANY to manage a socket. No matter which network adapter the data is from, data from the bound port can be received.