Hi I have been trying for a while to make a win32 app client be able to talk to a server app that I put on my friends computer. I am not 100% sure if the problem is caused by the fact that I have two routers which may interfere with my IP address. The following code works on my LAN and with the loop back address 127.0.0.1.

I am gonna put up the parts that have to do with winsock since it is using SDL/C++/winsock

For the client

bool ConnectToHost( int PortNo, char* IPAddress )
{
	WSADATA wsadata;
	int error = WSAStartup( 0x0202, &wsadata );
	
	if( error )
	{
		return false;
	}
	
	if( wsadata.wVersion != 0x0202 )
	{
		WSACleanup();
		return false;
	}
	SOCKADDR_IN target;
	
	target.sin_family = AF_INET;
	target.sin_port = htons( PortNo );
	target.sin_addr.s_addr = inet_addr( IPAddress );
	
	//s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
	s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
	
	if( s == INVALID_SOCKET )
	{
		return false;
	}
	
	if( connect( s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR )
	{
		return false;
	}
	
	WSAAsyncSelect (s, hwnd, 1045, FD_READ | FD_CONNECT | FD_CLOSE);
	return true;
}

and for the server

bool ListenOnPort( int PortNo )
{
	WSADATA w;
	
	int error = WSAStartup ( 0x0202, &w );
	
	if( error )
	{
		return false;
	}
	
	if( w.wVersion != 0x0202 )
	{
		WSACleanup();
		return false;
	}
	
	SOCKADDR_IN addr;
	
	addr.sin_family = AF_INET;
	addr.sin_port = htons( PortNo );
	addr.sin_addr.s_addr = htonl( INADDR_ANY );
	
	//s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
	s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
	
	if( s == INVALID_SOCKET )
	{
		return false;
	}
	
	if( bind( s, (LPSOCKADDR)&addr, sizeof(addr)) == SOCKET_ERROR )
	{
		return false;
	}
	
	listen( s, SOMAXCONN  );
	WSAAsyncSelect(s, hwnd, 1045, FD_READ | FD_CONNECT | FD_CLOSE | FD_ACCEPT);
	return true;
}

for my message handler

(not sure why the formatting is all messed up)

switch(message)
    {
    //Winsock related message...
    case 1045:
        switch (lParam)
        {
            case FD_CONNECT: //Connected OK
            	break;
            	
            case FD_CLOSE: //Lost connection
				SDL_WM_SetCaption( "SPONG - LISTENER (Connection Lost)", NULL );
            	CloseConnection();
            	ListenOnPort(6221);
           		break;
            
            case FD_READ:
				{
	                char buffer[80];
	                string strbuff;
	                memset(buffer, 0, sizeof(buffer));
	                recvfrom(s, buffer, sizeof(buffer)-1, 0, (SOCKADDR *)&from, &fromlen);
	                strbuff = buffer;
	            	if( strbuff.length() > 0 )
	            	{
						addMessage(strbuff);
					}
				}
            	break;
            
            case FD_ACCEPT:
				{
					SOCKET TempSock = accept(s, (SOCKADDR *)&from, &fromlen);
					s = TempSock;
				}
				break;
		}
	break;

at the start of the program I have
ListenOnPort(6221);
for the server and
ConnectToHost(6221, "MY.FRIENDS.IP.ADDRESS");
as I said above if I use "127.0.0.1" or my IP on LAN it works perfect however I cannot connect or be connected to from outside of my network.

Any help would be great. If you want are willing to really help me out I the full source for both the client and server are included however you might have to set up the links in to the SDL lib, just ask and I'll be happy to send it.

Again thanks.

Recommended Answers

All 7 Replies

I haven't looked at your code but when you connected to your friends computer. Were they at your location or somewhere else.
If somewhere else is your friends computer acting as the host on a static or dynamic IP. If dynamic then you're out of luck. If static then you merely need to indicate to your client application to connect to their static IP. The router on his end must be mapped to pass the port being addressed on that static IP and route it to his computer on the local network side of the router.

I tried it when he was at his house and I was at mine. He went to what is my ip.com to get his address for me to try to connect. He also opened up the port on his end.

IP Providers for dynamic IP's typically have blocking software to prevent their customers from becoming a tree branch in a peer-to-peer network. Which means that although you may detect the real IP that's been rented for that connection, you won't be able to connect to computer. I'm guessing that you'll connect fine when both of you are on your local network, but not outside it. When on both sides of the firewall, one of you has to be on a static IP, and have an open port on the Host side to allow access!
If you go into security on the host, you should be able to see the connections attempt if they're getting through whether your host software works or not.

I'm assuming you've already run the Host and Client on the same computer for testing purposes.

I'm guessing you're already familiar with running...
netstat -an
...from the command shell?
You also mention that the port was open. If using a router like I think, you had said, did he connect the local IP of that computer to that port # so that the connection gets routed to his computer on that local network? Even though he's on a router, when working with network code, you should change his automatic configuration to manual and set a hardcoded local IP, out of the routers's assignment range so that it doesn't move around.
192.168.1.101 ... N is first come first served!

yeah I've already tried on the same computer and connecting to 127.0.0.1 or connecting to my brothers computer on my LAN 192.168.5.123. One of the main problems I think I have is that all the computers on my LAN get the same IP address from whatismyip.com and this has only started happening since we installed a 2nd router and a server at my house. Outsiders are able to connect to the actual server that we set up. Another problem might be the fact that this server is our DHCP. =(

192.168.5.X (5)

typically its .1. or sometimes .0.

So you must have a static IP since people can connect to you.

Or again, outside connections are blocked by his provider.
That's the best I can do for you! If you had host-2-client connection issues in C/C++ I'd be able to help you better. But sounds like an infrastructure problem.

As to your routers.
I'm assuming you turned off automatic IP assignments on one of them? AND you re-assigned it to a different IP address?
192.168.1.1
192.168.2.1

something like that!

Yes, my routers are set up so the 1st one is 192.168.1.1 and it is in DMZ mode so all traffic goes to the second wireless router and that is 192.168.5.1. Then our server and all computers are plugged into the 5.1 router and the computers obtain their assigned ip addresses from the server. You could be right that nothing works because of the setup but do you think that if I moved this "server" program to the server that outsiders could connect to it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.