user1670407
user1670407

Reputation:

C# Sockets over the internet

I successfully made a client server program in C# working perfectly under a LAN, we used the TcpListener and TcpSocket class.

We could not get it to work over the internet though, I understand that it has to do with firewalls, router port blocking etc.

We forwarded the port we used and turned off our firewalls and still no luck.

What do I have to do differently in order to make this work? Like a certain port to use that will work without issues? How does say "Msn Messenger" do it?

Server Code:

  private static TcpListener serverTcpListener;

   public static bool Run()
    {
        // Initialize new thread for client communications
        Thread listenThread = new Thread(new ThreadStart(ListenForClients));
        
        // Initialize TCP listener and attempt to start
        ServerTcpListener = new TcpListener(IPAddress.Any, 3000);

        try
        {
            ServerTcpListener.Start();
        }
        catch (SocketException)
        {
            return false;
        }


        // Start client communications thread
        listenThread.Start();

        return true;
    }





    
    public static void ListenForClients()
    {
        while (true)
        {
            TcpClient client = ServerTcpListener.AcceptTcpClient();
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

Client Code:

    private TcpClient myClient;
    private NetworkStream clientStream;

   public InitializeResult Initialize()
    {
        IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);

        try
        {
            MyClient.Connect(serverEndPoint);
        }
        catch (SocketException)
        {
            return InitializeResult.AccessError;
        }
        catch (ArgumentNullException)
        {
            return InitializeResult.NullRemote;
        }

        try
        {
            ClientStream = MyClient.GetStream();
        }
        catch (Exception)
        {
            return InitializeResult.StreamFail;
        }

        if (!Authenticate())
        {
            return InitializeResult.AuthenticateFail;
        }


        return InitializeResult.Success;
        
    }

Upvotes: 5

Views: 11648

Answers (4)

Trisped
Trisped

Reputation: 6005

You need to replace 127.0.0.1 with your actual IP address in:

IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);

To get your IP you can Google "what is my ip" or go to www.whatismyip.com/ from the server.

I also recommend installing Wireshark on both your client and server. I use this tool often when debugging sockets and other network issues.

Upvotes: -1

Matt Sieker
Matt Sieker

Reputation: 9645

If this:

IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);

is exactly what's in your client code, that's probably where the issue lies, even if firewalls are set up right. You'll have to modify the client program to take an IP as a configuration setting, command line parameter, text box, etc. and then use that instead.

I would first test this with two computers on the same LAN, if possible, using the address of the box running the server. If you can't connect, ensure the IP is right (by running ipconfig on the machine running the server), and seeing if Windows Firewall is on on the server. If it is, you'll either have to allow your program, or just open port 3000.

Once that's confirmed working, from another network entirely, with the external IP of the network the server is on. You can get this by going to http://icanhazip.com/ or similar (I prefer this over whatismyip, no ads, it only serves back the IP address), and ensuring port 3000 on the firewall/router is set to be forwarded to the internal IP of the server box.

Upvotes: 2

caesay
caesay

Reputation: 17233

Here are some common problems you might run in to:

Server

  1. Windows Firewall.
  2. Port forwarding on the router.
    • http://portforward.com/ is a good resource, I believe you can select your router and it will give you steps for that. You will need to select the LAN IP address of the machine hosting the server, and the port you are attempting to connect on (in your case, 3000) .

Client

For the client, it should be able to connect fine, but you will have to specify the external IP address, and not the LAN IP address of the server. You can find it by going to http://whatismyip.com on the computer hosting the server.

--

Msn and similar chat programs use a straightforward client/server approach like this - very easy to accomplish. A very interesting chat program to look into though, is Skype. It uses a P2P system, not a Client-Server. It accomplishes it using Nat Hole Punching, if you're interested (as a student) I'd suggest looking into it to expand your knowledge of networking.

Upvotes: 1

sehe
sehe

Reputation: 393969

You need to bind to the external NIC. Chances are you bind to the Loopback adaptor

If you provide some code, we could, maybe, diagnose (or even fix) things

Upvotes: 0

Related Questions