Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123 строки
3.6 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. cb.Items.Add("127.0.0.1");
  38. }
  39. #region 开启服务器
  40. public void OpenServer(string ipAddr, int port)
  41. {
  42. IPP = new IPEndPoint(IPAddress.Parse(ipAddr), port);
  43. socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  44. socket.Bind(IPP);
  45. socket.Listen(0);
  46. SocketExist = true;
  47. }
  48. #endregion
  49. #region 关闭服务器
  50. public void CloseServer()
  51. {
  52. if (socketClient != null)
  53. socketClient.Close();
  54. if (socket != null)
  55. socket.Close();
  56. SocketExist = false;
  57. }
  58. #endregion
  59. #region 接收客户端连接
  60. /// <summary>
  61. /// 接收客户端连接
  62. /// </summary>
  63. public string AcceptConn()
  64. {
  65. try
  66. {
  67. socketClient = socket.Accept(); // 阻塞等待客户端连接
  68. return socketClient.RemoteEndPoint.ToString() + " 已连接 \n";
  69. }
  70. catch (Exception)
  71. {
  72. return null;
  73. }
  74. }
  75. #endregion
  76. const int recvTimeOut = 3000; // 设置接收超时时间
  77. const int recvLength = 65535;
  78. #region 接收数据
  79. /// <summary>
  80. /// 接收数据
  81. /// </summary>
  82. public string AcpMsg()
  83. {
  84. byte[] dataRecv = new byte[recvLength]; // 定义接收数组
  85. string reEdPoint = "";
  86. try
  87. {
  88. reEdPoint = socketClient.RemoteEndPoint.ToString();
  89. socketClient.ReceiveTimeout = recvTimeOut;
  90. socketClient.Receive(dataRecv);
  91. var cs = BinaryED.Deserialize<Info.ClientSend>(dataRecv);
  92. Info.ServerSend ss = DataAnalyze.ClientSendAnalyze(cs);
  93. byte[] dataSend = BinaryED.Serialize<Info.ServerSend>(ss);
  94. socketClient.Send(dataSend);
  95. return reEdPoint + " 断开连接 \n";
  96. }
  97. catch // 客户端断开连接
  98. {
  99. if (socketClient != null)
  100. return reEdPoint + " 断开连接 \n";
  101. else
  102. return null;
  103. }
  104. finally
  105. {
  106. if (socketClient != null)
  107. socketClient.Close();
  108. }
  109. }
  110. #endregion
  111. }
  112. }