• 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. }
     





  • 相关阅读:
    Atitit attilax要工作研究的要素 纪要 方案 趋势 方向 概念 理论
    Atitit 常见每日流程日程日常工作.docx v7 r8f
    Atitit it 互联网 软件牛人的博客列表
    Atitit 信息链(Information Chain)的概念理解 attilax总结
    Atitit 知识点的体系化 框架与方法 如何了解 看待xxx
    Atitit 聚合搜索多个微博 attilax总结
    Atitit 企业知识管理PKM与PIM
    Atitit 项目沟通管理 Atitit 沟通之道 attilax著.docx
    Atitit 项目管理软件 在线服务 attilax总结 1. 项目管理协作的历史 1 1.1. Worktile 406k 1 1.2. Teambition  584k in baidu
    Atitit.每周末总结 于每周一计划日程表 流程表 v8 import 上周遗漏日志补充 检查话费 检查流量情况 Crm问候 Crm表total and 问候
  • 原文地址:https://www.cnblogs.com/BookCode/p/5949480.html
Copyright © 2020-2023  润新知