日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#Socket異步通訊實(shí)現(xiàn)詳解

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

達(dá)拉特網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(liá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. 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);
  31. // 生成一個(gè)TCP/IP socket.
  32. Socket client = new Socket(AddressFamily.InterNetwork,
  33. SocketType.Stream, ProtocolType.Tcp);
  34. // 與目標(biāo)終端連接.
  35. client.BeginConnect(remoteEP,
  36. new AsyncCallback(ConnectCallback), client);
  37. //等待,直到連接程序完成。在ConnectCallback中適當(dāng)位置有connecDone.Set()語句
  38. connectDone.WaitOne();
  39. // 發(fā)送數(shù)據(jù)到遠(yuǎn)程終端.
  40. Send(client, "This is a test");
  41. sendDone.WaitOne();
  42. // 接收返回?cái)?shù)據(jù).
  43. Receive(client);
  44. receiveDone.WaitOne();
  45. // Write the response to the console.
  46. Console.WriteLine("Response received : {0}", response);
  47. // Release the socket.
  48. client.Shutdown(SocketShutdown.Both);
  49. client.Close();
  50. return 0;
  51. }

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

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

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

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

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. // 開始發(fā)送數(shù)據(jù)到遠(yuǎn)程設(shè)備.
  6. client.BeginSend(byteData, 0, byteData.Length, 0,
  7. new AsyncCallback(SendCallback), client);
  8. }  
  9. private static void SendCallback(IAsyncResult ar)
  10. {
  11. // 從state對(duì)象中獲取socket
  12. Socket client = (Socket)ar.AsyncState;
  13. // 完成數(shù)據(jù)發(fā)送.
  14. int bytesSent = client.EndSend(ar);
  15. Console.WriteLine("Sent {0} bytes to server.", bytesSent);
  16. // 指示數(shù)據(jù)已經(jīng)發(fā)送完成,主線程繼續(xù).
  17. sendDone.Set();
  18. }
  19. } catch (Exception e) {
  20. Console.WriteLine(e.ToString());
  21. }

  22. }
  23. public static int Main(String[] args) {
  24. StartClient();
  25. return 0;
  26. }
  27. }

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


網(wǎng)站題目:C#Socket異步通訊實(shí)現(xiàn)詳解
轉(zhuǎn)載注明:http://m.5511xx.com/article/ccseohd.html