I try to transform a program from windows to ubuntu using Wine.
The program is quite simple, just accept a new connection and
at the same time close the previous one.
I find that the program works well in Windows but not in Ubuntu.
The program work well when communicate with the first connection.
But if there is a new connection, I will close the first connection and then
receive "SOCKET_ERROR" from the recv() function.
Code: Select all
waiting for connection
accepted '127.0.0.1'
remote computer is closed
accepted '127.0.0.1'
recv failed: FormatMessage() unable to fetch error text: 317
accepted '127.0.0.1'
recv failed: FormatMessage() unable to fetch error text: 317
....
Code: Select all
DWORD WINAPI Interactive(LPVOID param) {
SOCKET remote_socket = *((SOCKET*)param);
char buf[PKG_SIZE];
char net_buffer[PKG_SIZE<<1];
int net_buffer_size = 0;
while(true) {
int bytes = recv(remote_socket, buf, PKG_SIZE, 0);
if(bytes == SOCKET_ERROR) { // !!!!! always SOCKET_ERROR except the first connection
ColorWrite(COLOR_ERROR, "recv failed: %s\n", GetLastErrorDetails());
closesocket(remote_socket);
return 0;
} else if(bytes == 0) {
ColorWrite(COLOR_WARNING, "remote computer is closed\n");
closesocket(remote_socket);
return 0;
} else {
ParsePackage(buf, bytes, net_buffer, net_buffer_size);
}
}
closesocket(remote_socket);
return 0;
}
int main() {
SOCKET socket_listen;
SOCKADDR_IN sock_addr;
WinSockManager::GetInst()->NewTcpServer(&socket_listen, &sock_addr, REMOTE_PORT, BACKLOG, true);
ColorWrite(COLOR_STATUS, "waiting for connection\n");
SOCKET remote_socket = accept(socket_listen, (SOCKADDR*)&sock_addr, &SOCKADDR_LEN);
if(remote_socket == INVALID_SOCKET) {
FatalMessageBox("accept failed. %s\n", GetLastErrorDetails());
} else {
ColorWrite(COLOR_OK, "accepted '%s'\n", inet_ntoa(sock_addr.sin_addr));
}
while(true) {
SOCKET tmp = remote_socket;
HANDLE h_thread = CreateThread(NULL, 0, Interactive, (LPVOID)&tmp, 0, NULL);
if(h_thread == NULL) {
ColorWrite(COLOR_ERROR, "CreateThread failed: %s\n", GetLastErrorDetails());
break;
}
remote_socket = accept(socket_listen, (SOCKADDR*)&sock_addr, &SOCKADDR_LEN);
closesocket(tmp);
if(remote_socket == INVALID_SOCKET) {
closesocket(socket_listen);
FatalMessageBox("accept failed. %s\n", GetLastErrorDetails());
} else {
ColorWrite(COLOR_OK, "accepted '%s'\n", inet_ntoa(sock_addr.sin_addr));
}
}
return 0;
}