2015-05-04 124 views
1

我目前正在嘗試實現一個類來創建和使用TCP連接。 (我在C++中很新,所以有可能我忘了一些明顯的東西,儘管我一直在認真檢查我的代碼)10038 wsa socket error recv

請注意,稍後我將實現第二個類來處理新的連接,是代碼的一些部分不完整/安排,我希望這不會打擾你,而試圖理解它。

這裏是我的問題:

服務器端和客戶端似乎做工精細(我總是檢查我聽,捆綁,連接功能:沒有錯誤),我顯然能夠與發送數據客戶端,但是當我嘗試從服務器讀取它時,我得到WSA錯誤10038(這意味着我在一個無效的套接字上執行recv())。我一直在查找它幾天,似乎無法找到我的錯誤。除了代碼中的測試之外,我用「netstat -an -p tcp」來檢查連接的狀態,一切都很正常,但服務器不會收到數據。

注意:我正在使用Visual Studio 6(是的,它很舊,但它是強制性的,所以......)和winsock,而不是winsock2。

如果我忘記了一些問題,我想盡可能地完整,但忘記一些事情並不是不可能的。

這裏是我的源代碼,我試圖把它切到必要的,但恐怕錯誤可以幾乎任何地方:

CmTcpTransport.cpp

/** 
Initializes the TCP connexion (socket) in CLIENT or SERVER mode. 
It does not connect yet. 

@param strIPAddress: IP Adress to reach 
     nPort: Port to connect to 
     hSocket: client Socket if SERVER mode 
     nTcpMode: mode for this connexion SERVER(1) or CLIENT(2)  

@return an int to get the exit point of the function 
*/ 
int CmTcpTransport::initialize(std::string strIPAddress, unsigned int nPort,   SOCKET hSocket, int nTcpMode) 
{ 
m_nTcpMode = nTcpMode; 

cout << "Creating TCP connexion..." << endl << endl; 

if (nTcpMode == 1) // SERVER 
{ 

    serveraddr.sin_family = AF_INET; // address family Internet 
    serveraddr.sin_port = htons (nPort); // Port to connect on 
    serveraddr.sin_addr.s_addr = inet_addr (strIPAddress.c_str()); // Target IP 

    cout << "SERVER: Retreaving socket information..." << endl; 

    if (hSocket == INVALID_SOCKET) 
    { 
     return 102; // The received socket is invalid 
     cout << "SERVER: Socket creation failed" << endl; 
    } 

    serverSocket = hSocket; 

    cout << "SERVER: Listening socket received" << endl; 

    if (bind(serverSocket, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR) 
    { 
     return 103; // Couldn't bind 
     cout << "SERVER: Socket binding failed" << endl; 
    } 
    else { 
     cout << "SERVER: Binding successful" << endl; 
    } 

    if (listen(serverSocket, 1) == SOCKET_ERROR) 
    { 
     return 103; // Couldn't listen 
     cout << "SERVER: Socket listening failed" << endl; 
    } 
    else { 
     cout << "SERVER: Listening to socket" << endl; 
    } 

} 
else // CLIENT 
{ 
    serveraddr.sin_family = AF_INET; // address family Internet 
    serveraddr.sin_port = htons (nPort); // Port to connect on 
    serveraddr.sin_addr.s_addr = inet_addr (strIPAddress.c_str()); // Target IP 

    cout << "CLIENT: Creating client socket..." << endl; 

    clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // CREATE CLIENT SOCKET 

    if (clientSocket == INVALID_SOCKET) 
    { 
     return 104; // Couldn't create the socket 
     cout << "CLIENT: Socket creation failed" << endl; 
    } 

    cout << "CLIENT: Client socket created" << endl; 


} 

cout << "Initialization succeeded." << endl << endl << endl; 
return 105; 
} 

/** 
Creates the connexion to TCP 

@param none   
@return an int to get the exit point of the function 
*/ 
int CmTcpTransport::connectTcp() 
{ 


if (m_nTcpMode == 1) // SERVER 
{ 
    cout << "Waiting for connexion..." << endl; 
    int clientLen(0); 
    clientLen = sizeof(clientaddr); 


    if ((clientSocket = accept(serverSocket, (SOCKADDR *)&clientaddr, &clientLen) == INVALID_SOCKET)) 
    { 
     cout << "SERVER: Connexion failed." << endl; 
     return 202;; // Couldn't listen 
    } 
    else { 
     cout << "SERVER: Connexion established." << endl << endl << endl; 
     if (clientSocket == SOCKET_ERROR) 
     { 
      printf("socket became invalid after connexion:: %d\n", WSAGetLastError()); 
     } 
     else { 
       cout << "SOCKET STILL VALID AND WORKING AFTER CONNEXION" << endl; 
     } 
     return 201; 
    } 


} 
else // CLIENT 
{ 
    cout << "Connecting..." << endl; 
    if (connect(clientSocket, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR) 
    { 
     cout << "CLIENT: Connexion failed." << endl << endl << endl; 
     return 202;; // Couldn't connect 
    } 
    else 
    { 
     cout << "CLIENT: Connexion to server succeeded." << endl << endl <<   endl; 
    } 
    return 203; 
} 
} 

/** 
Receives a buffer of bytes from the TCP 

@param buffer: buffer to store the data 
     bufLen: size of the buffer 

@return nbChar: the result of the recv function 
if no error occurs, recv returns the number of bytes received 
else, it returns a value of SOCKET_ERROR 
*/ 
int CmTcpTransport::recvTcp(char *buffer, int bufLen) 
{ 
if (clientSocket == SOCKET_ERROR) 
     { 
      printf("socket became invalid before receive:: %d\n", WSAGetLastError()); 
     } 
     else { 
       cout << "SOCKET STILL VALID AND WORKING BEFORE RECEIVE" <<   endl; 
     } 

int nbChar = recv(clientSocket, buffer, bufLen, 0); 
if (nbChar == SOCKET_ERROR) 
{ 
    printf("recv failed: %d\n", WSAGetLastError()); 
    cout << "No data received." << endl << endl << endl; 
    return nbChar; 
} 
else { 
    buffer[nbChar]=0; // Gestion de la taille du buffer 
    cout << "Data received: " << buffer << endl << endl << endl; 
    return nbChar; 
} 
} 
/** 
Sends a buffer of bytes to the TCP 

@param buffer: buffer containing the data 
     bufLen: size of the buffer 

@return nbChar: the result of the send function 
if no error occurs, recv returns the number of bytes sent 
else, it returns a value of SOCKET_ERROR 
*/ 
int CmTcpTransport::sendTcp(char *buffer, int bufLen) 
{ 

int nbChar = send(clientSocket, buffer, bufLen,0); 
if (nbChar == SOCKET_ERROR) 
{ 

    cout << "No data sent." << endl << endl << endl; 
    return nbChar; 
} 
else { 
    cout << "Data sent: " << buffer << endl << endl << endl; 
    return nbChar; 
} 
} 

服務器main.cpp中

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

cout << "Initializing winsock library..." << endl; 

// Start up Winsock 
WSADATA wsadata; 

int error = WSAStartup(0x101, &wsadata); 

// Error ? 
if (error) 0; 

// Try Winsock Version ? 
if (wsadata.wVersion != 0x101) 
{ 
    WSACleanup(); // Clean up Winsock 
    return 101; 
} 

cout << "Initialization successful" << endl << endl << endl; 


// ----------------------- SERVER --------------------------- 

char buffer[3000]; 

int testRecv; // return of the recv function 

cout << "SERVER" << endl << endl << endl; 

// Simulate the activity of the TcpListener transfering a listening Socket for the client connexion 
SOCKET testSocket; 
testSocket = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP); 

// Creating a new TCP transport 
CmTcpTransport server; 

// Initialize and connect the TCP transport 
server.initialize("127.0.0.1", 10000, testSocket, TTCP_MODE_SERVER); 

server.connectTcp(); 

// Try to receive data 
testRecv = server.recvTcp(buffer, sizeof(buffer)); 

cout << testRecv << endl; 

// Disconnect 
server.disconnectTcp(); 


return 0; 
} 

