我目前正在尝试实现一个类来创建和使用 TCP 连接。
(我在 C++ 中很新,所以我可能忘记了一些明显的东西,即使我一直在认真检查我的代码)
请注意,稍后我将实现第二个类来处理新连接,因此代码的某些部分是不完整/排列的,我希望在尝试理解它时不会打扰您。
这是我的问题:
服务器和客户端似乎都工作正常(我一直在检查我的监听、绑定、连接功能:没有错误)并且我显然能够与客户端发送数据但是当我尝试从服务器读取它时,我收到 WSA 错误 10038(这意味着我正在无效套接字上执行 recv())。我已经查了几天了,似乎找不到我的错误。除了代码中的测试外,我还使用“netstat -an -p tcp”检查连接状态,一切正常,但服务器无法接收数据。
注意:我使用的是 Visual Studio 6(是的,它很旧,但它是强制性的,所以......)和 winsock,而不是 winsock2。
如果我忘记了什么就问,我会尽量做到完整,但忘记某些东西并不是不可能的。
这是我的源代码,我试图将其简化,但我担心错误可能几乎无处不在:
CmTcpTransport.cpp
/**
Initializes the TCP connection (socket) in CLIENT or SERVER mode.
It does not connect yet.
@param strIPAddress: IP Address 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: Retrieving 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;
}
}
服务器主.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
感谢您的帮助,我意识到这需要阅读/处理很多内容,我感谢您的努力!