winsock connect not working between two different wireless networks

Started by
11 comments, last by Deek880 13 years, 1 month ago
I have set up a basic chat box in which one of my computers acts as a server and the host is the ip address of that computer.
The client can connect to the host on the same computer or between my lap top and my home computer as long as they are
both connected through my verizon wirelss service. As soon as I take my lap top away from home and try to communicate with my running program on my home computer through some other wireless network, the client program can't find the server.

#include <winsock2.h>
#include <windows.h>

#pragma comment(lib,"ws2_32.lib")

#define IDC_EDIT_IN 101
#define IDC_EDIT_OUT 102
#define IDC_MAIN_BUTTON 103
#define WM_SOCKET 104

char *szServer="192.168.0.254";
int nPort=5555;

HWND hEditIn=NULL;
HWND hEditOut=NULL;
HWND ghMainWnd=0;
HINSTANCE ghAppInst = 0;

SOCKET Socket=NULL;
char szHistory[10000];
int gLength=0;
int iIndex=0;


// Set up Winsock
WSADATA WsaDat;
int nResult=WSAStartup(MAKEWORD(2,2),&WsaDat);
if(nResult!=0)
{
MessageBox(hWnd,
"Winsock initialization failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}

Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(Socket==INVALID_SOCKET)
{
MessageBox(hWnd,
"Socket creation failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}

nResult=WSAAsyncSelect(Socket,hWnd,WM_SOCKET,(FD_CLOSE|FD_READ));
if(nResult)
{
MessageBox(hWnd,
"WSAAsyncSelect failed",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}

// Resolve IP address for hostname
struct hostent *host;
if((host=gethostbyname(szServer))==NULL)
{
MessageBox(hWnd,
"Unable to resolve host name",
"Critical Error",
MB_ICONERROR);
SendMessage(hWnd,WM_DESTROY,NULL,NULL);
break;
}

// Set up our socket address structure
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(nPort);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);

connect(Socket,(LPSOCKADDR)(&SockAddr),sizeof(SockAddr));


There are other parts that are left out of course that work as I copied the program primarily from another source. Why would it work when the two programs are connected on the internet and physically close together but not when the two computers are trying to connect from a distance using different internet providers/
Advertisement
char *szServer="192.168.0.254";[/quote]

That is your LAN address! So when you try to connect from the same home network, your router does its magic and everything works. When you try to connect from a different network outside your LAN, then the program will try to access that address from its own network, which would fail (assuming there was not another server setup there that it'd connect to instead).

To enable your program to work across the internet you need to:

1. Make the server bind to an interface that can connect to the internet ( 192.*.*.* addr for example in your case) If you try to bind to 127.*.*.* then you will not be able to accept any incoming connections outside your PC due to how the loopback adapter works. You can also just bind to your physical IP if it is static.

2a. The network the server runs on has port forwarding setup to the server if it is behind a router. So you would need to forward all TCP traffic with a destination port of your program to the network address of the server.
2b. The network the server runs on needs to have its firewall configured to allow these packets if you have one enabled (either a physical one or inside a router). This is by far one of the most common errors people run into trying to figure out why something doesn't work. In addition, any firewalls on the server itself must be configured.

3. The client program connects to the physical IP of the server in either case you are outside or inside the server's network or you can connect to the network IP if you are inside the network.

Putting all of that together in an example:

Network 1 ( ip = 76.76.76.76 )
Router (ip = 192.168.1.1 subnet mask of 255.255.255.0 )
Server (network ip = 192.168.1.100)
Client 0 (network ip = 192.168.1.150)

Network 2 ( ip = 67.67.67.67 )
Router (ip = 192.168.1.1 subnet mask of 255.255.255.0 )
Client 1 (network ip = 192.168.1.100)
Client 2 (network ip = 192.168.1.101)
Client 3 (network ip = 192.168.1.102)

1. Server binds chat app to 192.168.1.100 on port 5555
2. Router is configured to forward TCP packets on port 5555 to 192.168.1.100
3. Client 0 connects to 76.76.76.76 : 5555 or 192.168.1.100 : 5555
4. Clients 1-3 connect to 76.76.76.76 : 5555

Then you should be set!
I just wanted to give a quick thank you for your answer. I'm still trying to evaluate what it means. As I look into it little bits ans pieces are starting to make sense. I'm brand new to TCP/IP and have only spent a couple of hours on winsock



char *szServer="192.168.0.254";


That is your LAN address! So when you try to connect from the same home network, your router does its magic and everything works. When you try to connect from a different network outside your LAN, then the program will try to access that address from its own network, which would fail (assuming there was not another server setup there that it'd connect to instead).

To enable your program to work across the internet you need to:

1. Make the server bind to an interface that can connect to the internet ( 192.*.*.* addr for example in your case) If you try to bind to 127.*.*.* then you will not be able to accept any incoming connections outside your PC due to how the loopback adapter works. You can also just bind to your physical IP if it is static.

2a. The network the server runs on has port forwarding setup to the server if it is behind a router. So you would need to forward all TCP traffic with a destination port of your program to the network address of the server.
2b. The network the server runs on needs to have its firewall configured to allow these packets if you have one enabled (either a physical one or inside a router). This is by far one of the most common errors people run into trying to figure out why something doesn't work. In addition, any firewalls on the server itself must be configured.

3. The client program connects to the physical IP of the server in either case you are outside or inside the server's network or you can connect to the network IP if you are inside the network.

Putting all of that together in an example:

Network 1 ( ip = 76.76.76.76 )
Router (ip = 192.168.1.1 subnet mask of 255.255.255.0 )
Server (network ip = 192.168.1.100)
Client 0 (network ip = 192.168.1.150)

Network 2 ( ip = 67.67.67.67 )
Router (ip = 192.168.1.1 subnet mask of 255.255.255.0 )
Client 1 (network ip = 192.168.1.100)
Client 2 (network ip = 192.168.1.101)
Client 3 (network ip = 192.168.1.102)

1. Server binds chat app to 192.168.1.100 on port 5555
2. Router is configured to forward TCP packets on port 5555 to 192.168.1.100
3. Client 0 connects to 76.76.76.76 : 5555 or 192.168.1.100 : 5555
4. Clients 1-3 connect to 76.76.76.76 : 5555

Then you should be set!
[/quote]

I just wanted to give a quick thank you for your answer. I'm still trying to evaluate what it means. As I look into it little bits ans pieces are starting to make sense. I'm brand new to TCP/IP and have only spent a couple of hours on winsock


You should check out the Forum FAQ, then!
enum Bool { True, False, FileNotFound };
Hello Drew:
Sorry about the flip reply earlier.
I may be confused because I think you are talking about two different servers simultaneously and I'm not sure which is which.

There's my server program which I've named WinEx8.exe and my network server, my linksys dual band wirelss n adapter, and my verizon wireless service whicn somes with its own router a "five spot".

Here's what Ipconfig shows for my home computer:

Ethernet adapter Local Area Connection 2:

Media State . . . . . . . . . . . : Media discon

Ethernet adapter Wireless Network Connection 2:

Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 192.168.0.254
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1

My LAN address would be my linksys or my verizon fivespot?

So should I use the winsock bind() function to try to bind WinEx8 to the linksys dual band n which I think is 192.168.0.254 if its the lan because 192.168.0.1 is the verizon fivespot?

With regards to 2a are you referring to the server as WinEx8.exe or the verizon service?

With regards to (3) which would be the physical IP of the server?

Thanks again.


char *szServer="192.168.0.254";


That is your LAN address! So when you try to connect from the same home network, your router does its magic and everything works. When you try to connect from a different network outside your LAN, then the program will try to access that address from its own network, which would fail (assuming there was not another server setup there that it'd connect to instead).

To enable your program to work across the internet you need to:

1. Make the server bind to an interface that can connect to the internet ( 192.*.*.* addr for example in your case) If you try to bind to 127.*.*.* then you will not be able to accept any incoming connections outside your PC due to how the loopback adapter works. You can also just bind to your physical IP if it is static.

2a. The network the server runs on has port forwarding setup to the server if it is behind a router. So you would need to forward all TCP traffic with a destination port of your program to the network address of the server.
2b. The network the server runs on needs to have its firewall configured to allow these packets if you have one enabled (either a physical one or inside a router). This is by far one of the most common errors people run into trying to figure out why something doesn't work. In addition, any firewalls on the server itself must be configured.

3. The client program connects to the physical IP of the server in either case you are outside or inside the server's network or you can connect to the network IP if you are inside the network.

Putting all of that together in an example:

Network 1 ( ip = 76.76.76.76 )
Router (ip = 192.168.1.1 subnet mask of 255.255.255.0 )
Server (network ip = 192.168.1.100)
Client 0 (network ip = 192.168.1.150)

Network 2 ( ip = 67.67.67.67 )
Router (ip = 192.168.1.1 subnet mask of 255.255.255.0 )
Client 1 (network ip = 192.168.1.100)
Client 2 (network ip = 192.168.1.101)
Client 3 (network ip = 192.168.1.102)

1. Server binds chat app to 192.168.1.100 on port 5555
2. Router is configured to forward TCP packets on port 5555 to 192.168.1.100
3. Client 0 connects to 76.76.76.76 : 5555 or 192.168.1.100 : 5555
4. Clients 1-3 connect to 76.76.76.76 : 5555

Then you should be set!
[/quote]
On the PC that you wish to run your server application, WinEx8.exe, run ipconfig to get the "IP Address" of the interface that you are using to connect to the internet. You can ignore the "Default Gateway" address because that is your router/modem. Also, you might be able to go to IP Address Lookup and click "get my LAN address" to confirm, but I'm not sure if there are any cases where it breaks, but it can be used as a backup, maybe.

Once you have your LAN address, 192.168.X.X (note, this should not be your Default Gateway address), you will bind to that address in your server application. For example, if the PC you listed was the server PC, that address would be "192.168.0.254".

Now, referring back to the IP Address Lookup page linked previously, you can obtain your real IP address there. That is your "WAN" address. That address is what your client application will connect to, your physical IP address.

Next, you need to make sure your router/modem is configured to forward TCP packets to your server PC. You will need to setup port forwarding to the LAN address of the server PC. Usually, you type in an application name, the ports, the network protocol, and then the IP address. The IP address is simply your LAN address of the server PC.

Finally, your client applications can connect through their own networks to your server PC using your WAN address if they are on separate networks or your LAN address if they are on the same address!

Now for an example. This is my ipconfig:
ss1bt.th.png

I connect to the internet through my router via ethernet, so I look for that interface. My LAN address is 192.168.1.135. I would bind my server application to that address on the desired port(s).

Next, I configure my router (which is 192.168.1.1 on my network):
ss2hu.th.png

I'd type in a name for the application, then change the ports to the ones needed for my program. I use TCP protocol since that is what the application uses and finally I set the address to forward to. This should be my LAN address, so I use 192.168.1.135.

Finally, I can connect to the server application with the client applications by using the "WAN" address of the server, the physical IP. I'm not going to paste mine, but you can find yours as mentioned from a website or your router.

So let's say I setup a server and you were wanting to connect with a client from your own network. I'd have to give you my physical IP address and the port to connect to. You would then be able to connect. Let's say you were on my same network in my house in a different room. You could connect the same way or you could just connect using my LAN address, which would be 192.168.1.135. So on the same network, you can use either address. On different networks, you have to use the WAN address only.

I hope that makes more sense now.
Hello Drew:

I got into my verizon 5 spot wireless router and it does have a port forwarding feature.

I assigned my computer a static IP address through windows xp of 192.168.0.13. I used that as the IP/Address for portforwarding with the port of the program (5555). I still can't connect from any network other than Verizon, which is what I expected. The wireless service obviously needs any connecting computers to be part of my network to distinguish between thousands of other computers that have choosen the same static IP.

The physical address of my computer is not a TCP/AP address that is it something like xx-44-gg-22-hh etc. What else can you suggest.

Thanks



On the PC that you wish to run your server application, WinEx8.exe, run ipconfig to get the "IP Address" of the interface that you are using to connect to the internet. You can ignore the "Default Gateway" address because that is your router/modem. Also, you might be able to go to IP Address Lookup and click "get my LAN address" to confirm, but I'm not sure if there are any cases where it breaks, but it can be used as a backup, maybe.

Once you have your LAN address, 192.168.X.X (note, this should not be your Default Gateway address), you will bind to that address in your server application. For example, if the PC you listed was the server PC, that address would be "192.168.0.254".

Now, referring back to the IP Address Lookup page linked previously, you can obtain your real IP address there. That is your "WAN" address. That address is what your client application will connect to, your physical IP address.

Next, you need to make sure your router/modem is configured to forward TCP packets to your server PC. You will need to setup port forwarding to the LAN address of the server PC. Usually, you type in an application name, the ports, the network protocol, and then the IP address. The IP address is simply your LAN address of the server PC.

Finally, your client applications can connect through their own networks to your server PC using your WAN address if they are on separate networks or your LAN address if they are on the same address!

Now for an example. This is my ipconfig:
ss1bt.th.png

I connect to the internet through my router via ethernet, so I look for that interface. My LAN address is 192.168.1.135. I would bind my server application to that address on the desired port(s).

Next, I configure my router (which is 192.168.1.1 on my network):
ss2hu.th.png

I'd type in a name for the application, then change the ports to the ones needed for my program. I use TCP protocol since that is what the application uses and finally I set the address to forward to. This should be my LAN address, so I use 192.168.1.135.

Finally, I can connect to the server application with the client applications by using the "WAN" address of the server, the physical IP. I'm not going to paste mine, but you can find yours as mentioned from a website or your router.

So let's say I setup a server and you were wanting to connect with a client from your own network. I'd have to give you my physical IP address and the port to connect to. You would then be able to connect. Let's say you were on my same network in my house in a different room. You could connect the same way or you could just connect using my LAN address, which would be 192.168.1.135. So on the same network, you can use either address. On different networks, you have to use the WAN address only.

I hope that makes more sense now.

The physical address of my computer is not a TCP/AP address that is it something like xx-44-gg-22-hh etc. What else can you suggest.


If you go to http://www.whatismyip.com/ what does it say your publicly accessible IP address is?
enum Bool { True, False, FileNotFound };
Hello hplus0603:

Today that website says my IP address is 64.12.116.78

Yesterday it said my IP address was 64.12.116.9 and on a separate try 205.188.116.78

Deek

Today that website says my IP address is 64.12.116.78

Yesterday it said my IP address was 64.12.116.9 and on a separate try 205.188.116.78


That IP address is what you need to connect to from the outside. For further information, check out the "how can people find my server" question answers in the FAQ!
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement