c系统调用connect()挂在客户端(网络编程)

时间:2019-04-11 01:58:45

标签: c network-programming system-calls

我正在学习套接字,并且正在遵循教科书中的一些示例代码。我有两台PC,一个用作服务器,另一个用作客户端。我尝试通过套接字使两台PC通信,但客户端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()之前,并且它已打印出来)。有人可以帮忙吗?因为我没有网络编程方面的知识,所以我什至不知道如何调试。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

在服务器计算机中:

netcat localhost 7000
hello world

您知道这是一台可以正常工作的TCP服务器,因此您将能够检测到服务器中是否存在任何错误。

在客户端计算机中

netcat -l localhost 7000

类似于服务器计算机,此命令创建一个侦听localhost:7000的TCP服务器。

它可能无法解决您的问题,因为这似乎是配置问题,如果我们不知道您使用的是哪种机器和配置设置,则很难给您一些建议。但是我认为netcat是用于此目的的强大工具。

相关问题