CAsyncSocket Server - unable to connect using public IP

Posted on 2008-11-15
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
    LVL 86

    Expert Comment

    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

    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

    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.

    Author Comment

    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

    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

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now