| 開始從客戶端套接字接收數據的 acceptcallback 方法的此節首先初始化 stateobject 類的一個實例,然后調用 beginreceive 方法以開始從客戶端套接字異步讀取數據。 下面的示例顯示了完整的 acceptcallback 方法。它假定以下內容:存在一個名為 alldone 的 manualresetevent 實例,定義了 stateobject 類,以及在名為 socketlistener 的類中定義了 readcallback 方法。 [c#] public static void acceptcallback(iasyncresult ar) { // get the socket that handles the client request. socket listener = (socket) ar.asyncstate; socket handler = listener.endaccept(ar); // signal the main thread to continue. alldone.set(); // create the state object. stateobject state = new stateobject(); state.worksocket = handler; handler.beginreceive( state.buffer, 0, stateobject.buffersize, 0, new asynccallback(asynchronoussocketlistener.readcallback), state); } 需要為異步套接字服務器實現的 final 方法是返回客戶端發送的數據的讀取回調方法。與接受回調方法一樣,讀取回調方法也是一個 asynccallback 委托。該方法將來自客戶端套接字的一個或多個字節讀入數據緩沖區,然后再次調用 beginreceive 方法,直到客戶端發送的數據完成為止。從客戶端讀取整個消息后,在控制臺上顯示字符串,并關閉處理與客戶端的連接的服務器套接字。 下面的示例實現 readcallback 方法。它假定定義了 stateobject 類。 [c#] public void readcallback(iasyncresult ar) { stateobject state = (stateobject) ar.asyncstate; socket handler = state.worksocket; // read data from the client socket. int read = handler.endreceive(ar); // data was read from the client socket. if (read > 0) { state.sb.append(encoding.ascii.getstring(state.buffer,0,read)); handler.beginreceive(state.buffer,0,stateobject.buffersize, 0, new asynccallback(readcallback), state); } else { if (state.sb.length > 1) { // all the data has been read from the client; // display it on the console. string content = state.sb.tostring(); console.writeline("read {0} bytes from socket./n data : {1}", content.length, content); } handler.close(); } } 同步客戶端套接字示例 下面的示例程序創建一個連接到服務器的客戶端。該客戶端是用同步套接字生成的,因此掛起客戶端應用程序的執行,直到服務器返回響應為止。該應用程序將字符串發送到服務器,然后在控制臺顯示該服務器返回的字符串。 [c#] using system; using system.net; using system.net.sockets; using system.text; public class synchronoussocketclient { public static void startclient() { // data buffer for incoming data. byte[] bytes = new byte[1024]; // connect to a remote device. 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,11000); // create a tcp/ip socket. socket sender = new socket(addressfamily.internetwork, sockettype.stream, protocoltype.tcp ); // connect the socket to the remote endpoint. catch any errors. try { sender.connect(remoteep); console.writeline("socket connected to {0}", sender.remoteendpoint.tostring()); // encode the data string into a byte array. byte[] msg = encoding.ascii.getbytes("this is a test<eof>"); // send the data through the socket. int bytessent = sender.send(msg); // receive the response from the remote device. int bytesrec = sender.receive(bytes); console.writeline("echoed test = {0}", encoding.ascii.getstring(bytes,0,bytesrec)); // release the socket. sender.shutdown(socketshutdown.both); sender.close(); } catch (argumentnullexception ane) { console.writeline("argumentnullexception : {0}",ane.tostring()); } catch (socketexception se) { console.writeline("socketexception : {0}",se.tostring()); } catch (exception e) { console.writeline("unexpected exception : {0}", e.tostring()); } } catch (exception e) { console.writeline( e.tostring()); } } public static int main(string[] args) { startclient(); return 0; }< | 
新聞熱點
疑難解答
圖片精選