客戶的main.c第

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

// ----------------------- WINSOCK ---------------------------- 

cout << "Initializing winsock library..." << endl; 

// Start up Winsock 
WSADATA wsadata; 

int error = WSAStartup(0x0202, &wsadata); 

// Error ? 
if (error) 0; 

// Try Winsock Version ? 
if (wsadata.wVersion != 0x0202) 
{ 
    WSACleanup(); // Clean up Winsock 
    return 101; 
} 

cout << "Initialization successful" << endl << endl << endl; 


// ----------------------- CLIENT --------------------------- 

char buffer[10000]; 

cout << "CLIENT" << endl << endl << endl; 

// this Socket won't be used as the TCP transport is in client mode 
SOCKET test; 
test = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP); 

// Create, initialize and connect the client TCP transport 
CmTcpTransport client; 

client.initialize("127.0.0.1", 10000, test, TTCP_MODE_CLIENT); 

client.connectTcp(); 

// Copy the data in the buffer and send it 
strcpy(buffer, "Test donnees"); 

client.sendTcp(buffer, sizeof(buffer)); 

client.disconnectTcp(); 

return 0; 
} 

這裏是我的控制檯的屏幕截圖,當我運行兩個電源:

http://www.hostingpics.net/viewer.php?id=892752Consoles.jpg

感謝你的幫助,我意識到這是很多讀/過程,我很欣賞努力!

+1

這通常是一個調試器是什麼,不是關於SO的問題。 –

+0

Smal編輯:我已經糾正了事實,即我的「返回」的錯誤情況之前,我的「錯誤...」,這是一箇舊的,但我的代碼工作版本。而通過工作,我的意思是我達到了recv()的錯誤10038。 –

回答

2

我認爲你是不正確的支架上accept無效的新襪子ID:

if ((clientSocket = accept(serverSocket, (SOCKADDR *)&clientaddr, &clientLen) == INVALID_SOCKET))

嘗試將其更改爲:

if ((clientSocket = accept(serverSocket, (SOCKADDR *)&clientaddr, &clientLen)) == INVALID_SOCKET)

+0

謝謝!有效。我確定這是荒謬的,我想這是一個很好的教訓。 –

+0

謝謝,Prabhu,我用同樣的問題撕掉了我的頭髮! –