No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

124 líneas
3.8 KiB

  1. /* Describtion : Class for Tcp Network Connection
  2. * Company : Wuxi Xinje
  3. * Author : Somuns
  4. * DateTime : 2021/1/18
  5. */
  6. using System;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Windows.Forms;
  10. namespace StuMgmLib.MyNameSpace
  11. {
  12. // 还有一种验证连接方式: Token
  13. public class TcpConn
  14. {
  15. private IPEndPoint IPP = null;
  16. private Socket socket = null;
  17. private Socket socketClient = null;
  18. private bool my_SocketExist = false;
  19. /// <summary>
  20. /// 判断服务器开关
  21. /// </summary>
  22. public bool SocketExist
  23. {
  24. get { return my_SocketExist; }
  25. private set { my_SocketExist = value; }
  26. }
  27. public void GetIPAddress(ComboBox cb)
  28. {
  29. cb.Items.Clear();
  30. foreach (IPAddress ipAddr in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
  31. {
  32. if (ipAddr.AddressFamily.ToString() == "InterNetwork")
  33. {
  34. cb.Items.Add(ipAddr.ToString());
  35. }
  36. }
  37. if (cb.FindString("127.0.0.1") == -1)
  38. cb.Items.Add("127.0.0.1");
  39. }
  40. #region 开启服务器
  41. public void OpenServer(string ipAddr, int port)
  42. {
  43. IPP = new IPEndPoint(IPAddress.Parse(ipAddr), port);
  44. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  45. socket.Bind(IPP);
  46. socket.Listen(0);
  47. SocketExist = true;
  48. }
  49. #endregion
  50. #region 关闭服务器
  51. public void CloseServer()
  52. {
  53. if (socketClient != null)
  54. socketClient.Close();
  55. if (socket != null)
  56. socket.Close();
  57. SocketExist = false;
  58. }
  59. #endregion
  60. #region 接收客户端连接
  61. /// <summary>
  62. /// 接收客户端连接
  63. /// </summary>
  64. public string AcceptConn()
  65. {
  66. try
  67. {
  68. socketClient = socket.Accept(); // 阻塞等待客户端连接
  69. return DateTime.Now.ToLongTimeString() + " : " + socketClient.RemoteEndPoint.ToString() + " 已连接 \n";
  70. }
  71. catch (Exception)
  72. {
  73. return null;
  74. }
  75. }
  76. #endregion
  77. const int recvTimeOut = 3000; // 设置接收超时时间
  78. const int recvLength = 65535;
  79. #region 接收数据
  80. /// <summary>
  81. /// 接收数据
  82. /// </summary>
  83. public string AcpMsg()
  84. {
  85. byte[] dataRecv = new byte[recvLength]; // 定义接收数组
  86. string reEdPoint = "";
  87. try
  88. {
  89. reEdPoint = socketClient.RemoteEndPoint.ToString();
  90. socketClient.ReceiveTimeout = recvTimeOut;
  91. socketClient.Receive(dataRecv);
  92. var cs = BinaryED.Deserialize<Info.ClientSend>(dataRecv);
  93. Info.ServerSend ss = DataAnalyze.ClientSendAnalyze(cs);
  94. byte[] dataSend = BinaryED.Serialize<Info.ServerSend>(ss);
  95. socketClient.Send(dataSend);
  96. return DateTime.Now.ToLongTimeString() + " : " + reEdPoint + " 断开连接 \n";
  97. }
  98. catch // 客户端断开连接
  99. {
  100. if (socketClient != null)
  101. return DateTime.Now.ToLongTimeString() + " : " + reEdPoint + " 断开连接 \n";
  102. else
  103. return null;
  104. }
  105. finally
  106. {
  107. if (socketClient != null)
  108. socketClient.Close();
  109. }
  110. }
  111. #endregion
  112. }
  113. }