Linux Network Programming-the Client of TCP Programming

Keywords: socket network Programming Ubuntu

Overview of TCP

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol.

TCP has the following characteristics:
1) Abstraction of Service Model of Telephone System
2) Every complete data transmission must go through the process of establishing connection, using connection and terminating connection.
3) reliableTo ensure the reliability of data transmission, the error retransmitting and the corresponding confirmation should be given for each data received.

C/S Architecture of TCP Programming

The development of network programming based on TCP is divided into two parts: server side and client side. The common core steps and processes are as follows:

3. TCP Client Programming

For the TCP client programming process, it is a bit similar to the call process:
1. Find a cell phone to talk to (socket()
2. Dial the other party's number and make sure that the other party is the person you are looking for.
3. Active chat (send() or write()
4. Or, receive a call from the other party (recv() or read())
5. After the communication, the two sides said goodbye and hung up (close()

Required header file: #include <sys/socket.h>
int socket(int family,int type,int protocol);
Functions:
Create a socket for network communication socket (Descriptor) Detailed usage, please see "socket" word Introduction.
Parameters:
family: This example writes AF_INET, representing IPv4
type: This example writes SOCK_STREAM, representing TCP data streams
Protocol: Write 0 here, set to 0 to indicate the default protocol
Return value:
Success: socket
Failure < 0

int connect( int sockfd, const struct sockaddr *addr, socklen_t len );

Functions:

Actively establishing a connection with the server is somewhat similar. We call people and actively dial their phone number. What is the process, please?<The relationship among connect(), listen() and accept()>.

Parameters:

sockfd: Socket returned by socket()

addr: Connected server address structure

len: Address structure length

Return value:

Success: 0

Failure: -1


The connect() function is equivalent to dialing a number, only dialing the number and making sure that the other party is the person you are looking for.( The three handshake ) In order to carry out the next communication.


ssize_t send(int sockfd, const void* buf, size_t nbytes, int flags);

Functions:

To send data, when the last parameter is 0, write() can be used instead (send is equivalent to write). Note: TCP protocol cannot be used to send 0-length packets. If the data is not sent successfully, the kernel will automatically resend it.

Parameters:

sockfd: Connected socket

buf: Address to send data

nbytes: Size of slow data sent (in bytes)

flags: Socket flag (often 0)

Return value:

Success: Number of bytes sent successfully

Failure < 0


Here we communicate with the ubuntu client program in the virtual machine through the network debugging assistant of Windows. Network Debugging Assistant Download Please Click Access Password d5f3.


Windows network debugging assistant serves as TCP server and receives requests from clients. The configuration of debugging assistant is as follows:



3.1 - Example 1: tcp client sends data: Running in code ubuntu


  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #include <arpa/inet.h>  
  6. #include <sys/socket.h>  
  7. #include <netinet/in.h>  
  8. int main(int argc, charchar *argv[])  
  9. {  
  10.     unsigned short port = 8080;             //Port number of server  
  11.     charchar *server_ip = "10.221.20.24";       //Server ip address  
  12.       
  13.     if( argc > 1 )       //Function parameters, you can change the server's ip address  
  14.     {         
  15.         server_ip = argv[1];  
  16.     }     
  17.     if( argc > 2 )      //Function parameters, you can change the port number of the server \\\\\\\\  
  18.     {  
  19.         port = atoi(argv[2]);  
  20.     }  
  21.   
  22.     int sockfd;  
  23.     sockfd = socket(AF_INET, SOCK_STREAM, 0);//Create communication endpoints: sockets  
  24.     if(sockfd < 0)  
  25.     {  
  26.         perror("socket");  
  27.         exit(-1);  
  28.     }  
  29.       
  30.     //Setting up the server address structure  
  31.     struct sockaddr_in server_addr;  
  32.     bzero(&server_addr,sizeof(server_addr)); //Initialize the server address  
  33.     server_addr.sin_family = AF_INET;   // IPv4  
  34.     server_addr.sin_port = htons(port); //Port  
  35.     inet_pton(AF_INET, server_ip, &server_addr.sin_addr.s_addr);    // ip  
  36.       
  37.      //Active Connection Server  
  38.     int err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));       
  39.     if(err_log != 0)  
  40.     {  
  41.         perror("connect");  
  42.         close(sockfd);  
  43.         exit(-1);  
  44.     }  
  45.       
  46.     char send_buf[512] = {0};  
  47.     printf("send data to %s:%d\n",server_ip,port);  
  48.     while(1)  
  49.     {  
  50.         printf("send:");  
  51.         fgets(send_buf,sizeof(send_buf),stdin); //Input content  
  52.         send_buf[strlen(send_buf)-1]='\0';  
  53.         send(sockfd, send_buf, strlen(send_buf), 0);   //Send information to the server  
  54.     }  
  55.   
  56.     close(sockfd);  
  57.       
  58.     return 0;  
  59. }  
Operation results:


3.2 - Example 2: The tcp client receives data and the code runs under ubuntu

recv() is used to receive data from tcp.

ssize_t recv(int sockfd, void *buf,  size_t nbytes, int flags);

Functions:

Receive network data. By default, if no data is received, this function will block until data arrives.

Parameters:

sockfd: socket

buf: the address of the buffer that receives network data

nbytes: The size of the receiving buffer (in bytes)

flags: socket flag (often 0)

Return value:

Success: Number of bytes successfully received

Failure < 0


test The code is as follows:

  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #include <arpa/inet.h>  
  6. #include <sys/socket.h>  
  7. #include <netinet/in.h>  
  8. int main(int argc, charchar *argv[])  
  9. {  
  10.     unsigned short port = 8080;             //Port number of server  
  11.     charchar *server_ip = "10.221.20.24";       //Server ip address  
  12.       
  13.     if( argc > 1 )       //Function parameters, you can change the server's ip address  
  14.     {         
  15.         server_ip = argv[1];  
  16.     }     
  17.     if( argc > 2 )      //Function parameters, you can change the port number of the server \\\\\\\\  
  18.     {  
  19.         port = atoi(argv[2]);  
  20.     }  
  21.   
  22.     int sockfd;  
  23.     sockfd = socket(AF_INET, SOCK_STREAM, 0);//Create communication endpoints: sockets  
  24.     if(sockfd < 0)  
  25.     {  
  26.         perror("socket");  
  27.         exit(-1);  
  28.     }  
  29.       
  30.     //Setting up the server address structure  
  31.     struct sockaddr_in server_addr;  
  32.     bzero(&server_addr,sizeof(server_addr)); //Initialize the server address  
  33.     server_addr.sin_family = AF_INET;   // IPv4  
  34.     server_addr.sin_port = htons(port); //Port  
  35.     //inet_pton(AF_INET, server_ip, &server_addr.sin_addr); // ip  
  36.     server_addr.sin_addr.s_addr = inet_addr(server_ip);//Equivalent to inet_pton  
  37.       
  38.      //Active Connection Server  
  39.     int err_log = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));       
  40.     if(err_log != 0)  
  41.     {  
  42.         perror("connect");  
  43.         close(sockfd);  
  44.         exit(-1);  
  45.     }  
  46.       
  47.       
  48.     printf("send data to %s:%d\n",server_ip,port);  
  49.       
  50.     char send_buf[512] = "this is send data";  
  51.     printf("send data \"%s \" to %s:%d\n",send_buf,server_ip,port);  
  52.     send(sockfd, send_buf, strlen(send_buf), 0);   //Send information to the server  
  53.       
  54.     char recv_buf[512] = {0};  
  55.     recv(sockfd, recv_buf, sizeof(send_buf), 0); //Receiving data  
  56.     printf("%s\n", recv_buf);  
  57.   
  58.     close(sockfd);  
  59.       
  60.     return 0;  
  61. }  

Operation results:

Posted by netcoord99 on Mon, 22 Apr 2019 00:57:36 -0700