سؤال

I've started tying to learn Winsock with C++ and I'm having some problems. I started by following the Winsock Example on MSDN ( a basic client-server ). http://msdn.microsoft.com/en-us/library/ms738545(v=vs.85).aspx

When I build and run the code I get the output:

Client.exe

14 bytes sent.
Connection closed.

Server.exe

Nothing happens in the Server.exe cmd window. I have no clue what I am doing wrong. I am new to Winsock, but as far as I call tell in the Server.cpp file the accept() function never returns.

Any help / pointers are appreciated.
Thanks in advance, Tom.

EDIT The code can be found on the above MSDN Link. Scroll down on the page for the code for Client and Server.

هل كانت مفيدة؟

المحلول

Sounds like your client hasn't connected to the server. It's connected to something, but maybe not your server. Here are some things I would look at:

  1. Hard-code the server IP address in the client.

  2. The server listens only on IPv4 but the client will try both IPv4 and IPv6. Should be OK, but I recommend removing that ambiguity at this stage. In the client, where it says AF_UNSPEC change that to AF_INET.

  3. Check whether anything else is listening on port 27015 (perhaps on IPv6). Run netstat.exe to see what sockets are in use.

  4. Use plain old telnet to connect to your server. If it connects, then type anything and hit enter, and see how the server behaves.

  5. Get rid of the call to shutdown(ConnectSocket, SD_SEND) in the client. It should be OK, but it strikes me as a little unusual - I've never shut down just one direction on a socket, I always use SD_BOTH when I'm totally finished with the socket.

  6. Are you running both processes on the same machine, or is there a proxy or a firewall in the middle? A proxy might accept your client connection but fail to connect to the server, for example.

Good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top