0

我正在尝试使用 C++ 制作异步服务器侦听器。我可以编写没有异步的简单侦听器,但现在我遇到了问题CreateThread

例如,如果客户端已连接,控制台给了我关于此的结果 + 嗅探器可以修复它,10 秒后客户端必须再次向我发送具有不同数据的相同数据包。我的控制台没有给我关于那个数据包的结果,但是嗅探器可以看到那个数据包......如果有人能看到我的问题,请解释一下。

#include <winsock2.h>
#include <windows.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>

DWORD WINAPI SocketHandler(void*);

int main(int argv, char** argc){

    //The port you want the server to listen on
    int host_port = 7878;

    //Initialize socket support WINDOWS ONLY!
    unsigned short wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
     err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 ||
            HIBYTE( wsaData.wVersion ) != 2 )) {
        fprintf(stderr, "Could not find useable sock dll %d\n",WSAGetLastError());

    }

    //Initialize sockets and set any options
    int hsock;
    int * p_int ;
    hsock = socket(AF_INET, SOCK_STREAM, 0);
    if(hsock == -1){
        printf("Error initializing socket %d\n",WSAGetLastError());

    }

    p_int = (int*)malloc(sizeof(int));
    *p_int = 1;
    if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
        (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
        printf("Error setting options %d\n", WSAGetLastError());
        free(p_int);

    }

    free(p_int);

    //Bind and listen
    struct sockaddr_in my_addr;

    my_addr.sin_family = AF_INET ;
    my_addr.sin_port = htons(host_port);

    memset(&(my_addr.sin_zero), 0, 8);
    my_addr.sin_addr.s_addr = INADDR_ANY ;

    if( bind( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
        fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d\n",WSAGetLastError());

    }
    if(listen( hsock, 10) == -1 ){
        fprintf(stderr, "Error listening %d\n",WSAGetLastError());

    }

    //Now lets to the server stuff

    int* csock;
    sockaddr_in sadr;
    int    addr_size = sizeof(SOCKADDR);

    while(true){
        printf("waiting for a connection\n");
        csock = (int*)malloc(sizeof(int));

        if((*csock = accept( hsock, (SOCKADDR*)&sadr, &addr_size))!= INVALID_SOCKET ){
            printf("Received connection from %s",inet_ntoa(sadr.sin_addr));
            CreateThread(0,0,&SocketHandler, (void*)csock , 0,0);
        }
        else{
            fprintf(stderr, "Error accepting %d\n",WSAGetLastError());
        }
    }


}

DWORD WINAPI SocketHandler(void* lp){

    int *csock = (int*)lp;
    char buffer[1024];  
    int buffer_len = 1024;
    int bytecount;
    memset(buffer, 0, buffer_len);
    if((bytecount = recv(*csock, buffer, buffer_len, 0))==SOCKET_ERROR){
        fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());   
    }
    printf("Received bytes %d\n Received string \"%s\"\n", bytecount, buffer);



    char buff[1] = {0x11};        
    if((bytecount = send(*csock, buff, 1, 0))==SOCKET_ERROR){
        fprintf(stderr, "Error sending data %d\n", WSAGetLastError());    
    }

    printf("Sent bytes: %d. Send Message: %s\n ", bytecount,buff);


    free(csock);

}
4

2 回答 2

0

竞争条件很可能是您的问题的原因。您在堆栈上声明 csock,然后将其传递给 SocketHandler。问题是,由于 csock = accept() 处于循环中,因此 csock 的值很可能在 SocketHandler 获取并复制之前发生更改。解决方案是使用 malloc 动态分配 csock,然后让 SocketHandler 释放它。

即使这不是您的特定问题的原因,在您修复它之前,此代码也永远不会可靠。

于 2013-02-22T21:15:14.050 回答
0

CreateThread 不会循环或任何东西。您需要将代码放入循环中。像这样的东西:

DWORD WINAPI SocketHandler(void* lp) {
  for(;;) {
    /* Code here */
  }
  return EXIT_SUCCESS;
}
于 2017-10-07T17:30:16.303 回答