[C#]事件驱动的SOCKET类
2018年10月20日
⁄ 综合
⁄ 共 11421字 ⁄ 字号
小 中 大
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using log4net;
-
- namespace CallOut
- {
- class CallCenterCom
- {
- const int DataPackLen = 102440;
- const int DataHeadLen = 5;
- const int APIVersion = 1;
-
-
- private const int DataBufferSize = 1024 * 50;
-
- private delegate void ComResEvent(string ResCode, Socket socket);
-
- public class RecEventArgs : System.EventArgs
- {
- public string vDataPack;
- }
-
- public class SocketEventArgs : System.EventArgs
- {
-
-
-
- public string ResCode;
- public string ErrorStr;
- }
-
-
-
-
- public class TcsClientCom
- {
- Socket socket;
- string host;
- int port;
- ILog log = null;
-
- private int DataBufferSizeLast = 0;
- private byte[] DataBuffer = new byte[DataBufferSize];
- private byte[] DataBufferLast;
-
- public string EndPoint
- {
- get { return host + ":" + port.ToString(); }
- }
-
- public string RemoteEndPoint
- {
- get { return socket.RemoteEndPoint.ToString(); }
- }
-
-
- RecEventArgs Rec_e = new RecEventArgs();
-
- SocketEventArgs Socket_e = new SocketEventArgs();
-
-
- public delegate void OperResEvent(object sender, RecEventArgs e);
- public event OperResEvent RecEvent;
-
-
- public delegate void ComResEvent(object sender, SocketEventArgs e);
- public event ComResEvent SocketEvent;
-
-
- public TcsClientCom(string host, int port)
- {
- this.host = host;
- this.port = port;
- log = log4net.LogManager.GetLogger("DataPack");
- }
-
- public void Connect()
- {
- Thread con = new Thread(this.Connect_Thread);
- con.IsBackground = true;
- con.Start();
- }
-
-
- public void Connect_Thread()
- {
- try
- {
-
- socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.ReceiveTimeout = 2000;
-
- Socket_e.ResCode = "10001";
- SocketEvent(this, Socket_e);
- socket.Connect(host, port);
- SocketRead();
-
- Socket_e.ResCode = "10000";
- SocketEvent(this, Socket_e);
- }
- catch (Exception Ex)
- {
- Socket_e.ResCode = "00000";
- Socket_e.ErrorStr = Ex.Message;
- SocketEvent(this, Socket_e);
- }
- }
-
- public void DisConnect()
- {
- socket.Disconnect(false);
- }
-
- public void ReConnect()
- {
- socket.Disconnect(true);
- this.Connect();
- }
-
- public void SocketRead()
- {
- Thread tRead = new Thread(Thread_Read);
- tRead.IsBackground = true;
- tRead.Start();
- }
-
-
- public void Thread_Read()
- {
- while (true)
- {
- try
- {
- if (socket.Poll(-1, SelectMode.SelectRead))
- {
-
- byte[] DataBuffer = new byte[DataBufferSize - DataBufferSizeLast];
- int receivedBytesCount = socket.Receive(DataBuffer, DataBufferSize - DataBufferSizeLast, SocketFlags.None);
-
- if (receivedBytesCount == 0)
- {
-
- Socket_e.ResCode = "00000";
- SocketEvent(this, Socket_e);
- return;
- }
-
- int iTempDataBufferSizeLast = 0;
- byte[] TempBytes = new byte[receivedBytesCount + DataBufferSizeLast];
-
- if (DataBufferLast != null)
- {
- Buffer.BlockCopy(DataBufferLast, 0, TempBytes, 0, DataBufferSizeLast);
- iTempDataBufferSizeLast = DataBufferSizeLast;
- DataBufferLast = null;
- DataBufferSizeLast = 0;
- }
- Buffer.BlockCopy(DataBuffer, 0, TempBytes, iTempDataBufferSizeLast, receivedBytesCount);
- EncodingBytes(TempBytes, receivedBytesCount + iTempDataBufferSizeLast);
-
- }
- else if (socket.Poll(-1, SelectMode.SelectError))
- {
-
- }
- }
- catch (SocketException e)
- {
-
- if (e.NativeErrorCode.Equals(10035))
- {
-
- }
- else
- {
-
- Socket_e.ResCode = e.NativeErrorCode.ToString();
- SocketEvent(this, Socket_e);
- return;
- }
- }
- }
- }
-
- private void EncodingBytes(byte[] Totalbytes, int TotalBytesCount)
- {
- try
- {
-
- if (Totalbytes.Length <= 5)
- {
- DataBufferLast = new byte[TotalBytesCount];
- Buffer.BlockCopy(Totalbytes, 0, DataBufferLast, 0, TotalBytesCount);
- return;
- }
-
-
- byte[] TempHeadBytes = new byte[4];
-
- Buffer.BlockCopy(Totalbytes, 1, TempHeadBytes, 0, 4);
-
- int TempPackLen = BitConverter.ToInt32(TempHeadBytes, 0);
-
-
- if ((TempPackLen + DataHeadLen) > TotalBytesCount)
- {
- DataBufferSizeLast = TotalBytesCount;
- DataBufferLast = new byte[TotalBytesCount];
- Buffer.BlockCopy(Totalbytes, 0, DataBufferLast, 0, TotalBytesCount);
- return;
- }
-
- byte[] TempPackBytes = new byte[TempPackLen];
- for (int i = 0; i < TempPackLen; i++)
- {
- TempPackBytes[i] = Totalbytes[i + DataHeadLen];
- }
-
- string receivedStr = System.Text.Encoding.Default.GetString(TempPackBytes);
-
- Rec_e.vDataPack = receivedStr;
- log.Info(receivedStr);
- RecEvent(this, Rec_e);
-
- if (TotalBytesCount > DataHeadLen + TempPackLen)
- {
- byte[] Bytes = new byte[TotalBytesCount - DataHeadLen - TempPackLen];
- Buffer.BlockCopy(Totalbytes, DataHeadLen + TempPackLen, Bytes, 0, TotalBytesCount - DataHeadLen - TempPackLen);
- this.EncodingBytes(Bytes, TotalBytesCount - DataHeadLen - TempPackLen);
- }
- }
-
- catch (SocketException ex)
- {
- log.Info("EncodingBytes:" + ex.Message + "ErrorCode:" + ex.ErrorCode.ToString());
- }
- catch (Exception ex)
- {
- log.Info("EncodingBytes:" + ex.Message);
- }
- }
-
-
- public bool IsConnected()
- {
- if (socket.Poll(-1, SelectMode.SelectRead))
- {
- byte[] tmp = new byte[1];
- int nRead = socket.Receive(tmp);
- if (nRead == 0)
- {
-
- return false;
- }
- }
- return true;
-
- }
-
- public bool Send(string vDataPack)
- {
-
- byte[] Ver = Encoding.Default.GetBytes(APIVersion.ToString());
-
- byte[] Len = new byte[4];
- Len = Encoding.Default.GetBytes(vDataPack.Length.ToString());
-
- byte[] msg = Encoding.Default.GetBytes(vDataPack);
-
- byte[] PackageBytes = new byte[msg.Length + DataHeadLen];
-
- byte[] b4 = Int32ToBytes((UInt32)(msg.Length));
-
- Ver.CopyTo(PackageBytes, 0);
- b4.CopyTo(PackageBytes, 1);
-
- msg.CopyTo(PackageBytes, DataHeadLen);
-
-
- log.Info(Encoding.Default.GetString(PackageBytes, 5, PackageBytes.Length - 5));
- try
- {
- int i = socket.Send(PackageBytes);
- return true;
- }
- catch
- {
- return false;
- }
- }
-
-
- public byte[] Int32ToBytes(UInt32 i)
- {
- byte[] bytes = new byte[4];
- bytes[0] = (byte)(i % 256);
- bytes[1] = (byte)(i / 256);
- bytes[2] = (byte)(i / (65536));
- bytes[3] = (byte)(i / (16777216));
- return bytes;
- }
- }
-
-
-
-
- public class TcsServerCom
- {
-
- }
-
- }
- }