Unix服务器客户端连接:连接被拒绝

时间:2016-04-06 05:23:48

标签: c linux sockets unix connect

我在使用套接字创建第一个服务器 - 客户端应用程序时遇到了一些问题。我已将服务器的套接字地址硬编码到localhost,将端口硬编码为55555.当我尝试从单独的终端中的客户端连接到它时,键入命令./player localhost 55555,服务器没有任何反应。我认为应该发生的是服务器接收的数据是" Rock"然后打印"客户:准备好。\ n \ n \ n \ n \ n客户:Rock"。相反,服务器继续等待,对玩家没有反应。我怀疑我还没有真正连接到服务器,所以我试图硬编码套接字地址" localhost"进入播放器连接,以及55555的端口(也硬编码)。我尝试运行命令./player并且它喷出了#34; 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);
}

2 个答案:

答案 0 :(得分:1)

这是错误的:

server.sin_port = 55555;

应该是:

server.sin_port = htons(55555);

答案 1 :(得分:0)

更改

 server.sin_port = 55555;

到:

server.sin_port = htons(55555);

正在工作。