socket demonstration program under Linux

And C language tutorial , we start with a simple "Hello World!" Program to cut  socket  programming.

This section demonstrates the  Linux  under the code, server.cpp is server-side code, client.cpp is client-side code to achieve the function are: client reads a string from the server and print them out.

Server-side code server.cpp:

 
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8.  
  9. int main () {
  10. // Create a socket
  11. you serv_sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)
  12.  
  13. // The sockets and IP, port binding
  14. struct sockaddr_in serv_addr;
  15. memset (& serv_addr, 0, sizeof (serv_addr)); // each byte are filled with 0
  16. serv_addr.sin_family = AF_INET; // use IPv4 addresses
  17. serv_addr.sin_addr.s_addr = inet_addr ( "127.0.0.1"); // IP address of the specific
  18. serv_addr.sin_port = htons (1234); // port
  19. bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
  20.  
  21. // into the monitor, waiting for the user initiates a request
  22. listen(serv_sock, 20);
  23.  
  24. Receiving a client request @
  25. struct sockaddr_in clnt_addr;
  26. socklen_t clnt_addr_size = sizeof(clnt_addr);
  27. int clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
  28.  
  29. // send data to the client
  30. char str[] = "http://c.biancheng.net/socket/";
  31. write(clnt_sock, str, sizeof(str));
  32.  
  33. // close the socket
  34. close(clnt_sock);
  35. close(serv_sock);
  36.  
  37. return 0;
  38. }


Client code client.cpp:

 
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/socket.h>
  7.  
  8. int main () {
  9. // Create a socket
  10. int sock = socket (AF_INET, SOCK_STREAM, 0);
  11.  
  12. // initiate a request to the server (a specific IP and port)
  13. struct sockaddr_in serv_addr;
  14. memset (& serv_addr, 0, sizeof (serv_addr)); // each byte are filled with 0
  15. serv_addr.sin_family = AF_INET; // use IPv4 addresses
  16. serv_addr.sin_addr.s_addr = inet_addr ( "127.0.0.1"); // IP address of the specific
  17. serv_addr.sin_port = htons (1234); // port
  18. connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
  19.  
  20. // read the server returns data
  21. char buffer[40];
  22. read(sock, buffer, sizeof(buffer)-1);
  23.  
  24. printf("Message form server: %s\n", buffer);
  25.  
  26. // close the socket
  27. close(sock);
  28.  
  29. return 0;
  30. }


Start a terminal ( Shell ), compile and run server.cpp:

[ADMIN @ localhost ~] $ server.cpp G ++ -o Server
[ADMIN @ localhost ~] $ ./server
# wait for the arrival of the request

Under normal circumstances, the program runs to accept () function will be blocked, waiting for the client initiated the request.

Took restart a terminal, compile and run client.cpp:

[admin@localhost ~]$ g++ client.cpp -o client
[admin@localhost ~]$ ./client
Message form server: http://c.biancheng.net/socket/

client receives the string is sent from the server to run over the end, at the same time, server sends the string to complete tasks also run over. It can be observed by two open terminals.

After the client runs through connect () function to initiate a server request, the server in the listening state is activated, executed accept () function accepts a client request and then performs write () function returns the data to the client. After the client receives the returned data, Connect () to the end of the run, then read () to read out the data.

server only accepts a client request, when the server returns data to the client, the program will run over. If you want to receive again to the data server, you must run the server again, so this is a very simple socket program, not been able to accept the request of the client.

Source resolve

1) Let me talk about the code of server.cpp.

Line 11 creates a socket function, parameter indication AF_INET IPv4 address, indication SOCK_STREAM socket connection-oriented, IPPROTO_TCP go indication TCP protocol socket (). In Linux, socket is a file, there are file descriptors, you can use the write () / read () function I / O operations, it has been in the " What socket is made to explain" in.

19 through line bind () function serv_sock socket with a particular IP address and bound port, IP address, and port are kept in sockaddr_in structure.

socket () function to determine the various properties of the socket, the bind () function so that the socket with a particular IP address and port association, so clients can connect to the socket.

Line 22 allows the socket in a passive listening. The so-called passive listening, refers to a socket has been in a "sleep" until the client sends a request would be "wake up."

Line 27 accept () function for receiving a request from a client. Once the program is executed to accept () it will be blocked (pauses) until the client initiated the request.

Write line 31 () function is used to write data to a file in the socket, which is to send data to the client.

And a regular file, socket also () closed with a close after use.

2) tell you client.cpp code. client.cpp code and server.cpp there are some differences.

The first line of code 19 () initiates a request to the server via connect, the server's IP address and port number stored in sockaddr_in structure. Until the server returns data, we connect () before the end of the run.

The first 23 lines of code () reads the data file from the socket by read.

Guess you like

Origin blog.csdn.net/Qsir/article/details/93738454