【问题标题】:c system call connect() hangs on client side (network programming)c系统调用connect()挂在客户端(网络编程)
【发布时间】:2019-04-11 01:58:45
【问题描述】:

我正在学习套接字,并且正在遵循教科书中的一些示例代码。我有两台电脑,一台作为服务器,另一台作为客户端。我尝试让两台电脑通过套接字进行通信,但客户端 connect() 调用挂起。因为我刚开始学习所以我不知道发生了什么。

我试图搜索 c connect() 挂起但没有运气。我通过 ifcongif inet 138.51.83.123了解了我的服务器 IP

服务器:

#include <sys/stat.h> 
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/wait.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <ctype.h> 
#include <sys/types.h> 
#include <signal.h> 

#define SIZE sizeof(struct sockaddr_in)

void catcher(int sig);
int newsockfd;

int main(){
    int sockfd;
    char c;
    struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    static struct sigaction act;

    act.sa_handler = catcher;
    sigfillset(&(act.sa_mask));
    sigaction(SIGPIPE, &act, NULL);

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("socket call failed");
        exit(1);
    }

    if(bind(sockfd, (struct sockaddr*)&server, SIZE) == -1){
        perror("bind call failed");
        exit(1);
    }

    if(listen(sockfd, 5) == -1){
        perror("listen call failed");
        exit(1);
    }

    for(;;){
        if((newsockfd = accept(sockfd, NULL, NULL)) == -1){
            perror("accept call failed");
            continue;
        }

        if(fork() == 0){
            // keep reading if not EOF
            while(recv(newsockfd, &c, 1, 0) > 0){
                printf("***received: %c\n", c);
                c = toupper(c);
                send(newsockfd, &c, 1, 0);
            }
            close(newsockfd);
            exit(0);
        }
        // parent no need for newsockfd
        close(newsockfd);
    }
    return 0;
}

void catcher(int sig){
    close(newsockfd);
    exit(0);
}

客户:

#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close

#define SIZE sizeof(struct sockaddr_in)

int main(){

    int sockfd;
    char c, rc;
    struct sockaddr_in server = {AF_INET, 7000};
    server.sin_addr.s_addr = inet_addr("138.51.83.71");
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("socket call failed");
        exit(1);
    }

    if(connect(sockfd, (struct sockaddr*)&server, SIZE) == -1){
        printf("here\n");
        perror("connect call failed");
        exit(1);
    }

    printf("here\n"); // never got printed out


    for(;;){
        printf("Input a lowercase char\n");

        c = getchar();
        send(sockfd, &c, 1, 0);

        if(recv(sockfd, &rc, 1, 0) > 0) printf("receive: %c", rc);
        else{
            printf("server died\n");
            close(sockfd);
            exit(1);
        }
    }

}

在客户端,printf("here\n"); // never got printed out 所以这就是为什么我猜客户端 connect() 挂起(我试图把这个 printf 放在 connect() 之前,它被打印出来了)。任何人都可以帮忙吗?因为我没有网络编程的先验知识,所以我什至不知道如何调试。

【问题讨论】:

    标签: c network-programming system-calls


    【解决方案1】:

    首先尝试从您的客户端计算机 ping 服务器的 IP 地址并查看结果。如果 ping 不成功,则表示您的网络连接存在问题。我使用服务器 IP 为 127.0.0.1(localhost) 运行您的程序,它运行完美,这表明您的代码没有问题。

    【讨论】:

      【解决方案2】:

      您的客户端似乎看不到您的服务器(使用 ping 您将能够检测到这种情况)。如果您是 Linux 用户,调试此类程序的一种简单方法是使用 netcat。 Netcat 是一个工具,它可以让您在其他事物之间通过 UDP 或 TCP 创建客户端或服务器。如果你在不同的机器上有你的代码片段,我会将 IP 更改(仅出于调试目的)到 localhost,然后我会启动:

      在服务器机器中:

      netcat localhost 7000
      hello world
      

      这是一个您知道工作正常的 TCP 服务器,因此您将能够检测到您的服务器是否有任何问题。

      在客户端机器中

      netcat -l localhost 7000
      

      与服务器机器类似,此命令创建一个 TCP 服务器,监听 localhost:7000。

      它可能无法解决您的问题,因为它似乎是一个配置问题,如果我们不知道您正在使用什么样的机器和配置设置,很难给您一些建议。但我认为 netcat 是一个强大的工具。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多