3

我找到了这个 Sockets 教程http://www.binarytides.com/socket-programming-c-linux-tutorial/,我在最后一个例子中遇到了麻烦。它是一个使用套接字和 pthread 的线程服务器。

代码编译得很好,但没有按预期工作。我怀疑pthread_join电话是罪魁祸首,但我不确定。

我已经尝试使用 GCC 4.8.3 和 4.8.2 在 Cygwin(32 位)和 Alpine Linux(32 位)上编译和运行服务器和客户端。两种环境中的问题都是相同的。

我已经阅读了http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.htmlpthread_join上的 pthreads文档,但我似乎在服务器代码中的调用中找不到任何问题。pthread_join

更新了服务器、客户端和输出。

服务器代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>

void *connection_handler(void *);

int main(int argc, char *argv[])
{
    int socket_desc, new_socket, c;
    struct sockaddr_in server, client;
    char *message;

    // Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    // Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(2000);

    // Bind
    if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0)
    {
        puts("bind failed");
        return 1;
    }
    puts("bind done");

    // Listen
    listen(socket_desc, 3);

    // Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    while ((new_socket = accept(socket_desc, (struct sockaddr *) &client, (socklen_t*)&c)) )
    {
        puts("Connection accepted");

        // Reply to the client
        message = "Hello Client, I have received your connection. And now I will assign a handler for you\n";
        write(new_socket, message, strlen(message));

        pthread_t sniffer_thread;

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

        // Now join the thread , so that we don't terminate before the thread
        // pthread_join(sniffer_thread, NULL);
        puts("Handler assigned");
    }

    if (new_socket < 0)
    {
        perror("accept failed");
        return 1;
    }
    return 0;
}

/*
 * This will handle connection for each client
 * */
void *connection_handler(void *socket_desc)
{
    // Get socket descriptor
    int sock = (int)socket_desc;
    int read_size;
    char *message, client_message[2000]; 

    // Send some messages to the client
    message = "Greetings! I am your connection handler\n";
    write(sock, message, strlen(message) + 1);

    message = "Now type something and i shall repeat what you type\n";
    write(sock, message, strlen(message) + 1);

    // Receive a message from client
    read_size = recv(sock, client_message, 1999, 0); 

    if (read_size > 0) 
    {
        client_message[read_size] = 0; 
    }
    else
    {
        puts("recv failed");
    }

    //while(read_size > 0 )
    //{
        // Send the message back to client
        write(sock, client_message, strlen(client_message) + 1);
    //}

    if (read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if (read_size == -1)
    {
        perror("recv failed");
    }

    close(sock);

    return 0;
}

客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int arc, char *argv[])
{
    int socket_desc;
    struct sockaddr_in server;
    char *message, server_reply[2000];

    // Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons(2000);

    // Connect to remote server
    if (connect(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected\n");

    // Send some data
    message = "Hello server!";
    if (send(socket_desc, message, strlen(message), 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
    puts("Data Send\n");

    int length = 0;
    do {
        // Receive a reply from the server
        length = recv(socket_desc, server_reply, 1999, 0); 

        if (length > 0)
        {
            server_reply[length] = 0; 
        }
        else
        {
            puts("Nothing to recieve");
            close(socket_desc);
            return 1;
        }

        puts("Reply received\n");
        puts(server_reply);

    } while (length > 0);

    return 0;
}

服务器输出:

bind done
Waiting for incoming connections...
Connection accepted
Test 1
Segmentation fault (core dumped)

客户端输出:

Connected
Data Send
Reply received
Hello Client , I have received your connection. And now I will assign a handler for you
recv failed

看起来主线程在 sniffer_thread 之前返回/完成。

任何帮助都会得到帮助。

4

1 回答 1

2

您会遇到未定义的行为,因为您调用strlen的字符串不是以 '\0' 结尾的。recv不在字符串末尾放置空终止符。

char *message , client_message[2000];
//...
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
                                     ^^^^^^^^^^^^^^
                                     not null terminated

您应该使用的返回值recv来检查读取数据的大小。

于 2014-09-30T17:57:11.353 回答