自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C# Socket異步通訊實(shí)現(xiàn)詳解

開發(fā) 后端
C# Socket異步通訊是如何實(shí)現(xiàn)的呢?C# Socket異步通訊客戶端設(shè)計(jì)的思路是什么呢?那么本文就向你介紹具體的內(nèi)容。

C# Socket異步通訊客戶端實(shí)現(xiàn)源碼

C# Socket異步通訊客戶端之主程序:

  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() {
    // Connect to a remote device.
  30.  
  31. try {// Establish the remote endpoint for the socket.
    // The name of the
    // remote device is "host.contoso.com".
    IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
     
  32. // 生成一個(gè)TCP/IP socket.  
  33. Socket client = new Socket(AddressFamily.InterNetwork,  
  34. SocketType.Stream, ProtocolType.Tcp);  
  35.  
  36. // 與目標(biāo)終端連接.  
  37. client.BeginConnect(remoteEP,  
  38. new AsyncCallback(ConnectCallback), client);  
  39. //等待,直到連接程序完成。在ConnectCallback中適當(dāng)位置有connecDone.Set()語句  
  40. connectDone.WaitOne();  
  41.  
  42. // 發(fā)送數(shù)據(jù)到遠(yuǎn)程終端.  
  43. Send(client, "This is a test<EOF>");  
  44. sendDone.WaitOne();  
  45.  
  46. // 接收返回?cái)?shù)據(jù).  
  47. Receive(client);  
  48. receiveDone.WaitOne();  
  49.  
  50. // Write the response to the console.  
  51. Console.WriteLine("Response received : {0}", response);  
  52.  
  53. // Release the socket.  
  54. client.Shutdown(SocketShutdown.Both);  
  55. client.Close();  
  56. return 0;  

C# Socket異步通訊客戶端之連接部分Callback:

  1. private static void ConnectCallback(IAsyncResult ar)  
  2. {  
  3.  
  4. // 從state對象獲取socket.  
  5. Socket client = (Socket)ar.AsyncState;  
  6.  
  7. // 完成連接.  
  8. client.EndConnect(ar);  
  9.  
  10. Console.WriteLine("Socket connected to {0}",  
  11. client.RemoteEndPoint.ToString());  
  12.  
  13. // 連接已完成,主線程繼續(xù).  
  14. connectDone.Set();
  15. } catch (Exception e) {
  16. Console.WriteLine(e.ToString());
  17. }
  18. }

C# Socket異步通訊客戶端之?dāng)?shù)據(jù)接收:

  1.    private static void Receive(Socket client)  
  2. try {{  
  3.  
  4. // 構(gòu)造容器state.  
  5. StateObject state = new StateObject();  
  6. state.workSocket = client;  
  7.  
  8. // 從遠(yuǎn)程目標(biāo)接收數(shù)據(jù).  
  9. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,  
  10. new AsyncCallback(ReceiveCallback), state);  
  11. } catch (Exception e) {
  12. Console.WriteLine(e.ToString());
  13. }
    }
  14.  
  15. private static void ReceiveCallback(IAsyncResult ar)  
  16. {  
  17.  
  18. // 從輸入?yún)?shù)異步state對象中獲取state和socket對象  
  19. StateObject state = (StateObject)ar.AsyncState;  
  20. Socket client = state.workSocket;  
  21.  
  22. //從遠(yuǎn)程設(shè)備讀取數(shù)據(jù)  
  23. int bytesRead = client.EndReceive(ar);  
  24.  
  25. if (bytesRead > 0)  
  26. {  
  27. // 有數(shù)據(jù),存儲(chǔ).  
  28. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));  
  29.  
  30. // 繼續(xù)讀取.  
  31. client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,  
  32. new AsyncCallback(ReceiveCallback), state);  
  33. }  
  34. else 
  35. {  
  36. // 所有數(shù)據(jù)讀取完畢.  
  37. if (state.sb.Length > 1)  
  38. {  
  39. response = state.sb.ToString();  
  40. }  
  41. // 所有數(shù)據(jù)讀取完畢的指示信號.  
  42. receiveDone.Set();  
  43. }  
  44. } catch (Exception e) {
  45. Console.WriteLine(e.ToString());
  46. }
  47. }

C# Socket異步通訊客戶端之發(fā)送數(shù)據(jù):

  1. private static void Send(Socket client, String data)  
  2. {  
  3. // 格式轉(zhuǎn)換.  
  4. byte[] byteData = Encoding.ASCII.GetBytes(data);  
  5.  
  6. // 開始發(fā)送數(shù)據(jù)到遠(yuǎn)程設(shè)備.  
  7. client.BeginSend(byteData, 0, byteData.Length, 0,  
  8. new AsyncCallback(SendCallback), client);  
  9. }   
  10. private static void SendCallback(IAsyncResult ar)  
  11. {  
  12.  
  13. // 從state對象中獲取socket  
  14. Socket client = (Socket)ar.AsyncState;  
  15.  
  16. // 完成數(shù)據(jù)發(fā)送.  
  17. int bytesSent = client.EndSend(ar);  
  18. Console.WriteLine("Sent {0} bytes to server.", bytesSent);  
  19.  
  20. // 指示數(shù)據(jù)已經(jīng)發(fā)送完成,主線程繼續(xù).  
  21. sendDone.Set();  
  22. } catch (Exception e) {
  23. Console.WriteLine(e.ToString());
  24. }

  25. }
  26. public static int Main(String[] args) {
  27. StartClient();
  28. return 0;
  29. }
  30. }

C# Socket異步通訊客戶端的實(shí)現(xiàn)源碼內(nèi)容就基本向你介紹到這里,希望對你了解和學(xué)習(xí)C# Socket異步通訊有所幫助。

【編輯推薦】

  1. C#對象初始化學(xué)習(xí)總結(jié)
  2. C#改寫方法學(xué)習(xí)筆記
  3. 概述C#加框和消框
  4. 淺析C#異步操作
  5. 描述C#異步Socket

 

責(zé)任編輯:仲衡 來源: 博客園
相關(guān)推薦

2009-08-03 16:45:02

C#異步Socket

2009-08-21 10:13:02

C#異步初步

2024-03-21 08:34:49

Vue3WebSocketHTTP

2010-03-18 17:23:55

Java Socket

2010-12-22 10:21:17

C#基礎(chǔ)

2009-08-21 09:20:44

C#異步套接字

2009-08-21 11:39:58

C# Socket通信

2025-04-30 01:50:00

C#異步編程

2009-08-25 18:04:30

C#實(shí)現(xiàn)Singlet

2009-09-09 18:50:23

C# 加密RSA

2009-08-31 16:23:13

C#接口

2009-08-21 11:24:16

C#異步調(diào)用

2009-08-18 16:45:40

C# Raw Sock

2009-08-27 17:14:36

C# Socket

2009-09-09 18:57:26

C# 加密TripleDES

2009-08-25 17:43:17

C#串口監(jiān)聽

2009-08-25 10:44:50

C#實(shí)現(xiàn)多語言

2009-08-26 09:22:44

C#實(shí)現(xiàn)打印功能

2009-08-26 11:07:36

C#打印窗體

2009-08-26 12:59:08

C#打印設(shè)置
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號