zoukankan      html  css  js  c++  java
  • C#中网络通信

    一、服务端代码

    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+="监听成功······ ";
    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()+" ";
    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 +" ";
    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. }

    二、客户端代码

    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+=" "+Encoding.UTF8.GetString(buffer)+" ";
    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. }
     





  • 相关阅读:
    C语言-if语句
    C语言-表达式
    C语言-基础
    Java for LeetCode 187 Repeated DNA Sequences
    Java for LeetCode 179 Largest Number
    Java for LeetCode 174 Dungeon Game
    Java for LeetCode 173 Binary Search Tree Iterator
    Java for LeetCode 172 Factorial Trailing Zeroes
    Java for LeetCode 171 Excel Sheet Column Number
    Java for LeetCode 169 Majority Element
  • 原文地址:https://www.cnblogs.com/BookCode/p/5949480.html
Copyright © 2011-2022 走看看