C#中網絡通訊

1、服務端代碼

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace 網絡編程.SockteHandel
  11. {
  12. /// <summary>
  13. /// 服務端處理類
  14. /// </summary>
  15. publicclassServerHandle
  16. {
  17. /// <summary>
  18. /// 端口號
  19. /// </summary>
  20. publicstaticstringPoint{ get;set;}
  21. /// <summary>
  22. /// IP地址
  23. /// </summary>
  24. publicstaticstringIpSite{ get;set;}
  25. /// <summary>
  26. /// 服務端信息
  27. /// </summary>
  28. publicstaticTextBoxServerMsg{ get;set;}
  29. /// <summary>
  30. /// 接收到客戶端的消息
  31. /// </summary>
  32. publicstaticTextBoxReceiveMsg{ get;set;}
  33. /// <summary>
  34. /// 發送消息
  35. /// </summary>
  36. publicstaticTextBoxSendMessage{ get;set;}
  37. /// <summary>
  38. /// 負責通訊的Socket
  39. /// </summary>
  40. privateSocketConnectionSocket;
  41. /// <summary>
  42. /// 監聽端口
  43. /// </summary>
  44. publicvoidWatchPort()
  45. {
  46. //在服務器端建立一個負責監聽ID跟端口號 的Socket
  47. Socket socketWatch =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  48. //IPAddress ip = IPAddress.Any; //監聽
  49. IPAddress ip =IPAddress.Parse(IpSite);
  50. //建立端口號對象
  51. IPEndPoint pointObj =newIPEndPoint(ip,Convert.ToInt32(Point));
  52. //監聽
  53. socketWatch.Bind(pointObj);
  54. ServerMsg.Text+="監聽成功······\r\n ";
  55. //設定最多十個排隊鏈接請求
  56. socketWatch.Listen(10);
  57. //開啓一個新線程
  58. Thread thread =newThread(Listen);
  59. thread.IsBackground=true;
  60. thread.Start(socketWatch);
  61. }
  62. /// <summary>
  63. /// 等待客戶端鏈接,而且建立與之通訊的Socket
  64. /// </summary>
  65. /// <param name="obj"></param>
  66. voidListen(object obj)
  67. {
  68. Socket soketlisten = obj asSocket;
  69. while(true)
  70. {
  71. //等待客戶端鏈接,並建立一個負責通訊的Socket
  72. Socket socketSend = soketlisten.Accept();
  73. ServerMsg.Text+="鏈接成功·······"+ socketSend.RemoteEndPoint.ToString()+"\r\n";
  74. //開啓一個新線程不停接收客戶端發來的消息
  75. Thread reciveThread =newThread(Recive);
  76. reciveThread.IsBackground=true;
  77. reciveThread.Start(socketSend);
  78. }
  79. }
  80. /// <summary>
  81. /// 服務器端不停接收客戶端發來的消息
  82. /// </summary>
  83. /// <param name="obj"></param>
  84. voidRecive(object obj)
  85. {
  86. //接收消息
  87. ConnectionSocket= obj asSocket;
  88. if(ConnectionSocket==null)
  89. {
  90. return;
  91. }
  92. while(true)
  93. {
  94. //接收客戶端發來信息
  95. byte[] buffer =newbyte[1024*1024*2];
  96. //實際接收的有效字節數
  97. int receiveIndex =ConnectionSocket.Receive(buffer);
  98. //以實際接收有效字節數來判斷客戶端是否下線了
  99. if(receiveIndex ==0)
  100. {
  101. break;
  102. }
  103. string str =Encoding.UTF8.GetString(buffer,0, buffer.Length);
  104. ReceiveMsg.Text+= str +"\r\n";
  105. }
  106. }
  107. /// <summary>
  108. /// 服務端發送消息
  109. /// </summary>
  110. publicvoidServerSendMessage()
  111. {
  112. byte[] buffer =Encoding.UTF8.GetBytes(SendMessage.Text);
  113. int connectionIndex =ConnectionSocket.Send(buffer);
  114. }
  115. }
  116. }

2、客戶端代碼

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace 網絡編程.SockteHandle
  11. {
  12. /// <summary>
  13. /// 客戶端
  14. /// </summary>
  15. publicclassClientHandle
  16. {
  17. /// <summary>
  18. /// IP地址
  19. /// </summary>
  20. publicstaticstringConnectionIp{ get;set;}
  21. /// <summary>
  22. /// 端口號
  23. /// </summary>
  24. publicstaticstringPoint{ get;set;}
  25. //發送消息
  26. publicTextBoxSendMsg{ get;set;}
  27. /// <summary>
  28. /// 接收消息
  29. /// </summary>
  30. publicTextBoxReciveMsg{ get;set;}
  31. /// <summary>
  32. /// 客戶端Socket對象
  33. /// </summary>
  34. publicSocket socketSend =null;
  35. /// <summary>
  36. /// 建立負責通訊的Socket
  37. /// </summary>
  38. publicvoidCreateClientSocket()
  39. {
  40. socketSend =newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  41. IPAddress ip =IPAddress.Parse(ConnectionIp);
  42. IPEndPoint endPoint =newIPEndPoint(ip,Convert.ToInt32(Point));
  43. socketSend.Connect(endPoint);
  44. ReciveMsg.Text+="鏈接到服務器";
  45. //建立一個線程來接收服務器端數據
  46. Thread reciveThread =newThread(ReciveServerMessage);
  47. reciveThread.IsBackground=true;
  48. reciveThread.Start(socketSend);
  49. }
  50. /// <summary>
  51. /// 接收服務端信息
  52. /// </summary>
  53. voidReciveServerMessage(object obj)
  54. {
  55. Socket reciveSocket = obj asSocket;
  56. while(true)
  57. {
  58. byte[] buffer =newbyte[1024*1024*2];
  59. int reciveIndex = reciveSocket.Receive(buffer);
  60. if(reciveIndex ==0)
  61. {
  62. break;
  63. }
  64. ReciveMsg.Text+="\r\n"+Encoding.UTF8.GetString(buffer)+"\r\n";
  65. }
  66. }
  67. /// <summary>
  68. /// 發送消息
  69. /// </summary>
  70. publicvoidSendMessage()
  71. {
  72. byte[] buffer =Encoding.UTF8.GetBytes(SendMsg.Text);
  73. int sendIndex = socketSend.Send(buffer);
  74. }
  75. }
  76. }
 



相關文章
相關標籤/搜索