客户端与服务器建立多个连接

时间:2015-02-01 01:39:56

标签: c network-programming

我已根据研究更新了我的代码:

while (number_of_connections--) {
    client_sock = socket(AF_INET , SOCK_STREAM , 0);

    if (connect(client_sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
    }

    printf("socket %d created\n", client_sock);

    pthread_t sniffer_thread;
    new_sock = malloc(1);
    *new_sock = client_sock;

    if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
    {
        perror("could not create thread");
        return 1;
    }
}

然后我用这个函数处理它:

void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size, cursor;
    char *message , client_message[2000];

    //Send some messages to the client
    char handshakeBuf[sizeof(handshake)];
    memcpy(handshakeBuf, &handshake, sizeof(handshake));

    handshake.a++;
    handshake.c = 0xac;
    handshake.d = 0x0d;

    //Send some data
    if( send(sock , handshakeBuf , sizeof(handshakeBuf) , 0) < 0)
    {
        puts("Send failed");
    }

    //keep communicating with server
    while(1)
    {
        //Receive a reply from the server
        if( recv(sock , client_message , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts(client_message);
    }

    close(sock);

    return 0;
}

现在我的问题是为什么它到达第4个连接时会突然停止?

原始问题

我正在尝试编写我的第一个C客户端。我需要从一个客户端创建4个连接到服务器,以模拟连接的4个客户端,其中每个连接当然都有自己的处理程序。

这是我到目前为止所做的:

void connect_to_server(struct sockaddr_in server);

int main(int argc , char *argv[])
{
    int number_of_connections, x;
    struct sockaddr_in server;
    server.sin_addr.s_addr = inet_addr("ipaddress");
    server.sin_family = AF_INET;
    server.sin_port = htons( port );

    number_of_connections = 4;

    for ( x = 0; x < number_of_connections; x++ ) {
        connect_to_server(server);
    }

    return 0;
}

void connect_to_server(struct sockaddr_in server) {
    int sock;

    char message[1000] , server_reply[2000];

    sock = socket(AF_INET , SOCK_STREAM , 0);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
    }

    while(1)
    {
        scanf("%s" , message);

        if( send(sock , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
        }

        if( recv(sock , server_reply , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts(server_reply);
    }

    close(sock);
}

嗯,我显然也是C的新手。那有什么不对?我应该声明更多套接字,例如:int sockA, sockB, sockC, sockD或者我认为它是while loop内的connect_to_server吗?

1 个答案:

答案 0 :(得分:1)

似乎新版本的实现使用多线程:pthread_create(...)函数调用。但是在创建线程之后是否有等待的实现?

例如,可以实施等待:

  • 使用pthread_join(...)函数调用;
  • 使用getchar()函数调用等待特定按键事件。

备注

请小心这些陈述:

if (send(sock, handshakeBuf, sizeof(handshakeBuf), 0) < 0)
if (recv(sock, client_message, 2000 , 0) < 0)

send()recv()函数不保证在一次函数调用后将发送/接收整个缓冲区。这些函数返回实际的发送/接收字节数。

请介绍send()recv()函数调用的返回值分析:如果不是所有字节都发送则继续发送,如果收到“不够”字节则继续接收。此外,还有一篇文章涉及网络编程的一些基础知识:TCP/IP client-server application: exchange with string messages

相关问题