title: TCP/IP client server Communication over different network in c#
body: <p>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.</p>
  
  <pre><code>//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 &lt; 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 &lt; k; i++)
                  Console.Write(Convert.ToChar(bb[i]));
  
              tcpclnt.Close();
          }
  
          catch (Exception e)
          {
              Console.WriteLine("Error..... " + e.StackTrace);
          }
          Console.ReadLine();
       }
      }
   }
  </code></pre>