Using system;
Using system. text;
Using system. collections;
Using system. Collections. Generic;
Using system. net;
Using system. net. Sockets;
Using system. Threading;
Using system. runtime. interopservices;
Using system. IO;
Namespace common. tcplib
{
/// <Summary>
/// Synchronous socket processing client for short connection Processing
/// </Summary>
Public class tcpclients
{
Public socket _ clientsocket;
Private string _ serverip;
Private int _ port;
Private int _ buffersize = 1024;
Private bool _ disposed = false;
Private bool _ DEBUG = false;
# Region define delegates
/// <Summary>
/// Handle the event after connecting to the server
/// </Summary>
Public _ bgz_onconnecteventdelegate fonconnecteventdelegate;
/// <Summary>
/// Receives data event processing from the server
/// </Summary>
Public _ bgz_onreceivebegineventdelegate fonreceivebegineventdelegate;
/// <Summary>
/// Receives data event processing from the server
/// </Summary>
Public _ bgz_onreceiveingeventdelegate fonreceiveingeventdelegate;
/// <Summary>
/// Receives data event processing from the server
/// </Summary>
Public _ bgz_onreceiveendeventdelegate fonreceiveendeventdelegate;
/// <Summary>
/// Handle error messages
/// </Summary>
Public _ Bgz_OnErrorEventDelegate FOnErrorEventDelegate;
# Endregion
# Region Event
Private void OnConnectEvent (_ Bgz_ConnectionState state)
{
If (FOnConnectEventDelegate! = Null) FOnConnectEventDelegate (state );
}
Private void OnReceiveBeginEvent (_ Bgz_ConnectionState state)
{
If (FOnReceiveBeginEventDelegate! = Null) FOnReceiveBeginEventDelegate (state );
}
Private void OnReceiveingEvent (_ Bgz_ConnectionState state)
{
If (FOnReceiveingEventDelegate! = Null) FOnReceiveingEventDelegate (state );
}
Private void OnReceiveEndEvent (_ Bgz_ConnectionState state)
{
If (FOnReceiveEndEventDelegate! = Null) FOnReceiveEndEventDelegate (state );
}
Private void onerrorevent (errortype, string MSG, _ bgz_connectionstate state)
{
If (fonerroreventdelegate! = NULL) fonerroreventdelegate (errortype, MSG, State );
}
# Endregion
# Region Property
Public int buffersize
{
Get
{
Return _ buffersize;
}
}
Public bool debug
{
Get
{
Return _ debug;
}
Set
{
_ Debug = value;
}
}
# Endregion
# Region Constructor and Destructor
Public TcpClientS (string serverIp, int port)
{
This. _ serverIp = serverIp;
This. _ port = port;
}
Public TcpClientS (string serverIp, int port, int bufferSize)
{
This. _ serverip = serverip;
This. _ Port = port;
This. _ buffersize = buffersize;
}
~ Tcpclients ()
{
}
# Endregion
# Region private methods
Private void dispose ()
{
If (! _ Disposed)
{
GC. Collect ();
GC. WaitForPendingFinalizers ();
This. FOnConnectEventDelegate = null;
This. FOnErrorEventDelegate = null;
This. FOnReceiveBeginEventDelegate = null;
This. FOnReceiveEndEventDelegate = null;
This. FOnReceiveingEventDelegate = null;
_ Disposed = true;
}
}
# Endregion
# Region Public Methods
Public byte [] SendAsOdian (byte [] msg)
{
Return SendAsOdian (msg, 60000 );
}
Public byte [] SendAsOdian (byte [] msg, int ReceiveTimeout)
{
# Region prevents Socket from sending NULL bytes
If (msg = null | msg. Length = 0) return null;
# Endregion
_ Bgz_ConnectionState stx = new _ Bgz_ConnectionState ();
# Region Connect
Try
{
Stx. _ conn = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
Stx. _ conn. Connect (new IPEndPoint (IPAddress. Parse (_ serverIp), _ port ));
Stx. _ conn. LingerState = new LingerOption (false, 3 );
# Region sets the processing of Socket receipt latency
Stx. _ conn. ReceiveTimeout = ReceiveTimeout;
# Endregion
OnConnectEvent (stx );
}
Catch (Exception ex)
{
If (Debug)
{
OnErrorEvent (ErrorType. Catch, "SendAsOdian.1 Error! [Message]: \ r \ n "+ ex. Message +" [StackTrace]: \ r \ n "+ ex. StackTrace +" \ r \ n ", null );
}
Return null;
}
# Endregion
Try
{
Stx. _ conn. Send (msg );
# Region read to bytes
Stx. _ buffer = new byte [0];
Stx. _ count = 0;
Stx. _ getonceall = false;
Stx. _ conn. Receive (stx. _ buffer );
If (stx. _ conn. Available = 0)
{
If (Debug)
{
Onerrorevent (errortype. Catch, "Close the connection to the server! ", Null );
}
STX. _ conn. Shutdown (socketshutdown. Both );
STX. _ conn. Close ();
Return Stx. _ buffer;
}
OnReceiveBeginEvent (stx );
Stx. _ count = 0;
Stx. _ dataStream. SetLength (0 );
Stx. _ dataStream. Position = 0;
If (stx. _ getonceall)
{
Stx. _ buffer = new byte [stx. _ conn. Available];
Int ret = Stx. _ conn. Receive (Stx. _ buffer, 0, stx. _ buffer. length, socketflags. None );
If (Ret> 0)
{
STX. _ datastream. Write (Stx. _ buffer, 0, stx. _ buffer. Length );
STX. _ count ++;
OnReceiveingEvent (stx );
}
}
Else
{
While (stx. _ conn. Available> 0)
{
If (stx. _ conn. Available> this. _ bufferSize)
Stx. _ buffer = new byte [this. _ bufferSize];
Else
Stx. _ buffer = new byte [stx. _ conn. Available];
Int ret = Stx. _ conn. Receive (Stx. _ buffer, 0, stx. _ buffer. length, socketflags. None );
If (Ret> 0)
{
STX. _ datastream. Write (Stx. _ buffer, 0, stx. _ buffer. Length );
STX. _ count ++;
Onreceiveingevent (STX );
}
}
}
Onreceiveendevent (STX );
# Endregion
}
Catch (exception ex)
{
If (Debug)
{
Onerrorevent (errortype. Catch, "sendasodian.2 error! [Message]: \ r \ n "+ ex. Message +" [stacktrace]: \ r \ n "+ ex. stacktrace +" \ r \ n ", null );
}
}
Finally
{
If (Stx. _ Conn! = NULL)
If (Stx. _ conn. Connected)
{
STX. _ conn. Shutdown (socketshutdown. Both );
STX. _ conn. Close ();
}
OnErrorEvent (ErrorType. DisConnect, "Close the connection with the server! ", Null );
}
Return stx. _ buffer;
}
# Endregion
}
}