Today, when doing the socket network communication experiment, the server side executes the error at the RECV function: Transport endpoint is not connected.
The following are the source code for server and client:
Server.c
1 #include <stdio.h>
2 #include <arpa/inet.h>
3 #include <sys/socket.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #define SERVER_PORT 5000
Eight
Nine
10 int main(){
11 / * address initialization*/
12 struct sockaddr_in serveraddr;
13 struct sockaddr_in clientaddr;
14 memset(&serveraddr,0,sizeof(serveraddr));
15 serveraddr.sin_family = AF_INET;
16 serveraddr.sin_addr.s_addr = INADDR_ANY;
17 serveraddr.sin_port = htons(SERVER_PORT);
18 bzero(&(serveraddr.sin_zero),8);
19 //1.create sock
20 int sock,conn,sbind,lis;
21 char buf[32];
22 sock = socket(AF_INET,SOCK_STREAM,0);
23 if(sock ==-1){perror("sock error");exit(1);}
24 //2.bind
25 sbind = bind(sock, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
26 if(sock ==-1){perror("bind error");exit(1);}
27 //3.listen
28 lis = listen(sock, 2);
29 if(lis ==-1){perror("listen error");exit(1);}
30 //4.accept
31 int acc,snd,rec;
32 int len = sizeof(struct sockaddr_in);
33 while(1){
34 acc = accept(sock,(struct sockaddr*)&clientaddr, (socklen_t*)&len);
35 if(acc == -1){
36 perror("accept error!");continue;
37}
38 If ((REC = recv (sock, buf, 1024,0)) = = - 1) {PERROR ("server Connect failed!"); exit (1);} / / error location
39 buf[rec]=‘\0‘;
40 if(strcmp(buf,"liu")==0){
41 snd = send(acc, "Hello,You are connected\n", 24, 0);
42}
43}
Forty-four
45 close(sock);
46 return 0;
47}
Client.c
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define SERVER_PORT 5000
Int main () {
/*Address initialization*/
struct sockaddr_in serveraddr;
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = INADDR_ANY;
serveraddr.sin_port = htons(SERVER_PORT);
bzero(&(serveraddr.sin_zero),8);
//1.create sock
int sock,conn;
char buf[32];
sock = socket(AF_INET,SOCK_STREAM,0);
conn = connect(sock, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
if(conn ==-1){perror("connect error");exit(1);}
Int snd;
snd = send(sock, "liu", 4, 0);
if(snd ==-1){perror("send error");exit(1);}
Int rec;
if((rec=recv(sock,buf,1024,0))==-1){perror("client connect failed!");exit(1);}
buf[rec]= ‘\0‘;
printf("received:%s",buf);
Close (sock);
Return 0;
}
The error is in the Recv function.
First, let's take a look at the Accept function: int sockfd, void *addr, int *addrlen);
Its return value is a client socket descriptor, so the first parameter of the recv and send functions should be filled in with the socket descriptor returned by accept.
So the server.c in the
if ((Rec=recv (sock,buf,1024,0)) ==-1) {perror ("Server Connect failed!"); Exit (1);}
Switch
if ((Rec=recv (acc,buf,1024,0)) ==-1) {perror ("Server Connect failed!"); Exit (1);} Can be
recv function Error during socket programming: Transport endpoint is not connected resolution