在C中使用平台无关的线程处理程序的套接字I / O问题

时间:2019-02-11 03:43:16

标签: c sockets

我尝试创建一个套接字服务器,该套接字服务器应在Linux和Windows上以相同的方式工作。当服务器在Windows上运行时,它将按预期工作。

但是我在在Linux 中读写客户端套接字时遇到问题。客户端正确连接,并且也创建了线程,但是通信不起作用。因此,这可能是线程处理程序函数内部的错误。这是示例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>

#ifdef _WIN32 /* Windows */
#include <winsock.h>
#include <io.h>
#else /* UNIX/Linux */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif

#define RCVBUFSIZE 1024

/**
 * Thread Handler
 */
#ifdef _WIN32
DWORD WINAPI process_thread(LPVOID lpParam) {
    SOCKET current_client = (SOCKET)lpParam;
#else
void process_thread(int sock) {
    int current_client = sock;
#endif
    printf("[***] thread spawned\n");
    char buf[1024];
    int res;       
    while(1) {       
        res = recv(current_client, buf, 1024, 0);           
        if(res>0) {
            buf[res] = '\0';
            send(current_client, buf, strlen(buf), 0);
        }       
    }    
}

/**
 * Main
 */
int main( int argc, char *argv[]) {

    int port = port = atoi(argv[1]);

    struct sockaddr_in server, client;

    #ifdef _WIN32
    SOCKET sock, fd;
    #else
    int sock, fd;
    #endif

    unsigned int len;

    #ifdef _WIN32
    WSADATA wsaData;
    WSAStartup(0x102,&wsaData);
    #endif

    memset( &server, 0, sizeof (server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(port);

    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    bind(sock, (struct sockaddr*)&server, sizeof( server));
    listen(sock, 5);

    #ifdef _WIN32
        DWORD thread;
    #else
        pthread_t thread;
    #endif

    while(1) {
        len = sizeof(client);
        fd = accept(sock, (struct sockaddr*)&client, &len);
        if(fd>0) {
            printf("[***] new client connected\n");
            #ifdef _WIN32
            CreateThread(NULL, 0, process_thread, (LPVOID)fd, 0, &thread);
            #else
            pthread_create( &thread , NULL , process_thread , (void*) &client);
            #endif
        }
    }

    #ifdef _WIN32
    closesocket(fd);
    #else
    close(fd);
    #endif

    return 1;
}

0 个答案:

没有答案