Hello guys,
I have a TCP Listener Server which listens to requests and replies them. It is working fine in a particular network. But what if i would need to make it available for everyone (my clients) over internet to connect to it and send receive messages/data? My current code is working fine in LAN setup but what things I need to make to let my clients/users connect to it over internet?
please help me. Following is the code:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class ThreadedTcpSrvr {
   private TcpListener client;

   public ThreadedTcpSrvr()
   {
      client = new TcpListener(8000);
      client.Start();

      Console.WriteLine("Waiting for clients...");
      while(true)
      {
         while (!client.Pending())
         {
            Thread.Sleep(1000);
         }

         ConnectionThread newconnection = new ConnectionThread();
         newconnection.threadListener = this.client;
         Thread newthread = new Thread(new
                   ThreadStart(newconnection.HandleConnection));
         newthread.Start();
      }
   }

   public static void Main()
   {
      ThreadedTcpSrvr server = new ThreadedTcpSrvr();
   }
}

class ConnectionThread
{
   public TcpListener threadListener;
   private static int connections = 0;

   public void HandleConnection()
   {
      int recv;
      byte[] data = new byte[1024];

      TcpClient client = threadListener.AcceptTcpClient();
      NetworkStream ns = client.GetStream();
      connections++;
      Console.WriteLine("New client accepted: {0} active connections",
                        connections);

      string welcome = "Welcome to Server";
      data = Encoding.ASCII.GetBytes(welcome);
      ns.Write(data, 0, data.Length);

      while(true)
      {
         data = new byte[1024];
         recv = ns.Read(data, 0, data.Length);
         if (recv == 0)
            break;
      
         ns.Write(data, 0, recv);
      }
      ns.Close();
      client.Close();
      connections--;
      Console.WriteLine("Client disconnected: {0} active connections",
                         connections);
   }
}

Dani AI

Generated

Quick primer: , your listener works on the LAN but clients on the wider Internet can’t reach it unless the host is reachable from the WAN. is correct that you must change network/NAT/router settings — here’s a practical checklist and troubleshooting flow that fills in the missing steps.

  • Give the server a stable LAN address (static IP or a DHCP reservation) so the router’s forward always points to the right machine.
  • On the router, create a TCP port‑forward rule from the router’s WAN address to the server’s LAN IP and the port your app listens on. Use the same external port or map an external port to an internal one if you need.
  • Open an inbound rule on the server firewall (or allow the app) for that TCP port.
  • Verify the server is bound to a non‑loopback interface (it must accept remote connections, not only 127.0.0.1). Use netstat -an to confirm a listening socket on the expected host:port.
  • Find the router’s public IP (router status page or an external IP lookup) and test from outside your LAN (use a phone on cellular data or a remote host). Try telnet <public-ip> <port> or nc -vz <public-ip> <port> to confirm connectivity. Note: testing from inside the LAN using the public IP may fail on some routers (NAT hairpinning).

If tests fail, check these common blockers: the router rule targets the wrong internal IP, the server firewall still blocks the port, the ISP blocks incoming ports (or uses CGNAT so you don’t have a public IPv4), or your server is only bound to loopback. If your ISP uses CGNAT, port forwarding won’t work — options are requesting a public IP, hosting on a VPS/cloud instance, or using a reverse tunnel/relay service.

Security notes: do not expose an unauthenticated plaintext TCP service to the Internet. Add TLS (SslStream or a TLS proxy), authenticate clients, log and limit connections, and apply rate limits or IP restrictions for production use.

You need to port forward the TCP port on your router to the machine running your application. This has nothing to do with C# but rather networking hardware.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.