TCP/IP client server Communication over different network in c#

10,655

Your server is listening for connections from 192.168.0.110 port 8001

IPAddress ipAd = IPAddress.Parse("192.168.0.110");
TcpListener myList = new TcpListener(ipAd, 8001);

The client connects to server with IP is: 192.168.0.110. The server is on the same network. So if the server is on and listening for connection this will work.

tcpclnt.Connect("192.168.0.110", 8001);

So everything is fine but if your client is on an different network it can't find the server of course because the client will look for IP: 192.168.0.110:8001 (local server IP) in the local network and there is no server that will listen for that.

So to make this work you'll have to Port forward your router on the network of your server. Set in the router that every incoming connection on port: 8001 will be redirected to 192.168.0.110:8001 (local server IP).

For example if your public IP is 10.10.10.10 than you should let your client connect like this:

tcpclnt.Connect("10.10.10.10", 8001);

So if your client connects to 10.10.10.10:8001 the router will redirect it to the server on local network and the client will be able to connect to the server.

/*Server example for listening. (IPAddress.any) means any IP will be allowed to connect*/
TcpListener myList = new TcpListener(IPAddress.Any,8001);

!!Notice how to port forward is for every router different so a quick search on google will help you how to do this.

Share:
10,655
Valar_Dohaeris
Author by

Valar_Dohaeris

Updated on June 04, 2022

Comments

  • Valar_Dohaeris
    Valar_Dohaeris almost 2 years

    I am able to establish communication between client and server using TCP/IP.Client and server can send and receive messages to each other only if server and client is in same network if both are in different network then client does not connect with server.What should be done when they are in different network? please help.here is server and client code.Thank you.

    //Server Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Net;
    using System.IO;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
    class Program
    {
        static void Main(string[] args)
        {
    
            try
            {
                IPAddress ipAd = IPAddress.Parse("192.168.0.110"); //use local       m/c IP address, and use the same in the client
    
                /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 8001);
    
                /* Start Listeneting at the specified port */
                myList.Start();
    
                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local End point is  :" +   myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");
    
                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " +  s.RemoteEndPoint);
    
                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved...");
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(b[i]));
                Console.WriteLine("Enter the string to be strasmitted");
    
                String str = Console.ReadLine();
    
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes(str));
                Console.WriteLine("\nSent Acknowledgement");
                /* clean up */
                s.Close();
                myList.Stop();
    
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
            Console.ReadLine();
         }
    
        }
       }
    
       //Client Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;
    using System.Net.Sockets;
    
    namespace ConsoleApplication4
    {
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");
    
                tcpclnt.Connect("192.168.0.110", 8001); // use the ipaddress as   in the server program
    
                Console.WriteLine("Connected");
                Console.Write("Enter the string to be transmitted : ");
    
                String str = Console.ReadLine();
                Stream stm = tcpclnt.GetStream();
    
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                Console.WriteLine("Transmitting.....");
    
                stm.Write(ba, 0, ba.Length);
    
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
    
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(bb[i]));
    
                tcpclnt.Close();
            }
    
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
            }
            Console.ReadLine();
         }
        }
     }