Comunicação entre servidores-clientes através de programação de soquetes usando TCP/IP

Communication between server-clients through socket programming using TCP/IP

In this tutorial, I will explain how server-client communication uses TCP/IP. I already explained about socket and How to create socket in Linux . Before the explanation, let's see some basic overview of the terminology.
Three main things are needed to establish the connection between server-client models: 1) Transport protocol (TCP or UDP) 2) Socket 3) IP address and port Transport Protocol (TCP): TCP is a connection-oriented protocol which stands for transfer control protocol. It is a reliable and secure protocol. In TCP, the receiver can generate confirmation of the received packet, so the sender (client) needs to wait for a confirmation and if the response does not come or some packet error is generated, it will be resent to the client. Socket: Socket is an endpoint connection for communication between two machines. It is like a data connectivity path between two wireless terminals. The socket is required on both the server and client sides. You can refer to the socket tutorial and How to create socket in Linux . IP address and port The IP address is a numeric address unique to each computer and device. It plays an important role in the Internet Protocol network domain. In the server-client model, the server needs to know which device is connected to it and who the server establishes the connection with. IP address plays an important role in communication between server-client models. In network terms, the port is the place where information is sent. Various protocols assign different port numbers. A specific port number is required for server-client communication according to the protocol used. Let's check how to get the user's IP address. Enter the following command in the command terminal: ifconfig It displays network information like address, data packet, etc. Make sure which type of internet connection is on your system. The IP address of the system is located by inet address . It is the IP address of your system. For example, inet address: xxx.xx.xx.xx Here, x represents the numeric value. Server – Client Communication using TCP/IP The server-client model is the communication model to share the resource and provide the service to different machines. Server is the main system that provides resources and different types of services when the client requests to use it. Let's see, server-client communication through socket programming using TCP/IP. I thought you knew basic C programming and socket. Please refer to the socket tutorial and How to create socket in Linux before learning this tutorial . Server-client communication using TCP/IP The following tasks are performed on the client side: • Create a socket for communication • Configure TCP protocol with server IP address and port number • Connect to server via socket • Wait for confirmation from the server • Send message to server • Receive message from server The following tasks are performed on the server side: • Create a socket for communication • Bind local port and connection address • Configure TCP protocol with port number • Listen to the client connection • Accept client connection • Send confirmation • Receive customer message • Send message to customer Here I wrote two individual programs, one for the server side and one for the client side. We need a computer with a Linux environment. But do not worry; you can communicate with just a single system. Client-side program: //*********************************TCP_client.c************ ********************// #include #include #include #include #include #include #include #include #include main int { char sendMessage(512),receiveMessage(512); inner sock, result; hostent structure *host; structure sockaddr_in serverAdd; host = gethostbyname(“xxx.xx.xx.xx”); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror(“Socket creation failed”); output(1); } serverAdd.sin_family = AF_INET; serverAdd.sin_port=htons(5000); serverAdd.sin_addr = *((struct in_addr *)host->h_addr); bzero(&(serverAdd.sin_zero),8); if (connect(sock, (struct sockaddr *)&serverAdd, sizeof(struct sockaddr)) == -1) { perror(“Connection failed”); output(1); } while(1) { result = recv(sock,receberMessage,1024,0); receiveMessage(result) = ”; printf(“nMessage received: %s”, receiveMessage); printf(“nSEND message: “); fgets(sendMessage,512,stdin); send(sock,sendMessage,strlen(sendMessage), 0); } return 0; } //*********************************TCP_client.c************ ********************// Server-Side Program: //*********************************TCP_server.c************ *******************// #include #include #include #include #include #include #include #include #include main int { char sendMessage(1024), receiveMessage(1024); int sock, connected, result; structure sockaddr_in serverAdd, clientAdd; internal length; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror(“Socket creation failed”); output(1); } if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&1,size(int)) == -1) { perror(“Set socket option”); output(1); } serverAdd.sin_family = AF_INET; serverAdd.sin_port=htons(5000); serverAdd.sin_addr.s_addr=INADDR_ANY; bzero(&(serverAdd.sin_zero),8); if (bind(sock, (struct sockaddr *)&serverAdd, sizeof(struct sockaddr))== -1) { perror(“Unable to link”); output(1); } if (listen(half, 3) == -1) { perror(“Listen”); output(1); } printf(“TCPServer waiting for client connection”); flush(stdout); while(1) { length = sizeof(struct sockaddr_in); connected = accept(sock,(struct sockaddr *)&clientAdd,&length); printf(“The server is connected with IP address %s and port %dn”,inet_ntoa(clientAdd.sin_addr),ntohs(clientAdd.sin_port)); while (1) { printf(" SEND message: "); fgets(sendMessage,100,stdin); send(connected, sendMessage, strlen(sendMessage), 0); result = recv(connected,receiveMessage,512,0); receiveMessage(result) = ”; printf(“Message received: %sn”, receiveMessage); flush(stdout); } } return 0; } //*********************************TCP_server.c************ *******************// Save the file in the directory and compile it. After successful compilation, run the executable file in the command terminal. You can refer to the How to Make First C Program in Linux tutorial if you are not aware of the compilation and execution process. Note: Write down your server's IP address in place of “xxx.xxx.xx.xx”. in the client-side program . Run the server – client program on the same system You can run the client-server program on the same machine. First find the IP address of your client PC as I described above. Now, if you want to run on the same PC, the same IP is inserted into the client's source code. Save the server and client source code on the same PC. Open the command terminal and run the first server executable file. The server-side code is in run mode. Don't interrupt the code. Open another command terminal and run the client-side executable file. Note: If you do not run server-side code and try to run the client-side code first, a connection refused error will occur. See the following image for understanding: Here, the black parts of the image indicate the connected client's IP address and port number. Now enter the message from the server terminal. Now you can send data from the client to the server. The server-client model is communicated using TCP/IP. Enter the terminal client data and verify. Here, I didn't finish the connection. So both can communicate until the connection is closed. Press ctrl + C which terminal you want to exit.

Back to the blog

Leave a comment

Comments need to be approved before publication.