2011-01-26 121 views
2

我有代碼(我的c + +套接字服務器),但我不知道我怎麼總是打開。我的服務器將關閉。當它已經發送給客戶端時它將自動關閉。但我希望它能夠等待其他客戶,永不關閉。我該怎麼做 ?哦,我也使用多線程。如何始終打開服務器?

請幫幫我。

,這裏是我的代碼

int main(void) 
{ 
    HANDLE hThread[3]; 
    DWORD dwID[3]; 
    DWORD dwRetVal = 0; 

    hThread[0] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadTwo,NULL,0,&dwID[0]); 

    dwRetVal = WaitForMultipleObjects(3, hThread, TRUE, INFINITE); 

    CloseHandle(hThread[0]); 

return 0; 
} 

long WINAPI ThreadTwo(long lParam) 
{ 
    WSADATA wsaData; 
    SOCKET ListenSocket = INVALID_SOCKET, 
      ClientSocket = INVALID_SOCKET; 
    struct addrinfo *result = NULL, 
        hints; 
    char recvbuf[DEFAULT_BUFLEN]; 
    int iResult, iSendResult; 
    int recvbuflen = DEFAULT_BUFLEN; 

    iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 
    if (iResult != 0) { 
     printf("WSAStartup failed with error: %d\n", iResult); 
     return 1; 
    } 

    ZeroMemory(&hints, sizeof(hints)); 
    hints.ai_family = AF_INET; 
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_protocol = IPPROTO_TCP; 
    hints.ai_flags = AI_PASSIVE; 

    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); 
    if (iResult != 0) { 
     printf("getaddrinfo failed with error: %d\n", iResult); 
     WSACleanup(); 
     return 1; 
    } 

    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); 
    if (ListenSocket == INVALID_SOCKET) { 
     printf("socket failed with error: %ld\n", WSAGetLastError()); 
     freeaddrinfo(result); 
     WSACleanup(); 
     return 1; 
    } 

    iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); 
    if (iResult == SOCKET_ERROR) { 
     printf("bind failed with error: %d\n", WSAGetLastError()); 
     freeaddrinfo(result); 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return 1; 
    } 

    freeaddrinfo(result); 

    iResult = listen(ListenSocket, SOMAXCONN); 
    if (iResult == SOCKET_ERROR) { 
     printf("listen failed with error: %d\n", WSAGetLastError()); 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return 1; 
    } 

    printf("Waiting for client\n"); 

    ClientSocket = accept(ListenSocket, NULL, NULL); 
    if (ClientSocket == INVALID_SOCKET) { 
     printf("accept failed with error: %d\n", WSAGetLastError()); 
     closesocket(ListenSocket); 
     WSACleanup(); 
     return 1; 
    } 

    printf("Client acctped.\n"); 

    closesocket(ListenSocket); 

    do { 

     iResult = recv(ClientSocket, recvbuf, recvbuflen, 0); 
     if (iResult > 0) { 
      printf("Bytes received: %d\n", iResult); 

      recvbuf[iResult] = '\0'; 
      printf(recvbuf); 

      char* testsend = "222 333\n444 555\n666 777" ; 

      iSendResult = send(ClientSocket, testsend , strlen(testsend)+1 , 0); 
      if (iSendResult == SOCKET_ERROR) { 
       printf("send failed with error: %d\n", WSAGetLastError()); 
       closesocket(ClientSocket); 
       WSACleanup(); 
       return 1; 
      } 
      printf("Bytes sent: %d\n", iSendResult); 
     } 
     else if (iResult == 0) 
      printf("Connection closing...\n"); 
     else { 
      printf("recv failed with error: %d\n", WSAGetLastError()); 
      closesocket(ClientSocket); 
      WSACleanup(); 
      return 1; 
     } 

    } while (iResult > 0); 

    iResult = shutdown(ClientSocket, SD_SEND); 
    if (iResult == SOCKET_ERROR) { 
     printf("shutdown failed with error: %d\n", WSAGetLastError()); 
     closesocket(ClientSocket); 
     WSACleanup(); 
     return 1; 
    } 
    system("pause"); 

    closesocket(ClientSocket); 
    WSACleanup(); 

    return 0; 
} 

再次感謝。

+0

`closesocket(ListenSocket);` - 那是故意的嗎?什麼讓你`接受()`在下一個客戶端...? – sarnold 2011-01-26 10:42:16

+0

我應該刪除closesocket(ListenSocket),即closeSocket,並用accept()替換? – crazyoxygen 2011-01-26 10:47:29

+0

否這不是工作。我想我應該在某處放置一些循環。 – crazyoxygen 2011-01-26 10:52:24

回答

3

你必須使用一個循環:)

基本上,服務器主代碼必須在一個循環,這將一直持續下去......而你不停止它。

你應該有:

// That's close to pseudo code :) 

while(notStoppedByMaster) 
{ 
    // [...] 
    ClientSocket = accept(ListenSocket, NULL, NULL); 
    handleRequest(ClientSocket); 
} 

// close the listen socket here 

handleRequest應該處理在另一個線程請求。 意識到要同步 :)

MY2C

0

首先,它看起來像你正在創建一個控制檯應用程序。如果我是你,我會創建一個WIN32應用程序。這意味着你將有一個GUI,但更重要的是,一個消息循環。消息循環允許您使用異步套接字,它需要更多的理解,但它提供了更好更清晰的結果。

閱讀有關WSAAsyncSelect: http://msdn.microsoft.com/en-us/library/ms741540%28VS.85%29.aspx 又讀插座教程: http://msdn.microsoft.com/en-us/library/ms738545%28v=VS.85%29.aspx

其次,如果你必須使用同步(阻塞)插槽,是你必須運行各在自己的線程。您需要有一個等待連接的循環,然後像接受()一樣分配客戶端套接字。然後讓客戶套接字在自己的線程上運行。

此外,你正在代碼中間由於某種原因關閉你的listensocket。