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.






