0

我创建了一个服务器和客户端来通过套接字进行通信。

但是,当我在 localhost 下运行时,客户端端口号显示与客户端绑定的号码相同。

但是如果我在不同的机器上运行,客户端端口号与客户端绑定的端口号不同。

关于为什么的任何建议?

server.c 的代码

int main(int argc, char *argv[]) {
    int socket_file_descriptor, port_number;
    struct sockaddr_in server_address;
    connection_t *connection;
    pthread_t thread;
    char *client_address;
    int client_port;
    struct hostent client;

        //check for command line arguments
        if (argc != 2) {
            fprintf(stderr,"ERROR, no port provided\n");
            exit(1);
        }

        //create socket
        socket_file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
        if (socket_file_descriptor < 0)
            error("ERROR opening socket");
        printf("Successfully opened socket\n");

        //clear server adress
        bzero((char *) &server_address, sizeof(server_address));

        //obtain port number
        port_number = atoi(argv[1]);

        //bind socket to port
        server_address.sin_family = AF_INET;
        server_address.sin_addr.s_addr = htonl(INADDR_ANY);
        server_address.sin_port = htons(port_number);
        if (bind(socket_file_descriptor, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
            error("ERROR on binding");
        printf("Successfully Binded on port %d\n", ntohs(server_address.sin_port)); //show correct port number

        //listen on port
        if(listen(socket_file_descriptor,5) < 0)
            error("ERROR on listening");

        //print message to show connection has been made
        printf("Ready and Listening...\n");

        //keep server alive with while loop
        while(1) {
            //accept incoming connections
            connection = (connection_t *)malloc(sizeof(connection_t));
            connection->addr_len = sizeof(&connection->address);
            char ip[connection->addr_len];
            connection->sock = accept(*sfd, (struct sockaddr *)&connection->address, &connection->addr_len);
            connection->addr = (long)((struct sockaddr_in *)&connection->address)->sin_addr.s_addr;
            connection->client_port_number = ntohs(((struct sockaddr_in *)&connection->address)->sin_port);


            if(getsockname(connection->sock,&connection->address, &connection->addr_len) == -1) {
                printf("Error getting client socket address: %s\n", strerror(errno));
            } else {
                //client_port_number = ntohs((struct sockaddr_in *)&connection->address).sin_port);
            }

            if(connection->sock <= 0) {
                error("ERROR on accept");
                free(connection);
            } else {
                if(pthread_create(&thread, 0, pthread, (void *)connection) < 0)
                    error("ERROR creating thread");
                printf("Thread Created... Listening on Client:[%s:%d]\n",
                inet_ntop(AF_INET,(struct sockaddr *)&connection->addr,ip,connection->addr_len),
    connection->client_port_number);
                pthread_detach(thread);
            }
        }
     return 0;
     }

和client.c的代码

int main(int argc, char *argv[]) {
    int socket_file_descriptor;         //socket
    int port_number, my_port_number;    //server port number , client port number
    struct sockaddr_in server_address, my_address;  //server IP, client IP
    struct hostent * server;
    char buffer[256];   //buffer


    if(argv < 3) { //command prompt arg [./myC localhost 12345]
        fprintf(stderr,"usage %s hostname port\n", argv[0]);
    exit(0);
    }

    port_number = atoi(argv[2]);    //server port number = 12345
    socket_file_descriptor = socket(AF_INET, SOCK_STREAM, 0); //socket() allows socket_file_descriptor to create socket
    if(socket_file_descriptor < 0) {
        error("ERROR opening socket");
    }
    printf("Successfully Opened Socket\n");
    server = gethostbyname(argv[1]);    //get IP (into network byte) of localhost
    if(server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &server_address, sizeof(server_address)); //memset(server_address)
    server_address.sin_family = AF_INET;    //AF_INET = symbolic constant
    bcopy((char *) server->h_addr, (char *)&server_address.sin_addr.s_addr, server->h_length);
    server_address.sin_port = htons(port_number); //htons = 'hostbyte' to 'networkbyte' for type 'short'
    printf("Server_Address_Port: %d\n", ntohs(server_address.sin_port)); //ntohs = 'networkbyte' to 'hostbyte' for type 'short'

    //Bind client on port
    my_address.sin_family = AF_INET;
    my_address.sin_port = htons(my_port_number);
    if (bind(socket_file_descriptor, (struct sockaddr *) &my_address, sizeof(my_address)) < 0)
             error("ERROR on binding");
    printf("Client_Address_Port: %d\n", ntohs(my_address.sin_port));

    //Connecting to the Server
    if(connect(socket_file_descriptor,(struct sockaddr *)&server_address, sizeof(server_address)) < 0) //connect to socket
        error("ERROR connecting");
    printf("Please enter the message: "); //command prompt requests [send file/ get file / send struct]
    bzero(buffer,256);
    fgets(buffer,255,stdin);

...
}

在 server.c 的控制台上(在不同的机器上):

Successfully opened socket
Successfully Binded on port 1234
Ready and Listening...Thread Created... Listening on Client:[172.23.15.197:59540]

在 client.c 的控制台上:

Successfully Opened Socket
Server_Address_Port: 1234
Client_Address_Port: 32698
Please enter the message:

当服务器在本地主机上运行时,客户端端口号与客户端保持相同的值 32698(这是客户端绑定到的随机端口)。

代码本身有什么问题吗???

提前致谢。

4

1 回答 1

0

当我在 localhost 下运行时,客户端端口号显示与客户端 [绑定] 到的号码相同。

正确的。这是同一件事。

但是如果我在不同的机器上运行,客户端端口号与客户端 [绑定] 到的端口号不同。

可能发生的唯一方法是服务器或客户端位于 NAT 设备后面。

于 2015-10-22T06:32:15.740 回答