连接到服务器的新端口套接字时,客户端始终会拒绝连接

时间:2015-04-15 13:41:36

标签: c sockets multiprocessing fork server

我正在设计基于套接字编程的客户端/服务器系统。客户端首先连接到服务器上具有固定端口Pa的套接字Sa,以获得用于数据传输的新端口号Pb。获得Pb之后,关闭Sa并使用Pb创建一个新的插槽Sb,但客户端总是说连接被拒绝'。

以上所有内容都在Sa accept()之后的子进程fork()中,那么问题是什么?

while (1)
{
    struct sockaddr_in client_addr;
    socklen_t length = sizeof(client_addr);

    int new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length);
    if ( new_server_socket < 0)
    {
        printf("Server Accept Failed!\n");
        break;
    }

    printf("get new connection!\n");

    int child_process_pid = fork(); 
    if(child_process_pid == 0 )
    {
        close(server_socket); 
        int dataSock;
        char buffer[BUFFER_SIZE];
        bzero(buffer,BUFFER_SIZE);
        length = recv(new_server_socket,buffer,BUFFER_SIZE,0);
        if (length < 0)
        {
            printf("Server Recieve Data Failed!/n");
            exit(1);
        }

        if(strcmp(buffer, "Get New Port\n") == 0)
        {
            srand((unsigned)time(NULL));
            dataSock = (rand() % 10000) + 20000;
            //printf("num is %d\n", num);
            char sockNum[6];
            sprintf(sockNum, "%d", dataSock);
            bzero(buffer, BUFFER_SIZE);
            strcpy(buffer, "The new port num is ");
            strcat(buffer, sockNum); 
            strcat(buffer, " \n");

            struct sockaddr_in data_server_addr;
            bzero(&data_server_addr,sizeof(data_server_addr)); 

            data_server_addr.sin_family = AF_INET;
            data_server_addr.sin_addr.s_addr = htons(INADDR_ANY);
            data_server_addr.sin_port = htons(dataSock);

            int data_server_socket = socket(AF_INET,SOCK_STREAM,0);
            if( data_server_socket < 0)
            {
                printf("Create Socket Failed!");
                exit(1);
            }

            if( bind(data_server_socket,(struct sockaddr*)&data_server_addr,sizeof(data_server_addr)))
            {
                printf("Server Bind Port : %d Failed!", dataSock); 
                exit(1);
            }

            if ( listen(data_server_socket, LENGTH_OF_LISTEN_QUEUE) )
            {
                printf("Server Listen Failed!"); 
                exit(1);
            }

            send(new_server_socket,buffer,BUFFER_SIZE,0);
            close(new_server_socket);   

                while(1)
                {
                    struct sockaddr_in client_addr;
                    socklen_t length = sizeof(client_addr);

                    int new_data_server_socket = accept(data_server_socket,(struct sockaddr*)&client_addr,&length);

                    if ( new_data_server_socket < 0)
                    {
                        printf("Server Data Connection Accept Failed!\n");
                        break;
                    }

                    printf("get new data connection!\n");

1 个答案:

答案 0 :(得分:0)

在绑定成功之前,您不应发送新的端口号。而且您根本不需要使用随机数据端口号。只需指定零,执行bind(),调用getsockname(),然后查看系统分配的端口号。然后将 发送给客户端。