Advertisement

Mixing WSASend (Client) with recv (Server)

Started by April 29, 2015 02:38 PM
6 comments, last by d3vil401 9 years, 6 months ago

I'm using my server to use recv() but the client uses instead the WSA* functions.

So my client is sending a packet with WSASend() but obv. my server is not able to receive it since it uses recv().

Is there a way to let the server read anyway the packet even using different functions?

That doesn't make sense, should work. I would guess your problem lies elsewhere, like TCP delay or something. Is the connection properly detected on both sides, and does WSASend return success?

Advertisement
So my client is sending a packet with WSASend() but obv. my server is not able to receive it since it uses recv().

That's not the source of your problem. You don't have to use the same sockets API on both ends of the connection. The whole internet would stop working if that were the case.

It's more likely that you've configured one or both sides of the connection incorrectly, such that you're not actually sending where you are listening, or something. Or that an error you aren't checking for has occurred on one side or the other of the connection.

So my client is sending a packet with WSASend() but obv. my server is not able to receive it since it uses recv().


As others said, that's not the problem. Both the send() and WSASend() functions have the role of "enqueue a network transmission formulated according to the TCP or UDP standard" (depending on the socket.)
On the other end of the wire, the actual network packet (signal on the wire) will look the same no matter which function was used to make it.
The receiving end then uses whatever function is appropriate for "dequeue a received network transmission decoded according to the TCP or UDP standard." This could be recv() or WSARecv() or BeginReceive() or one of a host of other APIs.

What matters is the actual standard format used by the network, and the great benefit of the internet is that all participants on the internet agree on what that packet format looks like. Else nothing would work!
enum Bool { True, False, FileNotFound };

Thanks for the information.

I will take a look for further explanation for my problem, whenever i need help i will post here.

So this is my problem:

I still have this problem about client not connecting to the server and server not receiving anything.


WORD wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &wsaData);
 [...]
printf("Starting %s Login Core on %s:%d.\n", CM->getString(CM->SERVER_NAME).c_str(), CM->getString(CM->SERVER_IP).c_str(), CM->getNumber(CM->SERVER_PORT));
InitializeCriticalSection(&gCriticalSection);
 
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
err = ioctlsocket(s, FIONBIO, &iMode);
 
sin.sin_family = AF_INET;
sin.sin_port = CM->getNumber(CM->SERVER_PORT);
sin.sin_addr.s_addr = inet_addr(CM->getString(CM->SERVER_IP).c_str());
 
switch (sin.sin_addr.s_addr)
{
case INADDR_NONE:
printf("[!] Input IP Address is NONE!\n");
break;
 
case INADDR_ANY:
printf("[!] Input IP Address is ANY!\n");
break;
}
 
if (err = ::bind(s, (SOCKADDR*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("[!] Binding error (%ld).\n", WSAGetLastError());
return 1;
}
 
err = listen(s, SOMAXCONN);
if (err == SOCKET_ERROR)
{
printf("[!] Listening error (%ld).\n", WSAGetLastError());
return 1;
}
 
int fromlen;
while (true)
{
 
fromlen = sizeof(from);
sock_accept = accept(s, (struct sockaddr*)&from, &fromlen);
if (sock_accept != INVALID_SOCKET)
{
THREAD_STRUCT* th_struct = new THREAD_STRUCT;
th_struct->Socket = sock_accept;
th_struct->MySQLHandler = DB;
th_struct->ChannelsInfo = ChannelsInfo;
th_struct->ThreadHandle = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)WinSockThread, th_struct, NULL, &th_struct->SocketRead);
}
 
Sleep(100);
}
WSACleanup();
}

The server is local, so the IP is correct (127.0.0.1) for the client AND the server.

Client says "unable to connect", my server does not even know there's an incoming connection ( what the hell? ).

I tryed to connect to the server trough putty, raw socket on my server IP:Port and the result is "Connection Refused".

Advertisement
First, you should debug this with Wireshark. Learning how to use Wireshark is a really important skill for network developers.

Second, the bug is likely in this line:
sin.sin_port = CM->getNumber(CM->SERVER_PORT);
Wireshark will print a different port number than what you think you're using, because you're not using htons().
enum Bool { True, False, FileNotFound };

First, you should debug this with Wireshark. Learning how to use Wireshark is a really important skill for network developers.

Second, the bug is likely in this line:


sin.sin_port = CM->getNumber(CM->SERVER_PORT);
Wireshark will print a different port number than what you think you're using, because you're not using htons().

http://puu.sh/hyT4f/fa79bf5344.png

Did it already, still having the same problem.

Ok i fixed it.

I'm sorry, it was not related to my server code nor the client itself but about networking problems.

This topic is closed to new replies.

Advertisement