简体   繁体   English

客户端:接收传输端点未连接

[英]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. 但是,尽管服务器程序运行良好,但客户端程序退出并显示错误:: recv:传输端点未连接。 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. 服务器代码中有2个错误。

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. inet_addr函数需要一个字符串,而INADDR_ANY常量是一个全零的IP数字。 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). 另外, printf行也不正确-地址不是短而长,您也不能使用%s (很可能会崩溃)。 Note, that the compiler (well, gcc at least) warns about it - you should always enable and read compiler warnings. 请注意,编译器(至少是gcc )会对此发出警告-您应始终启用并阅读编译器警告。 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 :) 只要您能以十六进制读取IP,就可以了:)

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. 有了这些修复程序,它对我来说效果很好(我的意思是服务器和客户端)-请注意,可能还有其他错误,我不得不承认我没有仔细阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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