Today, when writing a simple TCP communication example, encountered a problem: the server and client can connect successfully, and the client can also be sent normally, but the server is not received, after searching the internet, finally solved the problem. Here are the following:
It is important to note that a server side can connect to multiple client side, the server side of the Accept () function is responsible for waiting for and receiving client connection request, and the accept () function will be different client side of the SOCKFD as the return value. To ensure that the client side data is received, the first parameter of the recv () function should be the return value after the accept succeeds when the client connection succeeds and the recv () function is used to receive the data.
The instance code is as follows:
1.server Terminal
1#include <sys/types.h>2#include <sys/socket.h>3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6#include <errno.h>7#include <unistd.h>8#include <netinet/inch.h>9#include <netdb.h>Ten One #defineMax_msg_len 1024 A #defineBACKLOG 10 - - intMainintargcChar*arg[]) the { - structsockaddr_in servaddr,clidaddr; - structHostent *host =NULL; - intPort =0, socketfd,sin_size; + socklen_t Peerlen; - intReclen =0; + CharBuf[max_msg_len] = {0}; A at if(argc<2) - { -printf"Please input port number!\r\n"); - return-1; - } - in //ipV4 TCP 0 - if(Socketfd=socket (Af_inet,sock_stream,0))==-1) to { +Perror ("Socket"); - return-1; the } *printf"socket FD =%d\n", SOCKETFD); $ Panax Notoginseng -memset (&SERVADDR,0,sizeof(structsockaddr_in)); theservaddr.sin_family =af_inet; +Servaddr.sin_port = htons (Atoi (arg[1])); AServaddr.sin_addr.s_addr=Inaddr_any; the + if(Bind (SOCKETFD, (structSOCKADDR *) &servaddr,sizeof(structSOCKADDR)) ==-1) - { $Perror ("bind:"); $ } - Else - { theprintf"bind success \ r \ n"); - }Wuyi the if(Listen (Socketfd,backlog) ==-1) - { WuPerror ("Listen:"); - } About Else $ { -printf"listening...\r\n"); - } -Sin_size=sizeof(structsockaddr_in); A + if(Socketfd=accept (SOCKETFD, (structSOCKADDR *) (&clidaddr,&sin_size)) ==-1) the { -Perror ("Accept:"); $ return-1; the } the Else the { theprintf"Accept successful!\r\n"); - } in the while(1) the { Aboutmemset (BUF,0,sizeof(BUF)); the if(Reclen = recv (Socketfd,buf,max_msg_len,0))==-1) the { thePerror ("recv:"); + } - Else the {Bayi if(reclen>0) the { theReclen =0; -printf"Receive a message:%s\r\n", buf); - } the } the } the Close (SOCKETFD); the return 0; -}
The key to the above code is 62 lines:
if ((socketfd=Accept (socketfd, (struct sockaddr *) &clidaddr,&sin_size) ==-1)
Recv (socketfd, buf,max_msg_len,0);
SOCKFD assignment to accept the return value problem is solved, for the sake of experimentation convenience, here is attached the client code:
#include <sys/types.h>#include<sys/socket.h>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<netinet/inch.h>#include<netdb.h>#defineMax_msg_len 1024intMainintargcChar*arg[]) { structsockaddr_in servaddr; structHostent *host =NULL; intPort =0, SOCKETFD; CharBuf[max_msg_len] = {0}; if(argc<3) {printf ("Please input IP and port number!\r\n"); return-1; } if(host = gethostbyname (arg[1]))==NULL) { return-1; } //ipV4 TCP 0 if(Socketfd=socket (Af_inet,sock_stream,0))==-1) {perror ("Socket"); return-1; } printf ("socket FD =%d\n", SOCKETFD); memset (&SERVADDR,0,sizeof(structsockaddr_in)); Servaddr.sin_family=af_inet; Servaddr.sin_port= Htons (Atoi (arg[2])); Servaddr.sin_addr= (*(structIN_ADDR *) host->h_addr); if(Connect (SOCKETFD,structSOCKADDR *) &servaddr,sizeof(structSOCKADDR)) ==-1) {perror ("Connect:"); return-1; } while(1) {printf ("Input:"); scanf ("%s", BUF); if(Send (Socketfd,buf,sizeof(BUF),0)==-1) {perror ("Send:"); } {printf ("Send successful!\r\n"); } memset (BUF,0,sizeof(BUF)); } close (SOCKETFD); return 0;}
Solve the problem of server and client TCP communication under Linux: Accept received successfully but error