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

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

// Create the state object. 
StateObject state = new StateObject(); 
state.workSocket = handler; 
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, 
new AsyncCallback(ReadCallback), state); 

public static void ReadCallback(IAsyncResult ar) { 
String content = String.Empty; 
// Retrieve the state object and the handler socket 
// from the asynchronous state object. 
StateObject state = (StateObject) ar.AsyncState; 
Socket handler = state.workSocket; 
// Read data from the client socket.  
int bytesRead = handler.EndReceive(ar);