Public class TCPClient
{
# region attribute
///& lt; Summary>
///the server IP
///& lt;/summary>
Public string ServerIp;
///& lt; Summary>
///the server listening on port
///& lt;/summary>
Public int ServerPort;
///& lt; Summary>
///TcpClient client
///& lt;/summary>
Public TcpClient TcpClient=null;
///& lt; Summary>
///a Tcp client connection thread
///& lt;/summary>
Public Thread Tcpthread=null;
///& lt; Summary>
///whether to start the Tcp connection thread
///& lt;/summary>
Public bool IsStartTcpthreading=false;
///& lt; Summary>
///connection is closed (to disconnect reconnection)
///& lt;/summary>
Public bool Isclosed=false;
///& lt; Summary>
///set the disconnect reconnection interval unit (ms) (default 3000 milliseconds)
///& lt;/summary>
Public int ReConnectionTime=3000;
///& lt; Summary>
///receiving Socket packet buffer string
///& lt;/summary>
Public string Receivestr;
///& lt; Summary>
///number of reconnection
///& lt;/summary>
Public int ReConectedCount=0;
# endregion
# region constructor
///& lt; Summary>
///initializes the TCPClient class
///& lt;/summary>
///& lt; Param name="IP" & gt; The service side IP
///& lt; Param name="port" & gt; Listen on port & lt;/param>
IP public TCPClient (string, string port)
{
ServerIp=IP;
ServerPort=int. Parse (port);
}
# endregion
# region method
///& lt; Summary>
///start the connection Socket server
///& lt;/summary>
Public void StartConnection ()
{
Try
{
CreateTcpClient ();
}
The catch (Exception ex)
{
//DelegateHelper. TcpClientErrorMsg (" error Message: "+ ex. Message);
}
}
///& lt; Summary>
///create a thread connection
///& lt;/summary>
Private void CreateTcpClient ()
{
If (Isclosed)
return;
Isclosed=true;
Tcpthread=new Thread (StartTcpThread);
IsStartTcpthreading=true;
Tcpthread. Start ();
}
///& lt; Summary>
///thread receives the Socket to upload data
///& lt;/summary>
Private void StartTcpThread ()
{
Byte [] receivebyte=new byte [1024].
Int bytelen;
Try
{
While (IsStartTcpthreading)
# region
{
//Thread. Sleep (50);
if (! Tcpclient. Connected)
{
Tcpclient=new Tcpclient ();
Try
{
If (ReConectedCount!=0)
{
//return status information
//DelegateHelper TcpClientStateInfo (string Format (the first {0} "is time to connect to the server... . ", ReConectedCount), EnumClass. SocketState. Reconnection);
}
The else
{
//SocketStateInfo
//DelegateHelper. TcpClientStateInfo (" is connect to the server... . ", EnumClass. SocketState. Connecting);
}
Tcpclient. Connect (IPAddress. Parse (ServerIp), ServerPort);
//DelegateHelper. TcpClientStateInfo (" has been connect to the server, "EnumClass. SocketState. Connected);
//Tcpclient. Client. Send (Encoding. The Default. GetBytes (" login "));
}
Catch
{
//connection failure
ReConectedCount++;
Isclosed=false;
IsStartTcpthreading=false;
Thread.sleep (ReConnectionTime);
continue;
}
}
Bytelen=Tcpclient. Client. The Receive (receivebyte);
//disconnects
If (bytelen==0)
{
//return status information
//DelegateHelper. TcpClientStateInfo (" disconnected with the server... . ", EnumClass. SocketState. Disconnect);
//abnormal exit, forced to reconnect
Isclosed=false;
ReConectedCount=1;
IsStartTcpthreading=false;
continue;
}
Receivestr=ASCIIEncoding. Default. Get string (receivebyte, 0, bytelen);
If (Receivestr. The Trim ()!="")
{
//receiving data
//DelegateHelper TcpClientReceive (Receivestr);
}
}
# endregion
//thread will end at this time, people over, automatically determine whether reconnection
Thread.sleep (ReConnectionTime);
CreateTcpClient ();
}
The catch (Exception ex)
{
//CreateTcpClient ();
//return error message
//DelegateHelper. TcpClientErrorMsg (" error Message: "+ ex. Message);
}
}
///& lt; Summary>
Disconnected///
///& lt;/summary>
Public void StopConnection ()
{
IsStartTcpthreading=false;
Isclosed=false;
Tcpclient. Close ();
Tcpthread. Interrupt ();
Tcpthread. Abort ();
//DelegateHelper. TcpClientStateInfo (" disconnected ", EnumClass. SocketState. Disconnect);
}
///& lt; Summary>
The Socket messages sent///
///& lt;/summary>
///& lt; Param name="CMDSTR & gt;"
Public void SendCommand (string CMDSTR)
{
Try
{
Byte [] _out=Encoding. The Default. GetBytes (CMDSTR);
Tcpclient. Client. Send (_out);
}
The catch (Exception ex)
{
//return error message
//DelegateHelper TcpClientErrorMsg (ex. Message);
}
}
# endregion
}
# region IPlugin interface implementation method
TCPClient _tcpClient;
///& lt; Summary>
///initializes the connection
///& lt;/summary>
IP public void Init (string, string port, string servername, string state, string heartbeat)
{
_tcpClient=new TCPClient (IP, port);
_tcpClient. StartConnection ();
HeartbeatMessage=heartbeat;
HeartStart ();
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull