简体   繁体   中英

How can I gracefully start my C# TCP client before I start my TCP server

I am writing a fairly simple TCP/IP client application. I am able to successfully connect when I start the server (another seperate application) and then start the client in that order. I would like to make the client robust so that I can start it first and it doesnt break. Also I dont want a special user action to tell the client to try and connect. I'd also like to use non-blocking asynchronous IO on the client.

In order to try and accomplish this I have tried to use the Socket.BeginConnect method. My intention is that this call will spawn a background thread which would be blocked at the EndConnect call until a connection is established. This way my client can call this method at its startup and then continue operation. Here is my code:

public bool Init_Port(string Host)
    {
        IPHostEntry ipHostInfo = Dns.Resolve(Host);
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);

        try
        {
            //Create the connection
            My_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            My_Socket.BeginConnect(localEndPoint, new AsyncCallback(Connect_To_Port), My_Socket);

        }
        catch (Exception e)
        {
            ErrorHandler(e.Message);
            return false;
        }
        return true;
    }

    private void Connect_To_Port(IAsyncResult ar)
    {
        try
        {
            if (My_Socket != null)
            {
                //will block the thread until a connection is made
                My_Socket.EndConnect(ar);

                Connection_Flag = true;

                //Setup the asynchronous read
                My_Socket.BeginReceive(Rx_Buffer, 0, SOCKET_BUFFER_SIZE, 0,
                                                new AsyncCallback(Read_Port), My_Socket );
            }
        }
        catch (Exception e)
        {
            ErrorHandler(e.Message);
        }
    }

I test this without starting the server application. I am hoping that the thread will be blocked at the EndConnect until my server application is started. What I observe is that EndConnect returns immediately and that I get the error message

No connection could be made because the target machine actively refused it 127.0.0.1:50042

Can anyone tell me what I can do to make this asynchronous connect wait for the server application? Thanks in advance.

Can anyone tell me what I can do to make this asynchronous connect wait for the server application?

Nothing. YOu run it in a loop every X seconds until it starts or you get a logical timeout. TCP will not wait, you have to retry.

Just have the client connect on demand, when the code first needs a connection, according to what the user is doing. If the connection fails, tell the user he can't do that yet, whatever it was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM