[Winsock] Connection only works to loopback device

Started by
6 comments, last by ToniKorpela 11 years, 9 months ago
Hi there,

I'm just starting out with netcoding, using Winsock 2.2 at the moment. My goal is to set up a connection! My code works fine when I run two instances of this console program, server mode on one, client on the other and then have the client connect the loopback device (127.0.0.1). It then says it's connected fine and I can exchange data using send () and recv ()!

However, when I'm trying this with an actual person (Germany to Australia), I get fairly weird results: when he's running server-mode and I'm connecting to his IP (ping [albeit 2s latency] and tracert work fine), I get "Failed to connect to server (error 10061)" which resolves to WSAECONNREFUSED. When we switch roles and he connects to my or, and this is where it gets weird, any other random IP, he always gets "Connected to <insert IP>..." instantly!

[source lang="cpp"]

bool WinsockStart ( )
{
WSADATA wsaData;

if ( WSAStartup ( MAKEWORD ( 2, 2 ), &wsaData ) != 0 )
{
cout < < "Failed to initialize Winsock..." < < endl;
return false;
}

cout < < "Winsock initialized..." < < endl;

return true;
}

bool CreateConnectionAsClient ( )
{
SOCKADDR_IN addr;

cout < < "Running client..." < < endl;

SOCKET s = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( s == INVALID_SOCKET )
{
cout < < "Failed to create socket (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Socket created..." < < endl;

cout < < "Please enter the ip address of the server you wish to connect to: ";
char *ipaddress = new char;
cin < < ipaddress;

memset ( &addr, 0, sizeof ( SOCKADDR_IN ) );
addr.sin_family = AF_INET;
addr.sin_port = htons ( 27015 );
addr.sin_addr.s_addr = inet_addr ( ipaddress );

if ( connect ( s, ( SOCKADDR* ) &addr, sizeof ( SOCKADDR ) ) == SOCKET_ERROR )
{
cout < < "Failed to connect to server " < < ipaddress < < " (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Connected to server " < < ipaddress < < "..." < < endl;

return true;
}

bool CreateConnectionAsServer ( )
{
SOCKADDR_IN addr;

cout < < "Running server..." < < endl;

SOCKET acceptSocket = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( acceptSocket == INVALID_SOCKET )
{
cout < < "Failed to create socket (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Socket created..." < < endl;

memset ( &addr, 0, sizeof ( SOCKADDR_IN ) );
addr.sin_family = AF_INET;
addr.sin_port = htons ( 27015 );
addr.sin_addr.s_addr = ADDR_ANY;

if ( bind ( acceptSocket, ( SOCKADDR* ) &addr, sizeof ( SOCKADDR_IN ) ) == SOCKET_ERROR )
{
cout < < "Failed to bind socket (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Socket binded..." < < endl;

if ( listen ( acceptSocket, 10 ) == SOCKET_ERROR )
{
cout < < "Failed to listen for connections (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "Listening..." < < endl;

SOCKET connectedSocket = accept ( acceptSocket, NULL, NULL );

if ( connectedSocket == INVALID_SOCKET )
{
cout < < "Failed to accept incoming connection (error " < < WSAGetLastError ( ) < < ")..." < < endl;
return false;
}

cout < < "New connection accepted..." < < endl;

return true;
}
[/source]


I'm simply not sure in what way I could debug or error check the code more and the results just don't make any sense to me. Since I'm new to this, I would really appreciate if some of the more experienced netcoders around here could overlook these three simple functions and give me a couple pointers!

Thanks ahead of time! If there's any other info or anything you lot need, let me know and I'll do my best to provide it!
Advertisement
If the forum eats another post, you can copy and paste the "edit" link into your browser address bar - it will still work even though clicking on it doesn't.
Thanks a ton, man! Sorted out very quickly! Using the copy link trick now! Feel free to delete this post as well!
WSAECONNREFUSED


Sounds like a firewall/port forwarding issue.
enum Bool { True, False, FileNotFound };
Okay, I've been looking up various articles on this matter (ie. Winsock firewall issue, Winsock port forwarding etc.), it all gets really technical very fast.

I am behind a DSL router that I, unfortunately, don't have access to (shared flat). I am using Windows Vista's build-in firewall. I have allowed my app to go through, of course, but just now, in the security center, I have also added the port my app uses. I will inform my test partner to do the same with whatever firewall he's using. I will be back with updates!

Ultimately, I would really like to do whatever I can with the Winsock API to be able to set up a connection without the user having to forward ports or anything the like. How do the big multiplayer games do this anyways?

Thanks for the help so far.
I suggest using a networking library. Read the networking FAQ on this forum.
The WinSock API won't let you re-configure your DSL firewall/router, which I bet is where the problem is.
Your only option in that case is to hope that the router supports NAT punch-through, and use an external server for NAT introduction. There are links in the FAQ about this.
enum Bool { True, False, FileNotFound };
The problem is not in your firewall, but on his. When he is running the server his ports are closed or not correctly forwarded which means that you can not connect to his computer where he runs the server software.

tracert works because it keeps sending packets and starts raising TTL from 1 to 30 or more. This means that the first packet comes back from the first router and the second packet from the second router and the third packet from the third router and so on. Most likely will not even reach your friends PC itself. Ping tools then again send ICMP packet which most routers either block or reply to it and ICMP does not require TCP or UDP ports, because it works directly on top of IP.

This topic is closed to new replies.

Advertisement