简体   繁体   中英

C# How do I keep the socket or connection from closing?

I am using C# written application that connects to a few of our devices via IP. The application connects to the device just fine and we can send the required commands we need to configure it. The issue I run into is after about 40 seconds to a minute of not sending any commands the connection disconnects. I am wondering what I can do to keep the socket alive for at least a few minutes. Need some guidance on implementing a heartbeat, any assistance is appreciated.

Here is the code we are using.

using System;
using System.Net;    
using System.Net.Sockets;    
using System.Threading;    
using System.Text;    
using System.Globalization;    

// State object for receiving data from remote device.    
public class StateObject
{

    // Client socket.
    public Socket workSocket = null;

    // Size of receive buffer.
    public const int BufferSize = 256;

    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];

    // Received data string.
    public StringBuilder sb = new StringBuilder();
}

public class AsynchronousClient
{

    // The port number for the remote device.
    //private const int port = 1000;

    // ManualResetEvent instances signal completion.
    private static ManualResetEvent connectDone =
        new ManualResetEvent(false);
    private static ManualResetEvent sendDone =
        new ManualResetEvent(false);
    private static ManualResetEvent receiveDone =
        new ManualResetEvent(false);

    // The response from the remote device.
    private static String response = String.Empty;

    static Socket client;

    internal static Boolean StartClient(string ip_address, int port)
    {
        // Connect to a remote device.
        Boolean bRtnValue = false;
        try
        {

            // Establish the remote endpoint for the socket.
            // The name of the 
            // remote device is "host.contoso.com".
            //IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
            IPAddress ipAddress = IPAddress.Parse(ip_address);
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

            // Create a TCP/IP socket.
            client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Connect to the remote endpoint.
            client.BeginConnect(remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();

            // Send test data to the remote device.
            string msg = ((char)2) + "S" + (char)13;
            Send(client, msg);
            //sendDone.WaitOne();

            // Receive the response from the remote device.
            Receive(client);
            //receiveDone.WaitOne();

            // Write the response to the console.
            Console.WriteLine("Response received : {0}", response);
            bRtnValue = true;

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        //return value 
        return bRtnValue;
    }

    private static void ConnectCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket client = (Socket)ar.AsyncState;

            // Complete the connection.
            client.EndConnect(ar);

            Console.WriteLine("Socket connected to {0}",
                client.RemoteEndPoint.ToString());

            // Signal that the connection has been made.
            connectDone.Set();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void Receive(Socket client)
    {
        try
        {
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = client;

            // Begin receiving the data from the remote device.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the state object and the client socket 
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;

            // Read data from the remote device.
            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0)
            {

                // There might be more data, so store the data received so far.
                state.sb.Clear();
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                Console.WriteLine(DateTime.Now.ToLongTimeString() + ": " + state.sb.ToString());
                //54A111503000000000017D8857E3
                //IDSSSSSSSSSSSSSSSSTTTTCCCCKK    017D
                if (state.sb.ToString(0, 2) == "54")
                {
                    string hexString = state.sb.ToString(18, 4);
                    int num = Int32.Parse(hexString, NumberStyles.HexNumber);
                    double degreesF = ((double)num / 16.0) * 9.0 / 5.0 + 32.0;
                    string f = degreesF.ToString("#.#");
                    Console.WriteLine(" " + f);
                }

                // Get the rest of the data.
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
            }
            else
            {
                Console.WriteLine("data");
                // All the data has arrived; put it in response.
                if (state.sb.Length > 1)
                {
                    response = state.sb.ToString();
                }
                // Signal that all bytes have been received.
                receiveDone.Set();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void Send(Socket client, String data)
    {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        client.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), client);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket client = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = client.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to server.", bytesSent);

            // Signal that all bytes have been sent.
            sendDone.Set();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    internal void StartListening(string ip_address, int port)
    {
        StartClient(ip_address, port);
    }

You need to implement a timer that simulates a heart beat when the is no data to be sent, Ie after the last packet you send you start a timer for a couple of seconds. If you have data to send before that you cancel it, send your data and restart the timer again. If the timer times out you send a dummy data packet and restart the timer to do it again after the same time out period.

The reason for this is what's known as timeout, an important feature of (to my knowledge) every network protocol in place to handle when the other end of the connection aborts, disconnects, or otherwise stops transmitting data altogether. There is, after all, no way to transmit a packet saying "my cat just knocked my router down, abort connection."

What you need to do is, as mentioned in comments, send periodic no-op (ie do-nothing) commands through the network, just to keep it from timing out. Such packets are called "heartbeat" or "keepalive" signals, and should be sent far more often than the actual timeout, in case any get lost or arrive late. This is best done by starting up a seondary thread which does nothing but send heartbeats while the connection is marked as open.

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