错误:连接到套接字服务器

时间:2016-05-17 02:41:53

标签: java c sockets

使用3个终端时,我有以下C_Client代码运行良好, 2台服务器和1台客户端。

然而,当我将同一组代码部署到3台不同的机器时, 它没有按预期执行。

有几种情况,

我使用8888端口用于server1,使用5678端口用于server2。输入参数是客户端输入的。

  1. 在同一台计算机上连接客户端和服务器 - 按预期工作 使用争论输入:127.0.0.1 127.0.0.1 8888 5678

  2. 在同一台机器上连接客户端和server2,在其他机器上连接server1,它也可以使用参数输入:192.168.28.116 127.0.0.1 8888 5678

  3. 有问题的情况是,无论我做了什么(例如关闭所有防火墙,检查端口是否收听),都会在不同的机器上连接客户端和服务器2,显示错误消息: ERROR connecting tpserver:connection refused

  4. 争论输入:

    192.168.28.116 192.168.28.117 8888 5678

    127.0.0.7 192.168.28.117 8888 5678

    从2开始,确定服务器1上的同一台机器或不同机器上的连接并不重要,但是当连接不同机器上的server2时,连接到server1套接字时奇怪地显示错误?为什么呢?

    Client.c

    //Variable declaration
    int sockfd, sockfd2, portno, portno2, n, a, pid, startonce;
    struct sockaddr_in serv_addr, serv_addr2;
    struct hostent *server, *server2; 
    
    //Opening socket
    portno = atoi(argv[3]);
    portno2 = atoi(argv[4]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
    if ((sockfd < 0) || (sockfd2 < 0)) {
        error("ERROR opening socket");
    }
    server = gethostbyname(argv[1]);
    server2 = gethostbyname(argv[2]);
    if (server == NULL){
        fprintf(stderr, "ERROR, no such host\n");
        exit(0);
    }
    
    //Serv_addr for tpServer
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {    
        error("ERROR connecting tpserver");  
    }
    
    //Serv_addr for resultServer
    bzero((char *) &serv_addr2, sizeof(serv_addr2));
    serv_addr2 = serv_addr; 
    serv_addr2.sin_port = htons(portno2);  
    if (connect(sockfd2, (struct sockaddr *) &serv_addr2, sizeof(serv_addr2)) < 0) {    
        error("ERROR connecting resultserver");  
    }
    

    Server1.c

    //Declare variables here
    int sockfd, newsockfd, portno, pid;
    socklen_t clilen;
    struct sockaddr_in serv_addr, cli_addr;
    if (argc < 2) {
        fprintf(stderr, "ERROR, no port provided\n");
        exit(1);
    }
    
    //Opening socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {    
        error("ERROR opening socket");
    }
    
    //Filling info for serv_addr
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        error("ERROR on binding");
    }
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    
    //Fork when there a successful connection
    while (1) {
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0) {
            error("ERROR on accept");
        }
        pid = fork();
        if (pid < 0) {
            error("ERROR on fork");
        }
        if (pid == 0) {
            close(sockfd);
            dostuff(newsockfd);
            exit(0);
        }
        else close(newsockfd);
    }
    close(sockfd);
    return 0; 
    }
    

    Server2.java

    public void startServer(final int portno) {
        try {
            ServerSocket serverSocket = new ServerSocket(portno);
            System.out.println("Waiting for clients to connect...");
    
            while (true) {                
                try {
                    Socket clientSocket = serverSocket.accept();
                    clientProcessingPool.submit(new ClientTask(clientSocket));
                }
                catch (IOException e) {
                    System.err.println("Unable to process client request");
                    e.printStackTrace();
                }
            }
        }
    catch (IOException e) {
            System.err.println("Unable to process client request");
            e.printStackTrace();
        }       
    }
    

    更新2016/5/17 11:44

    我改变了代码:

    //Serv_addr for resultServer
    bzero((char *) &serv_addr2, sizeof(serv_addr2));
    serv_addr2 = serv_addr; //Copied tpServer socket to resultServer info for local testing
    serv_addr2.sin_port = htons(portno2);  
    if (connect(sockfd2, (struct sockaddr *) &serv_addr2, sizeof(serv_addr2)) < 0) {    
        error("ERROR connecting resultserver");  
    }
    

    //Serv_addr for resultServer
    bzero((char *) &serv_addr2, sizeof(serv_addr2));
    serv_addr2.sin_family = AF_INET;
    bcopy((char *)server2->h_addr, (char *)&serv_addr2.sin_addr.s_addr, server2->h_length);
    serv_addr2.sin_port = htons(portno2);  
    if (connect(sockfd2, (struct sockaddr *) &serv_addr2, sizeof(serv_addr2)) < 0) {    
        error("ERROR connecting resultserver");  
    }
    

    但我仍然收到相同的错误消息:

    ./tp_client 192.168.28.152 192.168.28.130 8888 5678
    ERROR connecting tpserver: Connection refused
    

    更新2016/5/17 11:49

    我尝试使用另一台Linux机器并成功连接到两台服务器,但是当我在Windows上设置server2时,它仍然显示连接被拒绝。

    现在,它适用于3种不同的Linux但不适用于Linux的Windows?我在浏览器的Windows上测试了我的服务器,它就像魅力一样。如何将我的Linux客户端连接到Windows服务器?

    更新2016/5/18 10:52

    新发现,当客户端连接到2台服务器时,2 ips必须是相同的而不是2台不同的服务器机器,无论窗口与否。如果它们相同,则建立连接,否则连接被拒绝。我包含了perror()函数,但仍然得到相同的msg:连接被拒绝,当我在client.c中声明socket时有什么问题吗?

1 个答案:

答案 0 :(得分:0)

最后,事实证明,方法:gethostbyname已弃用且无法响应请求。更新到方法后:getaddrinfo,问题解决了。

现在的代码:

client.c

int sockfd, sockfd2;
struct addrinfo hints, *servinfo, *p;
struct addrinfo hints2, *servinfo2, *p2;
int rv, rv2;
 //Opening socket
memset(&hints, 0, sizeof(hints));
memset(&hints2, 0, sizeof(hints2));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints2.ai_family = AF_UNSPEC;
hints2.ai_socktype = SOCK_STREAM;

if ((rv = getaddrinfo(argv[1], argv[3], &hints, &servinfo)) != 0) {
    fprintf(stderr, "tpgetaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}

if ((rv2 = getaddrinfo(argv[2], argv[4], &hints2, &servinfo2)) != 0) {
fprintf(stderr, "rsgetaddrinfo: %s\n", gai_strerror(rv2));
exit(1);
}

for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
    perror("tpsocket");
    continue;
}

if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
    perror("tpconnect");
    close(sockfd);
    continue;
}

break;
}

for (p2 = servinfo2; p2 != NULL; p2 = p2->ai_next) {
if ((sockfd2 = socket(p2->ai_family, p2->ai_socktype, p2->ai_protocol)) == -1) {
    perror("rssocket");
    continue;
}

if (connect(sockfd2, p2->ai_addr, p2->ai_addrlen) == -1) {
    perror("rsconnect");
    close(sockfd2);
    continue;
}

break;
}

if (p == NULL) {
fprintf(stderr, "failed to connect tpserver\n");
exit(2);
}

if (p2 == NULL) {
fprintf(stderr, "failed to connect rsserver\n");
exit(2);
}

freeaddrinfo(servinfo);
freeaddrinfo(servinfo2);