basic Socket connection using tutorial...

Hello, I'm following the tutorial found on:

http://www.codeproject.com/Articles/13071/Programming-Windows-TCP-Sockets-in-C-for-the-Begin

I have compiled and can run the program fine and can chat within my own computer. I can also create a connection with a 2nd computer i have in my room over the internet without any issue (connecting to a unique IP). When I've tried to do the same with a friend's computer a few miles down the road we get all of the errors that relate to an unresolved IP. It doesn't matter if I try to be the host or he does. We've 'allowed' the connection to pass through the virus protection and firewalls - as far as we know...

Any ideas ?
Thanks !!!
As always, it's a routing problem.

You probably have an address like 192.168.0.x and your friend probably has the same. These are local addresses and your 192.168.0.x address is on a different network than your friends.

The line coming into your home will have a unique address on the world wide internet. But inside each home, you'll have local 192.168 addresses. That's the way domestic routers are configured.

You need to log onto your router to see what your home's actual address is. Alternatively a page like this will tell you.
http://ip1.dynupdate.no-ip.com/

You would then need to open the network port on the router that's hosting the server side. You need to tell the router the port and the host computer address (the 192.168 address) that has the server side app.

The client would specify the external world wide internet address (to the router) rather than the internal 192.168 address.

It's all terribly complicated unless you understand why things are that way.
Hey! Thank you for your answer and you are completely right :)

I figured I would have a follow up question but I'm still trying to grasp how to turn these concepts into code.

OK..so, it is the client who is connecting to the server. To do that, the client has to first connect to the server's world wide internet address - then the server's local 'home' address of 192.168... ? Is this right ? Because I attempted to connect directly to the world wide web address of the server which was met with failure.

Thanks !

This is part of what I have when the client is attempting to connect to the server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket
	if(s == INVALID_SOCKET)
	{
		return 0;
	}

	target.sin_family = AF_INET;		//address family internet
	target.sin_port = htons(PortNo);	//set server's port number
	target.sin_addr.s_addr = hostname;	//set server's IP

	//Try connecting...
	if(connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)		
	{	//error
		return 0;
	}
	WSAAsyncSelect(s, hwnd, 1045, FD_READ | FD_CONNECT | FD_CLOSE);
A couple ideas:

1. If the server's provider is your regular internet provider, they may block incoming connections to prevent people from running servers on a residential line.
2. The port you're attempting to connect to is closed/blocked, on the server or the provider.
Ahhh ok it's starting to make sense now thanks ! Then, the code should be fine the way it is - even without distinguishing between the multiple 192.168 IPs at my mini server's residential location...

So much for my idea of having a little homemade chat messenger to be used among a few friends :(
I'd recommend trying to run some kind of served up application (not made by you), just to test it out. There may be some already available messengers you can run in server mode then attempt to connect from the outside.

This is just to verify that it is network block, as opposed to a coding issue.
OK..so, it is the client who is connecting to the server.
Right.

To do that, the client has to first connect to the server's world wide internet address
Right again.

- then the server's local 'home' address of 192.168... ?
Wrong. There is already a device on your Internet address, it's your home router. You need to tell the router to:
1. open the port that your server is using
2. redirect traffic to that port to your server, which will have a 192.168 address

An internet is an network of inter-connected networks. It's the alternative to creating one large network. IP is "the" internet protocol. It's responsible for routing packets from one address to the next.

If the addresses are on the same network, then standard LAN technology makes routine easy. Signals can be broadcast on a LAN and all that sort of stuff. The problem comes in getting packets off one network to the next. IP uses a Router as network endpoint. That's where data enters the network from another network or leaves the network. There can be more than one Router on the network.

In your case, the route from your client box is from your host (that has a 192.168.x.x address) to your default router which will have address like 192.168.0.1 internally and the Interet address on the other side. That address is on your ISPs network and they'll route the packet to your friends default router on their ISPs network. From there that router routes the packet to server.

The steps I've given configure the router to route packets for a given port to one host that acts as a server for that service.

You can see the route with traceroute (or tracert on Windows).

If your ISP blocks some ports, you can always use another port, or another ISP.

[Edit...]
This:
 
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket 

should be:
 
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //create socket 

AF_INET is the address family. PF_INET is the protocol family. socket() takes the protocol details. struct sockaddr_in takes the address details.

It works because the constants have the same values. Many examples show the AF_INET, but we should try to do the right thing.
Last edited on
I've found a simple chat server and it was also unable to connect to my friend (or vice versa).

I've tried tracert on my friend's IP and the request goes through 5 hops before timing out on each following request. The last successful one was in the form:
5 56ms 43ms 63ms te-4-0-0-81.69w.ba13.etc

I am assuming that this is probably my ISP blocking the trace and thus my attempts at a connection ? So i shouldn't worry about my firewall...?

kbw:
If your ISP blocks some ports, you can always use another port, or another ISP.

Are you saying that the ISPs (for typical residential areas) in general, will leave some ports open so it's just a matter of finding those open ports (do i have to disable my firewall for this ?)?

Thanks again for all your help :)



2. redirect traffic to that port to your server, which will have a 192.168 address


Should I be looking into something like this then?
http://www.codeproject.com/Articles/12039/Server-Traffic-Redirector
Last edited on
It was roberts who suggested the ISP might block ports. I have no experience of that.

Not all routers allow routing info to be displayed.

I've found a simple chat server and it was also unable to connect to my friend (or vice versa).
Like I said, it's a routing problem.

Should I be looking into something like this then?
http://www.codeproject.com/Articles/12039/Server-Traffic-Redirector
No. I've given you all the information you need. Have you opened\redirected the port on the router?

Are you saying that the ISPs (for typical residential areas) in general, will leave some ports open so it's just a matter of finding those open ports (do i have to disable my firewall for this ?)?


I believe the ISO standard defines up to 65,535 ports, with the first 1024 reserved. Some ports might be overlooked, but most good security types will close everything and open only what is needed.
OK kbw thanks. It took a lot of reading to see that u gave me the info I need. Unfortunately, I'm connected to a public router so I will not be able to change router settings for a couple of months to test what u said. I hope that will resolve the problem and I'm glad to say that I (kinda) understand a little more about networking now ;)

Thanks roberts too !

I believe this will solve my problem thanks again.
I'm connected to a public router
You'll need an external address that both machines will see. You then need something like that Traffic-Redirector thing you posted above to route the traffic for you to and from the server.
Topic archived. No new replies allowed.