Thousand
Thousand

Reputation: 6638

client/server chat program in C#/winforms IP address issue

I am trying to make a winforms client/server chat application. I already have a client and a server, both are working fine, i can log in, send messages, etc .. The problem is that it only works if i connect to my local IP. When i try to run the program with my external IP, i get the following error:

"The requested address is not valid in its context".

In other words, i can run the server on my own computer, start up (for example) 3 clients and let them connect to the running server. This basically means that i'm talking to myself. What im trying to do is to run the chat server on my computer, and let other people run the client-program and connect to the server through the internet.

this is the part where i get an error when i enter my external IP:

public void StartListening()
    {

        // Get the IP of the first network device, however this can prove unreliable on certain configurations
        IPAddress ipaLocal = ipAddress;

        // Create the TCP listener object using the IP of the server and the specified port
        tlsClient = new TcpListener(ipaLocal, 50702);

        // Start the TCP listener and listen for connections
        tlsClient.Start(); <--- THINGS GO SOUTH HERE

        // The while loop will check for true in this before checking for connections
        ServRunning = true;

        // Start the new tread that hosts the listener
        thrListener = new Thread(KeepListening);
        thrListener.Start();
    }

im not sure if what i'm trying to do is possible? i can't imagine it is, but i kinda dont know how to proceed with this. I'm new to network programming so any help will be appreciated.

kind regards, Jane

Upvotes: 0

Views: 2120

Answers (2)

vgru
vgru

Reputation: 51214

Your server application needs to listen to incoming connections on a specified port, on a specified locally installed NIC. That is why TcpListener always needs to be created using a local IP address: because it only cares which NIC (if you have multiple installed) it should use.

The MSDN page for TcpListener also states it explicitly:

TcpListener Constructor (IPAddress, Int32) Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number.

External IP address is completely irrelevant to a TCP/IP server. You can have numerous routers and network devices along the way, which can then forward incoming connections to your machine.

Just do make sure that your firewall and router are configured properly to allow incoming connections on a specified port. To do that, start your TCP/IP server to open the port, and then use a service like CanYouSeeMe to see if server can be reached from outside.

Regarding your comment (this can prove unreliable on certain configurations), it's obviously "unreliable" when you think about it: a laptop can easily have an Ethernet network controller with a completely different IP address than a Wifi network adapter. Your server app should allow the user to select which IP address to use, instead of picking the first address it gets.

Upvotes: 1

iefpw
iefpw

Reputation: 7042

Jane,

I think your issue is with your IP address setup. This is a network connection problem. You need external IP address so that outside clients can contact your PC. You need more advanced networking. The IP address provided from you ISP is used for domestic uses. You need specialized public IP address so that clients can find you outside of the firewall. This is networking/ISP/external IP issue.

Upvotes: 1

Related Questions