2016-02-16 352 views
3

我創建了一個連接到C++服務器的C++客戶端。 它連接並正確發送數據到服務器。 我創建了一個單獨的線程來接收來自服務器的數據,這裏是我無法弄清楚的問題。 我得到這個錯誤:C++客戶端中的WSAGetLastError 10038

WSAGetLastError 10038 

我發現這個錯誤,因爲描述符不是一個套接字。

服務器發送方法正常工作,我用telnet客戶端測試它。

這是我的客戶端代碼:

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

using namespace std; 
int hsock; 
void *SocketHandler(void*); 

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

    //The port and address you want to connect to 
    int host_port= 2000;//1101; 
    char* host_name="10.188.126.198"; 
    pthread_t recept; 


    //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()); 
     return 0; 
    } 

    //Initialize sockets and set any options 

    int * p_int ; 
    hsock = socket(AF_INET, SOCK_STREAM, 0); 
    if(hsock == -1){ 
     printf("Error initializing socket %d\n",WSAGetLastError()); 
     return 0; 
    } 

    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); 
     return 0; 
    } 
    free(p_int); 

    //Connect to the server 
    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 = inet_addr(host_name); 

    int *csock ; 
      csock = (int*)malloc(sizeof(int)); 

    if((*csock =connect(hsock, (struct sockaddr*)&my_addr, sizeof(my_addr))) == SOCKET_ERROR){ 
     fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError()); 
     return 0; 
     } 

    cout<<"Client connect to :"<< htons(host_port)<<endl; 

    //Create thread to receive data from the server 
// the problem is here as I think 
    pthread_create(&recept,NULL,SocketHandler,(void *)csock); 


/// Send data to the server  
    char buffer[1024]; 
    int buffer_len = 1024; 
    int bytecount; 

    int c; 

    while(true){ 
      memset(buffer, '\0', buffer_len); 
      for(char* p=buffer ; (c=getch())!=13 ; p++){ 
       printf("%c", c); 
       *p = c; 
      } 


       if((bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){ 
        fprintf(stderr, "Error sending data %d\n", WSAGetLastError()); 
        return 0; 
       } 
       printf("Sent bytes %d\n", bytecount); 

    } 

    closesocket(hsock); 

return 0; 

} 
void *SocketHandler(void* lp){ 
    int *csock= (int*)lp; 
    cout<<"Start Listening Thread"<<endl; 

    char buffer[1024]; 
    int buffer_len = 1024; 
    int bytecount; 
     memset(buffer, 0, buffer_len); 

    while(1){ 

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

    } 

    free(csock); 
    return 0; 
} 
+2

檢查[連接](https://msdn.microsoft.com/en- us/library/windows/desktop/ms737625(v = vs.85).aspx)的返回值:_If沒有錯誤發生,連接返回零。否則,它會返回SOCKET_ERROR,並且可以通過調用WSAGetLastError._來檢索特定的錯誤代碼。您將其分配給'* csock',然後對其執行'recv'。 – CristiFati

+0

正如我所說,客戶端連接正確,並且可以將數據發送到服務器。問題出在recv方法的線程上。 – Kamel

+4

確切地說。 'connect'成功,並返回_0_。所以:'* csock = 0;'然後你有'if((bytecount = recv(* csock,buffer,...'你在_0_上調用'recv',通常是_stdin_。 – CristiFati

回答

2

您發送到線程是csock參數...

int *csock ; 
csock = (int*)malloc(sizeof(int)); 
... 
pthread_create(&recept,NULL,SocketHandler,(void *)csock); 

,然後你用csock作爲插座(在線程中)...

int *csock= (int*)lp; 
... 
if((bytecount = recv(*csock, buffer, buffer_len, 0))==SOCKET_ERROR) { 

我認爲你需要使用hsock爲您的插座,而不是*csock ...

BTW: 在Windows中,應該說是一個插座,不是int ...

希望它可以幫助...