簡體   English   中英

10038 WSA套接字錯誤記錄

[英]10038 wsa socket error recv

我目前正在嘗試實現一個類來創建和使用TCP連接。 (我是C ++的新手,所以即使我一直在認真檢查我的代碼,也有可能忘記了一些明顯的東西)

請注意,稍后我將實現第二個類來處理新的連接,因此代碼的某些部分不完整/已安排好,希望這不會在您理解它時打擾您。

這是我的問題:

服務器和客戶端似乎都可以正常工作(我一直在檢查監聽,綁定,連接功能:沒有錯誤),而且顯然可以與客戶端發送數據,但是當我嘗試從服務器讀取數據時,我收到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.cpp

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

感謝您的幫助,我了解到要閱讀/處理的內容很多,感謝您的努力!

我認為你是無效的新襪子-ID上accept不正確的括號:

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

嘗試將其更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM