123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Security;
- using System.Text;
- using System.Threading;
- using ZLPlugin_LisPacs_MR.Domain.Units;
- namespace ZLPlugin_LisPacs_MR
- {
- /// <summary>
- /// socket客户端
- /// </summary>
- public class SocketClient
- {
- public static string Message = null;//接收消息
- /// <summary>
- /// 客户端socket
- /// </summary>
- public Socket _ClientSocket = null;
- /// <summary>
- /// IP地址
- /// </summary>
- public string ipAddress = string.Empty;
- /// <summary>
- /// IP端口
- /// </summary>
- public int Port;
- /// <summary>
- /// 接收线程
- /// </summary>
- private Thread threadReceive;
- /// <summary>
- /// 是否运行
- /// </summary>
- private bool IsRun = false;
- /// <summary>
- /// 远端地址
- /// </summary>
- private string remoteEndPoint;
- /// <summary>
- /// 本端地址
- /// </summary>
- private string LocalEndPoint;
- /// <summary>
- /// 客户端
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="Port"></param>
- public SocketClient(string ip,int Port)
- {
- _ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- _ClientSocket.ReceiveTimeout = 60000;
- this.ipAddress = ip;
- this.Port = Port;
- Start();
- }
- /// <summary>
- /// 启动连接服务端
- /// </summary>
- public void Start()
- {
- try
- {
- _ClientSocket.Connect(new IPEndPoint(IPAddress.Parse(this.ipAddress), this.Port));//通过IP和端口号来定位一个所要连接的服务器端
- //客户端网络结点号
- remoteEndPoint = _ClientSocket.RemoteEndPoint.ToString();
- LocalEndPoint = _ClientSocket.LocalEndPoint.ToString();
- Log.Info($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint}");
- //threadReceive = new Thread(Receive);
- //threadReceive.IsBackground = true;
- //threadReceive.Start();
- /* remoteEndPoint = _ClientSocket.RemoteEndPoint.ToString();
- LocalEndPoint = _ClientSocket.LocalEndPoint.ToString();
- //Console.WriteLine($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint}");
- threadReceive = new Thread(Receive);
- threadReceive.IsBackground = true;
- threadReceive.Start();*/
- IsRun = true;
- }
- catch (Exception ex)
- {
- Log.Info("平台插件未运行!请启动插件。", true);
- Log.Info(ex.Message, true);
- Log.Info(ex.StackTrace, true);
- }
- }
- /// <summary>
- /// 接收服务端发送过来的消息
- /// </summary>
- public void Receive()
- {
- //string message = "";
- try
- {
- byte[] TempData = new byte[1024 * 10];
- while (IsRun)
- {
- //传递一个byte数组,用于接收数据。length表示接收了多少字节的数据
- int length = _ClientSocket.Receive(TempData);
- if (length == 0)
- {
- IsRun = false;
- //Console.WriteLine("服务器断开链接");
- break;
- }
- else
- {
- Message = Encoding.Default.GetString(TempData, 0, length);//只将接收到的数据进行转化
- //Console.WriteLine($"远端地址:{remoteEndPoint} 本端地址:{LocalEndPoint} 获取到的数据:{message}");
- //Log.Info("出参:" + Message);
- }
- }
- }
- catch (Exception ex)
- {
- //Log.Info("95478");
- Log.Info(ex.Message);
- //Log.Info(ex.StackTrace, true);
- }
- }
- /// <summary>
- /// 循环接收消息 直到有数据
- /// </summary>
- public void WhileReceive(int waittingdate)
- {
- byte[] buffer = new byte[1024*100];
- int receivedBytes = 0;
- var begintime = DateTime.Now.Ticks;
- TimeSpan timeSpan = new TimeSpan();
- bool flag = true;
- Log.Info("WhileReceive 开始接收消息》》");
- while (flag)
- {
- Log.Info("flag=" + flag);
- // 只循环 20 秒
- timeSpan = new TimeSpan(DateTime.Now.Ticks - begintime);
- Log.Info("timeSpan=" + timeSpan);
- if (timeSpan.TotalSeconds > waittingdate)
- {
- flag = false;
- Log.Info("flag=" + flag);
- }
-
- while (flag)
- {
- try
- {
- //传递一个byte数组,用于接收数据。length表示接收了多少字节的数据
- receivedBytes = _ClientSocket.Receive(buffer);
- }catch (ArgumentNullException e)
- {
- Log.Info("获取数据超时 ArgumentNullException " + e.Message);
- break;
- }
- catch (SocketException e)
- {
- Log.Info("获取数据超时 SocketException" + e.Message);
- break;
- }
- catch (ObjectDisposedException e)
- {
- Log.Info("获取数据超时 ObjectDisposedException" + e.Message);
- break;
- }
- catch (SecurityException e)
- {
- Log.Info("获取数据超时 SecurityException" + e.Message);
- break;
- }
- }
- Log.Info("receivedBytes " + receivedBytes);
- if (receivedBytes > 0)
- {
- Message = Encoding.Default.GetString(buffer, 0, receivedBytes);//只将接收到的数据进行转化
- Log.Info("WhileReceive 返回消息》》"+Message);
- break;
- }
- }
- }
- /// <summary>
- /// 发送消息到服务端
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public int Send(string data)
- {
- Message = null;
- return _ClientSocket.Send(Encoding.Default.GetBytes(data));
- }
- public void Close()
- {
- IsRun = false;
-
- if (threadReceive != null)
- {
- try
- {
- threadReceive.Interrupt();
- Thread.Sleep(200);
- }
- catch (Exception ex)
- {
- Log.Info(ex.Message, true);
- Log.Info(ex.StackTrace, true);
- }
- threadReceive = null;
- }
- if (_ClientSocket != null)
- {
- _ClientSocket.Close();
- }
- }
- }
- }
|