Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that uses a client socket to send data to server device. I only send data on this connection so if the connection
fails I do know. Buffering allows several messages to be send before an error. What is the best way to detect that the connection is valid before sending data and what is the best way to reconnect?
Posted

1 solution

to test connecting:
try
{
//send message
}
catch
{
//connection not valid
}


to connect/reconnect:
TcpClient tcpClient;

private void ConnectToServer(string ServerIP, int ServerPort)
        {
            
            
            tcpClient = new TcpClient();
            try
            {
                // Connect the TCP client to the specified IP and port
                tcpClient.Connect(ServerIP, ServerPort);

                //you will also have to reset your network stream you              write with:
                //NetworkStreamName = tcpClient.GetStream();
            }
            catch (Exception exMessage)
            {
                //error
                
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900