CAsyncSocket Server - unable to connect using public IP

Posted on 2008-11-15
Medium Priority
Last Modified: 2013-11-20
I created a server using CAsyncSocket. I am starting the server using the following code:

(m_serverSocket is a class derived from CAsyncSocket)

then I am connecting to it using CSocket using the following code:

m_clientSocket.Connect("", 111);
(m_clientSocket is a class derived from CSocket)

It works fine for localhost, but I cant connect to it using my public IP.
The error I am getting is 10060, which is a timeout. It seems my server is never reached from outside.

First thing that came to my mind is the firewall, so I disabled both the windows and the router firewalls with no luck. Also, I am sure of my public IP since I get it directly from my router's interface and I have tried various port numbers, like 3111, 7000, 45111..

Thanks in advance..
Question by:Invader
  • 4
  • 2
LVL 86

Expert Comment

ID: 22968420
From where is your client trying to connect? If that machine is in your network behind the router, that should work. If it is from outside the router, that will most likely fail.

Author Comment

ID: 22968439
Both the server and client are on the same machine. As a matter of fact the code for both is running from the same application. Its just a sample program to test sockets.

Localhost works, since the connection is going from my PC -> my PC.
When I use my public IP though the route should be my PC-> my router -> internet -> my router -> my PC.

You say if its outside my router it will most likely fail. Why ? Isnt supposed my server to be accessible from any computer anywhere provided my firewall allows the incoming connection to that port ?
LVL 86

Expert Comment

ID: 22968466
Is the server bound to the same IP address the client is trying to connect to?

As for teh "outside" part, the IP addresses you are using behind your router are plain invalid on the internet - it's mostly a 192.168.x.y address, which most networks use.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 22968552
I am not sure I understand what do u mean by 'bound'.

I execute server.Create(port) and I expect the server to be available for all IP addresses my computer has on that 'port'. Apparently I am wrong for my public IP.

client.Connect("192.168.x.y, port) works..
client.Connect("202.87.x.y, port) , which is my public IP, doesnt..

I understand my PC doesnt really have my public IP. My router does. Shouldnt my router though redirect the incoming connection to my pc at the same port. I also tried to create a NAT rule on my router to translate incoming connections of 'port' to 'port' for local. Still no luck..

Also, I run a netstat from dos prompt while attempting the connection. What I see being added is this:

Protocol  |  Local Address      |  Foreign Address    |  State
TCP         |  mypc: some port   |  dsldevice.lan: port  | SYN_SENT

The port on the local address is some random port assigned by my computer, the port on the foreign address is the one I am trying to connect to and consequently the one used by my server. It remains in SYN_SENT state until it times out.


Author Comment

ID: 22969020
Anyway, maybe I am overcomplicating things. To make it simple:

I want to start a server that is accessible from anywhere, not only from inside my local network.

The question is, do I need to program it differently or since I can connect to it from inside my local network I just need to configure my router properly to gain outside access ?

Accepted Solution

Invader earned 0 total points
ID: 22975947
I found the problem.

The server was fine all along. My mistake was that I was trying to use my external IP from the same computer as the server, creating some kind of loopback. When I tried to connect from another computer outside my network it worked fine.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question