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);
   }
}

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.