Sign in to follow this  
Kukanani

Remote Server Connection + Port Forwarding Problems

Recommended Posts

Kukanani    100
Hey all, I coded a UDP server and client with C++ and I'm trying to get them to connect to each other over the global Internet. Here's what I've done: 1. Code Client:
	int portShift = 0;
	socket.open(0);
	dispatcher = new MessageDispatcher();

	//set variables for connecting to the server
	serverAddress.sin_port = htons((unsigned short)10203);
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_addr.s_addr = inet_addr("68.64.208.244");
        //...
Server:
socket.open(10203)
//...
My socket wrapper:
bool EonSocket::open(unsigned short port)
{
	//set up winsock and variables.
	if(!initializeSockets())
	{
		logString("Winsock initiation error", LOG_FATAL);
		return false;
	}
	message = EonMessage();

	//create socket and check for errors.
    handle = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
    if ( handle <= 0 )
    {
        logString("failed to create network socket", LOG_ERROR);
        return false;
    }

	//create socket address
    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;

#ifdef EXODUS_SERVER
    address.sin_port = htons(port);

	//bind socket and look for errors.
    if ( bind( handle, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 )
    {
        logString("failed to bind network socket", LOG_ERROR);
        return false;
    }
#endif

//check for errors (omitted)

	//we're done.
	logString("set up socket", LOG_INFO);
	return true;
}
As you can see, my socket binds on the server but not on the client, and in both cases everything works. 2. IP My server computer has local IP address 10.0.0.6, and my router has a static IP address, which is why I'm using inet_addr instead of gethostname in my client. 3. Port Forwarding I'm 96% sure that the port forwarding is set up correctly. I've checked the user documentation for my router and followed the instructions to the letter to forward port 10203 to my local machine 10.0.0.6 4. Firewall Every time I test this, I ensure that my firewall is completely disabled. I will try it with the firewall once I'm sure it works w/o it. 5. WireShark When I run Wireshark on my machine (I'm trying to run the client on the same machine as the server for now), I get the following output:
10.0.0.6        68.64.208.244   UDP   Source port: trp Destination port: 10203
68.64.208.244   10.0.0.6        ICMP  Destination unreachable (Port unreachable)
This makes me think that my problem actually IS related to port forwarding, but I used the PFPortChecker utility from portforward.com and it tells me that UDP port 10203 is open. What I really don't get is the Source port: trp thing because I'm not setting the port at all in the client code. By the way, all my code works if I replace my static IP address in the client code with "127.0.0.1". Anyone have any ideas? Did I just miss something stupid? Thanks in advance, Kukanani

Share this post


Link to post
Share on other sites
Kukanani    100
I hate to bump my own topic, but I had a clear post...can no one give me any advice?

Someone please at least tell me if my code is right, then I'll know it's a problem with my port forwarding.

Share this post


Link to post
Share on other sites
Evil Steve    2017
The "Source port: trp" part means that the OS decided to bind your socket to that port ("trp" is apparently port 2156) - if you don't bind a client socket, it means that the OS should bind it to "any free port", so that's nothing to worry about.

Does it work if you use "10.0.0.6" for the server address?
Does it work if you run the client or server on a remote machine (I.e. ask a friend to run the client for you and run wireshark on your PC)? It's possible that the router doesn't like getting packets sent to its external IP from an internal one.

Share this post


Link to post
Share on other sites
hplus0603    11356
It may be that your firewall doesn't support "hairpin" NAT. This means that things on the inside of NAT (sourcing from the 10.x network) don't actually have access to the external IP facade -- thus, you can't port forward traffic from the inside.

Also: If you connect to your own address on the local machine, Wireshark won't see the traffic, because Windows doesn't show local packets to WinPCap. You can often get by this by using virtualization (for example, put the client inside Windows XP Mode in Windows 7).

Share this post


Link to post
Share on other sites
Kukanani    100
Ok, connecting to 10.0.0.6 works, I tried connecting from another computer on the LAN. I tried to connect to my global IP from a different LAN computer than the one running the server, but it didn't work.

I'm beginning to think it's the hairpin NAT issue like you said, hplus. I'm trying to get someone else to connect from a remote network as soon as I can.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this