簡體   English   中英

Unix服務器客戶端連接:連接被拒絕

[英]Unix server client Connect: Connection refused

我在使用套接字創建第一個服務器-客戶端應用程序時遇到一些問題。 我已將服務器的套接字地址硬編碼為localhost,端口為55555。當我嘗試在單獨的終端中從客戶端連接到它時,鍵入命令./player localhost 55555,服務器沒有任何反應。 我認為應該發生的是,服務器收到的數據是“ Rock”,然后顯示“ Client:ready。\\ nGo \\ n Client:Rock”。 相反,服務器將繼續等待,而不會對播放器做出任何反應。 我懷疑我實際上沒有連接到服務器,所以我嘗試將套接字地址“ localhost”硬編碼到播放器連接中,以及55555的端口(也進行硬編碼)。 我嘗試運行命令./player,並顯示“ Connect:Connection Refused”。 我想知道如何修復我的硬編碼播放器。

Referee.c (這是服務器)

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc, const char* argv[])
{
    char buf[1024];
    struct sockaddr_in localhost;
    int listenSock, serviceSock, bytesRead;
    size_t len = sizeof(localhost);
    int port = 55555;
    listenSock = socket(AF_INET, SOCK_STREAM, 0);
    if (listenSock<0) {
        perror("Socket");
        exit(1);
    }
    bzero((char*)&localhost, len);
    localhost.sin_family = AF_INET;
    localhost.sin_addr.s_addr = htonl(INADDR_ANY);
    localhost.sin_port = htons(port);
    bind(listenSock, (struct sockaddr*) &localhost, len);
    printf("Paper, Scissors, Rock game start.\nWaiting for player...\n");
    listen(listenSock, 5);
    do {
        serviceSock = accept(listenSock, 0, 0);
        if(serviceSock == -1){
            perror("Accept");
        }
        else do {
            bzero(buf, sizeof(buf));
            if((bytesRead = read(serviceSock, buf, 1024))<0) {
                perror("Read");
            }
            if(bytesRead > 0){
                printf("Client: ready.\nGo\n    Client: %s\n", buf);
            }
        } while (bytesRead != 0);
        close(serviceSock);
    } while(1);
}

player.c (客戶端)

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
    int sock;
    struct sockaddr_in server;
    struct hostent *hp;
    char buf[1024], *data = "Rock";
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("Socket");
        exit(1);
    }
    server.sin_family = AF_INET;
    hp = gethostbyname(argv[1]);
    if (hp == 0) {
        fprintf( stderr, "%s: unknown host", argv[1] );
        exit(1);
    }
    bcopy( hp->h_addr, &server.sin_addr, hp->h_length );

    server.sin_port = htons(atoi(argv[2]));
    if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
        perror("Connect");
        exit(1);
    }
    if(write(sock, data, strlen(data)) < 0) {
        perror("Write");
    }
    close(sock);
}

帶有硬編碼地址/端口的player.c

#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
    int sock;
    struct sockaddr_in server;
    struct hostent *hp;
    char buf[1024], *data = "Rock";
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("Socket");
        exit(1);
    }
    server.sin_family = AF_INET;
    hp = gethostbyname("localhost");
    if (hp == 0) {
        fprintf( stderr, "%s: unknown host", argv[1] );
        exit(1);
    }
    bcopy( hp->h_addr, &server.sin_addr, hp->h_length );

    server.sin_port = 55555;
    if(connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
        perror("Connect");
        exit(1);
    }
    if(write(sock, data, strlen(data)) < 0) {
        perror("Write");
    }
    close(sock);
}

這是錯誤的:

server.sin_port = 55555;

它應該是:

server.sin_port = htons(55555);

改變中

 server.sin_port = 55555;

至 :

server.sin_port = htons(55555);

正在工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM