简体   繁体   中英

Client: recv transport endpoint not connected

I am trying to implement a client-server program. However, while the server program runs fine, the client program exits with the error:: recv: transport endpoint not connected. Its been puzzling me for quite a while now. Some help will be greatly appreciated! Here is the server program ::

#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

int main()
{
    int sockid,newsockid,pid;
    struct sockaddr_in myaddr,clientaddr;
    socklen_t x;
    x=sizeof(clientaddr);
    char msg[20];
    sockid=socket(AF_INET,SOCK_STREAM,0);
    if(sockid == -1)
        perror("SOCKET");
    memset(&myaddr,0,sizeof(myaddr));
    myaddr.sin_family=AF_INET;
    myaddr.sin_port=htons(8888);
    myaddr.sin_addr.s_addr=inet_addr(INADDR_ANY);
    if(bind(sockid,(const struct sockaddr *)&myaddr,sizeof(myaddr)) == -1)
        perror("bind");
    listen(sockid,5);
    newsockid=accept(sockid,(struct sockaddr *)&clientaddr,&x);
    if(newsockid == -1)
        perror("accept");
    printf("NEW CLIENT ADDR:: %s",ntohs(clientaddr.sin_addr.s_addr));
    pid=fork();
    while(1)
    {   
        if(pid == 0)
        {
            memset(msg,'\0',20);
            if(recv(newsockid,msg,sizeof(msg),0) == -1)
                perror("recv");
            puts(msg);
            if(strcmp(msg,"exit") == 0)
            {
                close(newsockid);
                exit(getpid());
            }
        }
        if(pid!=0)
        {
            memset(msg,'\0',20);
            gets(msg);
            if(send(newsockid,msg,sizeof(msg),0) == -1)
                perror("send");
            if(strcmp(msg,"exit") == 0)
            {
                close(newsockid);
                exit(getpid());
            }
        }
    }
    return 0;
}

Also here is the client side of things::

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>

int main()
{
    int pid,sockfd;
    char msg[20];
    struct sockaddr_in myaddr;
    sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
        perror("socket");
    myaddr.sin_family=AF_INET;
    myaddr.sin_port=htons(8888);
    myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    if(connect(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)) == -1)
        perror("connect");
    pid=fork();
    while(1)
    {
        if(pid == 0)
        {
            memset(msg,'\0',20);
            if(recv(sockfd,msg,sizeof(msg),0) == -1)
            {
                perror("recv");
                exit(getpid());
            }
            puts(msg);
            if(strcmp(msg,"exit") == 0)
            {
                close(sockfd);
                exit(getpid());
            }
        }
        if(pid!=0)
        {
            memset(msg,'\0',20);
            gets(msg);
            if(send(sockfd,msg,sizeof(msg),0) == -1)
                perror("send");
            if(strcmp(msg,"exit") == 0)
            {
                close(sockfd);
                exit(getpid());
            }
        }
    }
    return 0;
}

There are 2 bugs in the server code.

One is in this line:

myaddr.sin_addr.s_addr=inet_addr(INADDR_ANY);

The inet_addr function expects a character string, while INADDR_ANY constant is an IP number with all zeros. The server code crashed with segmentation fault for me until this line was corrected (did it really work for you?). The correct usage:

myaddr.sin_addr.s_addr=htonl(INADDR_ANY);

Also, printf line is not correct - address is not short, but long, also you can't use %s (it would most likely crash). Note, that the compiler (well, gcc at least) warns about it - you should always enable and read compiler warnings. As a quick fix I've changed it to:

printf("NEW CLIENT ADDR:: %08x",ntohl(clientaddr.sin_addr.s_addr));

This is fine as long as you can read IP in hex :)

With those fixes it worked fine for me (I mean both server and client) - note, that there can be other bugs, I have to admit I didn't read it too carefuly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM