菜鳥(niǎo)學(xué)堂:
開(kāi)始從客戶端套接字接收數(shù)據(jù)的 acceptcallback 方法的此節(jié)首先初始化 stateobject 類(lèi)的一個(gè)實(shí)例,然后調(diào)用 beginreceive 方法以開(kāi)始從客戶端套接字異步讀取數(shù)據(jù)。
下面的示例顯示了完整的 acceptcallback 方法。它假定以下內(nèi)容:存在一個(gè)名為 alldone 的 manualresetevent 實(shí)例,定義了 stateobject 類(lèi),以及在名為 socketlistener 的類(lèi)中定義了 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);
}
需要為異步套接字服務(wù)器實(shí)現(xiàn)的 final 方法是返回客戶端發(fā)送的數(shù)據(jù)的讀取回調(diào)方法。與接受回調(diào)方法一樣,讀取回調(diào)方法也是一個(gè) asynccallback 委托。該方法將來(lái)自客戶端套接字的一個(gè)或多個(gè)字節(jié)讀入數(shù)據(jù)緩沖區(qū),然后再次調(diào)用 beginreceive 方法,直到客戶端發(fā)送的數(shù)據(jù)完成為止。從客戶端讀取整個(gè)消息后,在控制臺(tái)上顯示字符串,并關(guān)閉處理與客戶端的連接的服務(wù)器套接字。
下面的示例實(shí)現(xiàn) readcallback 方法。它假定定義了 stateobject 類(lèi)。
[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();
}
}
同步客戶端套接字示例
下面的示例程序創(chuàng)建一個(gè)連接到服務(wù)器的客戶端。該客戶端是用同步套接字生成的,因此掛起客戶端應(yīng)用程序的執(zhí)行,直到服務(wù)器返回響應(yīng)為止。該應(yīng)用程序?qū)⒆址l(fā)送到服務(wù)器,然后在控制臺(tái)顯示該服務(wù)器返回的字符串。
[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;
}
}