首页 > 解决方案 > c系统调用connect()挂在客户端(网络编程)

问题描述

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

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

服务器:

#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(),它被打印出来了)。任何人都可以帮忙吗?因为我没有网络编程的先验知识,所以我什至不知道如何调试。

标签: cnetwork-programmingsystem-calls

解决方案


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


推荐阅读