[c#]
public static void connect(endpoint remoteep, socket client) {
client.beginconnect(remoteep,
new asynccallback(connectcallback), client );
connectdone.waitone();
}
[c#]
private static void connectcallback(iasyncresult ar) {
try {
// retrieve the socket from the state object.
socket client = (socket) ar.asyncstate;
// complete the connection.
client.endconnect(ar);
console.writeline("socket connected to {0}",
client.remoteendpoint.tostring());
// signal that the connection has been made.
connectdone.set();
} catch (exception e) {
console.writeline(e.tostring());
}
}
[c#]
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, socketflags.none,
new asynccallback(sendcallback), client);
}
[c#]
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);
console.writeline("sent {0} bytes to server.", bytessent);
// signal that all bytes have been sent.
senddone.set();
} catch (exception e) {
console.writeline(e.tostring());
}
}
[c#]
public class stateobject {
public socket worksocket = null; // client socket.
public const int buffersize = 256; // size of receive buffer.
public byte[] buffer = new byte[buffersize]; // receive buffer.
public stringbuilder sb = new stringbuilder();// received data string.
}
[c#]
private static void receive(socket client) {
try {
// create the state object.
stateobject state = new stateobject();
state.worksocket = client;
// begin receiving the data from the remote device.
client.beginreceive( state.buffer, 0, stateobject.buffersize, 0,
new asynccallback(receivecallback), state);
} catch (exception e) {
console.writeline(e.tostring());
}
}
[c#]
private static void receivecallback( iasyncresult ar ) {
try {
// retrieve the state object and the client socket
// from the async state object.
stateobject state = (stateobject) ar.asyncstate;
socket client = state.worksocket;
// read data from the remote device.
int bytesread = client.endreceive(ar);
if (bytesread > 0) {
// there might be more data, so store the data received so far.
state.sb.append(encoding.ascii.getstring(state.buffer,0,bytesread));
// get the rest of the data.
client.beginreceive(state.buffer,0,stateobject.buffersize,0,
new asynccallback(receivecallback), state);
} else {
// all the data has arrived; put it in response.
if (state.sb.length > 1) {
response = state.sb.tostring();
}
// signal that all bytes have been received.
receivedone.set();
}
} catch (exception e) {
console.writeline(e.tostring());
}
}
[c#]
iphostentry iphostinfo = dns.resolve(dns.gethostname());
ipaddress ipaddress = iphostinfo.addresslist[0];
ipendpoint localendpoint = new ipendpoint(ipaddress, 11000);
[c#]
listener.bind(localendpoint);
listener.listen(100);
[c#]
console.writeline("waiting for a connection...");
socket handler = listener.accept();
string data = null;
while (true) {
bytes = new byte[1024];
int bytesrec = handler.receive(bytes);
data += encoding.ascii.getstring(bytes,0,bytesrec);
if (data.indexof("<eof>") > -1) {
break;
}
}
console.writeline( "text received : {0}", data);
byte[] msg = encoding.ascii.getbytes(data);
handler.send(msg);
handler.shutdown(socketshutdown.both);
handler.close();
[c#]
void acceptcallback( iasyncresult ar) {
//add the callback code here.
}
[c#]
listener.beginaccept(
new asynccallback(socketlistener.acceptcallback),
listener);
[c#]
public void startlistening() {
iphostentry iphostinfo = new dns.resolve(dns.gethostname());
ipendpoint localep = new ipendpoint(iphostinfo.addresslist[0],11000);
console.writeline("local address and port : {0}",localep.tostring());
socket listener = new socket( localep.address.addressfamily,
sockettype.stream, protocoltype.tcp );
try {
listener.bind(localep);
s.listen(10);
while (true) {
alldone.reset();
console.writeline("waiting for a connection...");
listener.beginaccept(
new asynccallback(socketlistener.acceptcallback),
listener );
alldone.waitone();
}
} catch (exception e) {
console.writeline(e.tostring());
}
console.writeline( "closing the listener...");
}
[c#]
public void acceptcallback(iasyncresult ac) {
alldone.set();
socket listener = (socket) ar.asyncstate;
socket handler = listener.endaccept(ar);
// additional code to read data goes here.
}
[c#]
public class stateobject {
public socket worksocket = null;
public const int buffersize = 1024;
public byte[] buffer = new byte[buffersize];
public stringbuilder sb = new stringbuilder();
}
新闻热点
疑难解答
图片精选