Jump to content

  • Log In with Google      Sign In   
  • Create Account


[Winsock] Connection only works to loopback device


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 d k h   Members   -  Reputation: 435

Like
0Likes
Like

Posted 29 June 2012 - 09:32 AM

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!

Edited by d k h, 29 June 2012 - 10:25 AM.


Sponsor:

#2 rip-off   Moderators   -  Reputation: 8113

Like
0Likes
Like

Posted 29 June 2012 - 10:00 AM

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.

#3 d k h   Members   -  Reputation: 435

Like
0Likes
Like

Posted 29 June 2012 - 10:02 AM

Thanks a ton, man! Sorted out very quickly! Using the copy link trick now! Feel free to delete this post as well!

Edited by d k h, 29 June 2012 - 10:10 AM.


#4 hplus0603   Moderators   -  Reputation: 5162

Like
0Likes
Like

Posted 29 June 2012 - 11:32 AM

WSAECONNREFUSED


Sounds like a firewall/port forwarding issue.
enum Bool { True, False, FileNotFound };

#5 d k h   Members   -  Reputation: 435

Like
0Likes
Like

Posted 29 June 2012 - 11:50 AM

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.

#6 Rasterman   Members   -  Reputation: 206

Like
0Likes
Like

Posted 29 June 2012 - 12:58 PM

I suggest using a networking library. Read the networking FAQ on this forum.

#7 hplus0603   Moderators   -  Reputation: 5162

Like
0Likes
Like

Posted 29 June 2012 - 07:59 PM

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 };

#8 TMKCodes   Members   -  Reputation: 271

Like
0Likes
Like

Posted 19 July 2012 - 06:52 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS