C# Socket非同步通訊用戶端之主程式:
[c-sharp]
view plaincopyprint?
- # public static int Main(String[] args)
- # {
- #
- # IPAddress ipAddress = IPAddress.Parse("192.168.1.104");
- # int port = 20000;
- # IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
- #
- # // 產生一個TCP/IP socket.
- # Socket client = new Socket(AddressFamily.InterNetwork,
- # SocketType.Stream, ProtocolType.Tcp);
- #
- # // 與目標終端串連.
- # client.BeginConnect(remoteEP,
- # new AsyncCallback(ConnectCallback), client);
- # //等待,直到串連程式完成。在ConnectCallback中適當位置有connecDone.Set()語句
- # connectDone.WaitOne();
- #
- # // 發送資料到遠程終端.
- # Send(client, "This is a test<EOF>");
- # sendDone.WaitOne();
- #
- # // 接收返回資料.
- # Receive(client);
- # receiveDone.WaitOne();
- #
- # // Write the response to the console.
- # Console.WriteLine("Response received : {0}", response);
- #
- # // Release the socket.
- # client.Shutdown(SocketShutdown.Both);
- # client.Close();
- #
- # return 0;
- #
C# Socket非同步通訊用戶端之串連部分Callback:
[c-sharp]
view plaincopyprint?
- 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. // 串連已完成,主線程繼續.
- 14. connectDone.Set();
- 15. }
C# Socket非同步通訊用戶端之資料接收:
[c-sharp]
view plaincopyprint?
- # private static void Receive(Socket client)
- # {
- #
- # // 構造容器state.
- # StateObject state = new StateObject();
- # state.workSocket = client;
- #
- # // 從遠程目標接收資料.
- # client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
- # new AsyncCallback(ReceiveCallback), state);
- #
- # }
- #
- # private static void ReceiveCallback(IAsyncResult ar)
- # {
- #
- # // 從輸入參數非同步state對象中擷取state和socket對象
- # StateObject state = (StateObject)ar.AsyncState;
- # Socket client = state.workSocket;
- #
- # //從遠程裝置讀取資料
- # int bytesRead = client.EndReceive(ar);
- #
- # if (bytesRead > 0)
- # {
- # // 有資料,儲存.
- # state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
- #
- # // 繼續讀取.
- # client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
- # new AsyncCallback(ReceiveCallback), state);
- # }
- # else
- # {
- # // 所有資料讀取完畢.
- # if (state.sb.Length > 1)
- # {
- # response = state.sb.ToString();
- # }
- # // 所有資料讀取完畢的指示訊號.
- # receiveDone.Set();
- # }
- #
- # }
C# Socket非同步通訊用戶端之發送資料:
[c-sharp]
view plaincopyprint?
- 1. private static void Send(Socket client, String data)
- 2. {
- 3. // 格式轉換.
- 4. byte[] byteData = Encoding.ASCII.GetBytes(data);
- 5.
- 6. // 開始發送資料到遠程裝置.
- 7. client.BeginSend(byteData, 0, byteData.Length, 0,
- 8. new AsyncCallback(SendCallback), client);
- 9. }
- 10.
- 11. private static void SendCallback(IAsyncResult ar)
- 12. {
- 13.
- 14. // 從state對象中擷取socket
- 15. Socket client = (Socket)ar.AsyncState;
- 16.
- 17. // 完成資料發送.
- 18. int bytesSent = client.EndSend(ar);
- 19. Console.WriteLine("Sent {0} bytes to server.", bytesSent);
- 20.
- 21. // 指示資料已經發送完成,主線程繼續.
- 22. sendDone.Set();
- 23.
- 24. }