Skip to content

C# Auto Reconnect Client to Server

jojo cadiente

Asked Sep 27, 2013, 3:08 AM EDT2 answers
Share
LinkedInX

Hello Could anybody help me about this I'm trying to create client and server application reverse connection. When I tried to open the client and server I didn't encounter any problem vise versa. The scenario is I execute the client and the server but when I closed the server and re open again it doesn't accept any connection.

Here is the Code:

CLIENT CODE:

C#
namespace client1
{
  class Program
   {
    public static bool isConnected { get; set; }
    public static NetworkStream Writer { get; set; }
    public static NetworkStream Reciever { get; set; }

    static void Main(string[] args)
    {
        Connect_To_Server();
    }

    public static void Connect_To_Server()
    {
        TcpClient Connecting = new TcpClient();
        string IP = "178.121.1.2";
        while (isConnected == false)
        {
            try
            {
                Connecting.Connect(IP, 2000);
                isConnected = true;

                Writer = Connecting.GetStream();
                Reciever = Connecting.GetStream();
                Console.WriteLine("Connected: " + IP);
            }
            catch
            {
                Console.WriteLine("Error Connection... .");
                Console.Clear();
            }
        }
        Console.ReadKey();
    }

 }
}
Code
Code
C#
SERVER CODE:
 namespace server1
 {
  class Program
   {

    public static bool isConnected { get; set; }
    public static NetworkStream Writer { get; set; }

    static void Main(string[] args)
    {
        Listen_To_Client();
    }

    public static void Listen_To_Client()
    {
        TcpListener Listen = new TcpListener(2000);

        while (true)
        {
            Listen.Start();

            if (Listen.Pending())
            {

                TcpClient Connect = Listen.AcceptTcpClient();
                isConnected = true;
                Console.WriteLine("Connection Accepted");
                Writer = Connect.GetStream();
                Console.ReadKey();
            }

        }

      }

     }
    }

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,
    Your code is working fine for me, without any error :-) . I'm running the both the client and server application in the same system.  So try to run your application in the same system, make sure it is not giving any issues.

    If your're in it two different system, make sure, the FireWall is allowed port number 2000 and there valid inbound and outbound rules also present in both the system.

    Hope this helps you.
    0
  • 0
    Yup sir the code works perfect. The only issue about  at first open the client and server or vice versa works fine. The Main problem now  don't close the client just close the server then re open the server it does not connect it anymore.

    If you will close the client leaving the server open then re open the client works 100% perfect. The main problem is doing vice versa..
    0