Client :

public class TCPClientWrapper : IDisposable
{
    private TcpClient tcpClient;
    private readonly string address;
    private readonly int port;

    public TCPClientWrapper(string address, int port)
    {
        tcpClient = new TcpClient();
        this.address = address;
        this.port = port;
    }

    private void TryConnect()
    {
        tcpClient = new TcpClient();
        tcpClient.SendTimeout = 15;
        bool isConnected = false;
        while (true)
        {
            try
            {
                Log.Info("TcpClient, Trying Connect");
                tcpClient.Connect(IPAddress.Parse(address), port);
                if (SocketConnected(tcpClient.Client))
                {
                    Log.Info("TcpClient, Connected");
                    isConnected = true;
                    break;
                }
            }
            catch (Exception e)
            {
                Log.Info("TcpClient, connection failed. Try to reconnect after 30 seconds, {0}", e.Message);
            }
            finally
            {
                if (!isConnected)
                    Thread.Sleep(30000);
            }
        }
    }

    public void SendMessage(string msg)
    {
        if (!SocketConnected(tcpClient.Client))
        {
            TryConnect();
        }

        byte[] buffer = Encoding.UTF8.GetBytes(msg);
        tcpClient.Client.Send(buffer);
    }

    private bool SocketConnected(Socket s)
    {
        if (!s.Connected)
            return false;

        bool part1 = s.Poll(1000, SelectMode.SelectRead);
        bool part2 = s.Available == 0;
        return !(part1 && part2);
    }

    public void Dispose()
    {
        tcpClient.Close();
    }
}

Server running as windows service:

 public class TcpServer
{
    private bool started;
    private bool stopped;
    private TcpListener tcpListener;
    private static ManualResetEvent allDone = new ManualResetEvent(false);

    public TcpServer(string url, int port)
    {
        tcpListener = new TcpListener(IPAddress.Parse(url), port);
    }

    [MethodImpl(MethodImplOptions.Synchronized)]
    public void Run()
    {
        if (started) return;
        stopped = false;

        tcpListener.Start();

        Task.Run(() =>
        {
            Log.Info("Server running");
            while (!stopped)
            {
                allDone.Reset();
                tcpListener.BeginAcceptSocket(AcceptCallback, tcpListener);
                Log.Info("Accepting socket");
                allDone.WaitOne();
            }
        });

        Log.Info("Ping server started");
        started = true;
    }

    [MethodImpl(MethodImplOptions.Synchronized)]
    public void Stop()
    {
        if (!started) return;
        stopped = true;

        tcpListener.Stop();
        started = false;
        Log.Info("Ping server stopped");
    }

    private void AcceptCallback(IAsyncResult result)
    {
        try
        {
            allDone.Set();
            if (stopped) return;
            Log.Info("Socket accepted");

            var listener = (TcpListener)result.AsyncState;
            var socket = listener.EndAcceptSocket(result);

            Log.Info("Process socket");
            ProcessSocket(socket);
        }
        catch (Exception e)
        {
            Log.Info("Error accepting callback. {0}", e.Message);
        }
    }

    private void ProcessSocket(Socket socket)
    {
        try
        {
            byte[] buffer = new byte[256];
            while (!stopped && socket.Receive(buffer) != 0)
            {
                var msg = Encoding.UTF8.GetString(buffer);
                Console.WriteLine(msg);
            }
        }
        catch (Exception e)
        {
            socket.Close();
            Log.Info("Socket closed:{0}", !socket.Connected);
        }
    }
}

The server is configured in such a way that request to server processed on one IP xxx.xx.xxx.135:5050 and response from server given from xxx.xx.xxx.134:5050

The client works fine for some period of time, but after i get the following error on client side:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xx.xxx.135:5050

What is the reason that the client can't connect to server ?

1 Answers

1
Gabriel David Gonzales On

Check firewall settings on production server

Check whether IP white listing is required

Antivirus might be blocking the request

If no luck, install Advanced REST Client tool on Chrome and manually test the request

https://forums.asp.net/t/2138734.aspx?A+connection+attempt+failed+because+the+connected+party+did+not+properly+respond+after+a+period+of+time