国产精品电影_久久视频免费_欧美日韩国产激情_成年人视频免费在线播放_日本久久亚洲电影_久久都是精品_66av99_九色精品美女在线_蜜臀a∨国产成人精品_冲田杏梨av在线_欧美精品在线一区二区三区_麻豆mv在线看

淺析C#異步套接字的實現過程

開發 后端
本文介紹了C#異步套接字如何實現,以及C#異步套接字是如何工作。

C#異步套接字實現是如何的呢?讓我們開始從實例開始:

下面的C#異步套接字實現實例程序創建一個連接到服務器的客戶端。該客戶端是用C#異步套接字生成的,因此在等待服務器返回響應時不掛起客戶端應用程序的執行。該應用程序將字符串發送到服務器,然后在控制臺顯示該服務器返回的字符串。

  1. using System;       
  2. using System.Net;       
  3. using System.Net.Sockets;       
  4. using System.Threading;       
  5. using System.Text;       
  6. // State object for receiving data from remote device.       
  7. public class StateObject {       
  8. // Client socket.       
  9. public Socket workSocket = null;       
  10. // Size of receive buffer.       
  11. public const int BufferSize = 256;       
  12. // Receive buffer.       
  13. public byte[] buffer = new byte[BufferSize];       
  14. // Received data string.       
  15. public StringBuilder sb = new StringBuilder();       
  16. }       
  17. public class AsynchronousClient {       
  18. // The port number for the remote device.       
  19. private const int port = 11000;       
  20. // ManualResetEvent instances signal completion.       
  21. private static ManualResetEvent connectDone =       
  22. new ManualResetEvent(false);       
  23. private static ManualResetEvent sendDone =       
  24. new ManualResetEvent(false);       
  25. private static ManualResetEvent receiveDone =       
  26. new ManualResetEvent(false);       
  27. // The response from the remote device.       
  28. private static String response = String.Empty;       
  29. private static void StartClient() {       
  30. // Connect to a remote device.       
  31. try {       
  32. // Establish the remote endpoint for the socket.       
  33. // The name of the       
  34. // remote device is "host.contoso.com".       
  35. IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");       
  36. IPAddress ipAddress = ipHostInfo.AddressList[0];       
  37. IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);       
  38. // Create a TCP/IP socket.       
  39. Socket client = new Socket(AddressFamily.InterNetwork,       
  40. SocketType.Stream, ProtocolType.Tcp);       
  41. // Connect to the remote endpoint.       
  42. client.BeginConnect( remoteEP,       
  43. new AsyncCallback(ConnectCallback), client);       
  44. connectDone.WaitOne();       
  45. // Send test data to the remote device.       
  46. Send(client,"This is a test< EOF>");       
  47. sendDone.WaitOne();       
  48. // Receive the response from the remote device.       
  49. Receive(client);       
  50. receiveDone.WaitOne();       
  51. // Write the response to the console.       
  52. Console.WriteLine("Response received : {0}", response);       
  53. // Release the socket.       
  54. client.Shutdown(SocketShutdown.Both);       
  55. client.Close();       
  56. catch (Exception e) {       
  57. Console.WriteLine(e.ToString());       
  58. }       
  59. }       
  60. private static void ConnectCallback(IAsyncResult ar) {       
  61. try {       
  62. // Retrieve the socket from the state object.       
  63. Socket client = (Socket) ar.AsyncState;       
  64. // Complete the connection.       
  65. client.EndConnect(ar);       
  66. Console.WriteLine("Socket connected to {0}",       
  67. client.RemoteEndPoint.ToString());       
  68. // Signal that the connection has been made.       
  69. connectDone.Set();       
  70. catch (Exception e) {       
  71. Console.WriteLine(e.ToString());       
  72. }       
  73. }       
  74. private static void Receive(Socket client) {       
  75. try {       
  76. // Create the state object.       
  77. StateObject state = new StateObject();       
  78. state.workSocket = client;       
  79. // Begin receiving the data from the remote device.       
  80. client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,       
  81. new AsyncCallback(ReceiveCallback), state);       
  82. catch (Exception e) {       
  83. Console.WriteLine(e.ToString());       
  84. }       
  85. }       
  86. private static void ReceiveCallback( IAsyncResult ar ) {       
  87. try {       
  88. // Retrieve the state object and the client socket       
  89. // from the asynchronous state object.       
  90. StateObject state = (StateObject) ar.AsyncState;       
  91. Socket client = state.workSocket;       
  92. // Read data from the remote device.       
  93. int bytesRead = client.EndReceive(ar);       
  94. if (bytesRead > 0) {       
  95. // There might be more data, so store the data received so far.       
  96.     
  97. state.sb.Append(Encoding.ASCII.GetString(      
  98. state.buffer,0,bytesRead));       
  99. // Get the rest of the data.       
  100. client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,       
  101. new AsyncCallback(ReceiveCallback), state);       
  102. else {       
  103. // All the data has arrived; put it in response.       
  104. if (state.sb.Length > 1) {       
  105. response = state.sb.ToString();       
  106. }       
  107. // Signal that all bytes have been received.       
  108. receiveDone.Set();       
  109. }       
  110. catch (Exception e) {       
  111. Console.WriteLine(e.ToString());       
  112. }       
  113. }       
  114. private static void Send(Socket client, String data) {       
  115. // Convert the string data to byte data using ASCII encoding.       
  116. byte[] byteData = Encoding.ASCII.GetBytes(data);       
  117. // Begin sending the data to the remote device.       
  118. client.BeginSend(byteData, 0, byteData.Length, 0,       
  119. new AsyncCallback(SendCallback), client);       
  120. }       
  121. private static void SendCallback(IAsyncResult ar) {       
  122. try {       
  123. // Retrieve the socket from the state object.       
  124. Socket client = (Socket) ar.AsyncState;       
  125. // Complete sending the data to the remote device.       
  126. int bytesSent = client.EndSend(ar);       
  127. Console.WriteLine("Sent {0} bytes to server.", bytesSent);       
  128. // Signal that all bytes have been sent.       
  129. sendDone.Set();       
  130. catch (Exception e) {       
  131. Console.WriteLine(e.ToString());       
  132. }       
  133. }       
  134. public static int Main(String[] args) {       
  135. StartClient();       
  136. return 0;       
  137. }       
  138. }    

C#異步套接字在服務器的示例 下面的示例程序創建一個接收來自客戶端的連接請求的服務器。該服務器是用C#異步套接字生成的

因此在等待來自客戶端的連接時不掛起服務器應用程序的執行。該應用程序接收來自客戶端的字符串

在控制臺顯示該字符串,然后將該字符串回顯到客戶端。來自客戶端的字符串必須包含字符串“”

以發出表示消息結尾的信號。

  1. using System;       
  2. using System.Net;       
  3. using System.Net.Sockets;       
  4. using System.Text;       
  5. using System.Threading;       
  6. // State object for reading client data asynchronously       
  7. public class StateObject {       
  8. // Client socket.       
  9. public Socket workSocket = null;       
  10. // Size of receive buffer.       
  11. public const int BufferSize = 1024;       
  12. // Receive buffer.       
  13. public byte[] buffer = new byte[BufferSize];       
  14. // Received data string.       
  15. public StringBuilder sb = new StringBuilder();       
  16. }       
  17. public class AsynchronousSocketListener {       
  18. // Thread signal.       
  19. public static ManualResetEvent allDone =       
  20. new ManualResetEvent(false);       
  21. public AsynchronousSocketListener() {       
  22. }       
  23. public static void StartListening() {       
  24. // Data buffer for incoming data.       
  25. byte[] bytes = new Byte[1024];       
  26. // Establish the local endpoint for the socket.       
  27. // The DNS name of the computer       
  28. // running the listener is "host.contoso.com".       
  29. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());       
  30. IPAddress ipAddress = ipHostInfo.AddressList[0];       
  31. IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);       
  32. // Create a TCP/IP socket.       
  33. Socket listener = new Socket(AddressFamily.InterNetwork,       
  34. SocketType.Stream, ProtocolType.Tcp );       
  35. // Bind the socket to the local       
  36. //endpoint and listen for incoming connections.       
  37. try {       
  38. listener.Bind(localEndPoint);       
  39. listener.Listen(100);       
  40. while (true) {       
  41. // Set the event to nonsignaled state.       
  42. allDone.Reset();       
  43. // Start an asynchronous socket to listen for connections.       
  44. Console.WriteLine("Waiting for a connection...");       
  45. listener.BeginAccept(       
  46. new AsyncCallback(AcceptCallback),       
  47. listener );       
  48. // Wait until a connection is made before continuing.       
  49. allDone.WaitOne();       
  50. }       
  51. catch (Exception e) {       
  52. Console.WriteLine(e.ToString());       
  53. }       
  54. Console.WriteLine("\nPress ENTER to continue...");       
  55. Console.Read();       
  56. }       
  57. public static void AcceptCallback(IAsyncResult ar) {       
  58. // Signal the main thread to continue.       
  59. allDone.Set();       
  60. // Get the socket that handles the client request.       
  61. Socket listener = (Socket) ar.AsyncState;       
  62. Socket handler = listener.EndAccept(ar);       
  63. // Create the state object.       
  64. StateObject state = new StateObject();       
  65. state.workSocket = handler;       
  66. handler.BeginReceive( state.buffer,       
  67. 0, StateObject.BufferSize, 0,       
  68. new AsyncCallback(ReadCallback), state);       
  69. }       
  70. public static void ReadCallback(IAsyncResult ar) {       
  71. String content = String.Empty;       
  72. // Retrieve the state object and the handler socket       
  73. // from the asynchronous state object.       
  74. StateObject state = (StateObject) ar.AsyncState;       
  75. Socket handler = state.workSocket;       
  76. // Read data from the client socket.       
  77. int bytesRead = handler.EndReceive(ar);       
  78. if (bytesRead > 0) {       
  79. // There might be more data, so store the data received so far.       
  80. state.sb.Append(Encoding.ASCII.GetString(       
  81. state.buffer,0,bytesRead));       
  82. // Check for end-of-file tag. If it is not there, read       
  83. // more data.       
  84. content = state.sb.ToString();       
  85. if (content.IndexOf("< EOF>") > -1) {       
  86. // All the data has been read from the       
  87. // client. Display it on the console.       
  88. Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",       
  89. content.Length, content );       
  90. // Echo the data back to the client.       
  91. Send(handler, content);       
  92. else {       
  93. // Not all data received. Get more.       
  94. handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,       
  95. new AsyncCallback(ReadCallback), state);       
  96. }       
  97. }       
  98. }       
  99. private static void Send(Socket handler, String data) {       
  100. // Convert the string data to byte data using ASCII encoding.       
  101. byte[] byteData = Encoding.ASCII.GetBytes(data);       
  102. // Begin sending the data to the remote device.       
  103. handler.BeginSend(byteData, 0, byteData.Length, 0,       
  104. new AsyncCallback(SendCallback), handler);       
  105. }       
  106. private static void SendCallback(IAsyncResult ar) {       
  107. try {       
  108. // Retrieve the socket from the state object.       
  109. Socket handler = (Socket) ar.AsyncState;       
  110. // Complete sending the data to the remote device.       
  111. int bytesSent = handler.EndSend(ar);       
  112. Console.WriteLine("Sent {0} bytes to client.", bytesSent);       
  113. handler.Shutdown(SocketShutdown.Both);       
  114. handler.Close();       
  115. catch (Exception e) {       
  116. Console.WriteLine(e.ToString());       
  117. }       
  118. }       
  119. public static int Main(String[] args) {       
  120. StartListening();       
  121. return 0;       
  122. }       
  123. }    

C#異步套接字的相關內容就向你介紹到這里,希望對你了解和學習C#異步套接字有所幫助。

【編輯推薦】

  1. 總結C#語言命名規范
  2. C#反射相關知識學習
  3. 大話F#和C#:是否會重蹈C#失敗的覆轍?
  4. 總結和學習C#接口
  5. 學習C#程序有感
責任編輯:book05 來源: IT專家網
相關推薦

2009-08-21 09:20:44

C#異步套接字

2009-03-10 13:59:41

C#套接字編程

2009-08-17 13:34:02

C#異步操作

2009-08-21 11:24:16

C#異步調用

2009-08-17 14:36:15

C#進度條實現

2009-08-20 17:30:56

C#異步編程模式

2009-08-13 17:44:34

C# using關鍵字

2009-08-20 17:47:54

C#異步編程模式

2009-08-20 18:47:19

C#異步通信

2009-09-02 17:24:44

C#關機代碼

2009-09-07 09:36:29

C# DisposeDispose方法

2009-08-26 09:54:45

C#打印預覽C#打印

2009-08-20 18:37:52

委托C#異步委托

2009-08-21 11:31:59

異步和多線程的區別

2009-09-01 18:29:24

C#實現多個接口

2009-08-31 16:48:02

C#實現IDispos

2009-09-02 15:34:37

C#實現插件構架

2009-09-03 09:44:02

DropDownLisC#遞歸

2009-09-07 14:00:57

C#抓取網頁

2009-08-27 18:09:49

C#接口的實現
點贊
收藏

51CTO技術棧公眾號

精品国产伦理网| 久久资源av| 男女羞羞视频教学| 久久精品主播| 国产精品白丝jk喷水视频一区| 婷婷综合六月| 在线观看91av| 亚洲人性生活视频| 亚洲欧美另类久久久精品| 欧美精品一区免费| 国产在线国偷精品产拍免费yy| 国产一区不卡在线观看| 国产精品91一区二区三区| 欧美在线视频观看| 国产成人精品福利| 欧美猛少妇色xxxxx| 曰本一区二区| 久久激情五月丁香伊人| 精品成人av| 亚洲精品一区二区久| 久久99亚洲网美利坚合众国| 欧美午夜在线一二页| 男女av在线| 色屁屁一区二区| 三级视频在线播放| 欧美在线免费视屏| 尤物网址在线观看| 欧美一区二区精美| 538视频在线| 亚洲免费伊人电影在线观看av| 2001个疯子在线观看| 欧美一级爆毛片| a视频在线观看免费| 国产日韩精品在线播放| 激情小说网站亚洲综合网| 91精品黄色| 午夜爽爽视频| 欧美日韩激情| 亚洲一区在线不卡| 国产高清成人在线| 韩日视频在线观看| 不卡免费追剧大全电视剧网站| 国产极品尤物在线| 国产蜜臀97一区二区三区| 成人一级片网站| 国产精品国产自产拍高清av| 久草影视在线| 51午夜精品国产| 欧洲一区二区三区| 一区二区成人精品| 国产精品白丝一区二区三区| 国产精品久久久久一区二区 | 成人午夜电影免费在线观看| 久久不射中文字幕| 久久久久久久久久久99| 亚洲欧美视频一区| 尤物网在线观看| 中文字幕精品视频| 国产精品一区二区av交换| av色综合网| 狠狠色综合色综合网络| 中文字幕在线导航| 日本高清不卡在线观看| 欧美裸体视频| 日韩美女在线看| 日韩精品五月天| 色诱视频在线观看| 在线观看av一区| 日韩综合av| 99re6在线| 99麻豆久久久国产精品免费| 欧美变态视频| 亚洲精品日韩在线| 国产精品亚洲二区| 亚洲日本japanese丝袜| 亚洲三级小视频| 美女尤物在线视频| 97超级碰在线看视频免费在线看| 宅男噜噜噜66国产日韩在线观看| 日韩av片在线看| 91精品国产aⅴ一区二区| 欧美成人午夜77777| 正在播放91九色| 性做久久久久久久免费看| 日本韩国欧美| 国产精品久久久久久久久久直播 | 岛国一区二区三区高清视频| 国产91高潮流白浆在线麻豆| 在线视频三级| 久久国产精品久久久| 亚洲大胆视频| xx欧美撒尿嘘撒尿xx| 亚洲精品不卡在线| 欧美激情91| 在线观看国产一级片| 日韩精品一区二区三区视频| 精品国产精品久久一区免费式| 欧美a级免费视频| 欧美日韩一区二区不卡| 香蕉久久夜色精品国产更新时间| 大桥未久一区二区三区| 欧美日韩精品一区二区在线播放 | 中文字幕久久久av一区| 亚洲精品在线二区| 久久久性生活视频| 欧美丰满嫩嫩电影| 欧美精品一区二区久久| 黄www在线观看| 亚洲天堂av在线播放| 国产视频一区三区| 超碰超碰在线| 国产精品网址在线| 免费精品视频在线| 99sesese| 九九热99久久久国产盗摄| 日韩成人一级| 久久伊人资源站| 成人免费高清在线观看| 综合图区亚洲| 国产精品无码av无码| 色偷偷久久一区二区三区| 精品国产乱码一区二区三区| 亚洲免费精品视频| 欧美日韩日日摸| 久久影视一区| 1024在线视频| 欧美伊久线香蕉线新在线| www激情久久| 亚洲爽爆av| 各处沟厕大尺度偷拍女厕嘘嘘| 亚洲男人av在线| 精品一区二区三区久久| 毛片在线网站| 国产日韩欧美大片| 久草精品在线| 在线欧美一级视频| 亚洲成人久久久久| 狂野欧美一区| 国产原创精品在线| 日韩的一区二区| 日韩毛片视频| 成人免费视频| 国产成人精品在线视频| 91在线视频播放| 美女隐私在线观看| 国产超碰91| 欧洲色大大久久| 亚洲一二三区视频| 久久精品影视大全| 久久久久久久久久久免费| 国产三级精品三级| 啪啪激情综合网| 一级网站免费观看| 91免费看国产| 欧美日韩国产小视频在线观看| 国产精品综合色区在线观看| 日本孕妇大胆孕交无码| 日本黄色a视频| 亚洲色图激情小说| 久久久久久黄色| 日韩大片在线免费观看| 一级片在线视频| 国模精品娜娜一二三区| 亚洲国产精品va在线观看黑人| 国产成人小视频| 亚洲超碰在线观看| 国产精品久久久久白浆| 精品高清视频| 亚洲欧美在线播放| 中文字幕精品—区二区四季| 日韩欧美不卡| 2024最新电影在线免费观看| 韩国无码av片在线观看网站| 欧美黑人xxx| 日韩欧美主播在线| 老色鬼精品视频在线观看播放| 9999在线精品视频| 免费看成年人视频在线观看| 欧美理论一区二区| 久久精品国产成人| 懂色av影视一区二区三区| 日本欧美在线观看| 成人盗摄视频| 最新电影电视剧在线观看免费观看| 中文字幕剧情在线观看一区| 久久久久久久久中文字幕| 在线免费观看视频一区| 国产99久久久久| 成人羞羞视频播放网站| 男女免费观看在线爽爽爽视频| 逼特逼视频在线| 1卡2卡3卡精品视频| 亚洲视频777| 天涯成人国产亚洲精品一区av| 麻豆成人91精品二区三区| 另类图片第一页| 久草在线视频网站| 午夜免费性福利| 精品无码国产一区二区三区av| 91精品视频在线播放|