c#(Socket)异步套接字代码示例

2019-12-26 11:15:51于海丽


private static void Send(Socket client, String data) { 
// Convert the string data to byte data using ASCII encoding. 
byte[] byteData = Encoding.ASCII.GetBytes(data); 
// Begin sending the data to the remote device. 
client.BeginSend(byteData, 0, byteData.Length, 0, 
new AsyncCallback(SendCallback), client); 

private static void SendCallback(IAsyncResult ar) { 
try { 
// Retrieve the socket from the state object. 
Socket client = (Socket) ar.AsyncState; 
// Complete sending the data to the remote device. 
int bytesSent = client.EndSend(ar);