MasterProgram:
Code
Public Static Int Main (string [] ARGs)
{
IPaddress = IPaddress. parse ( " 192.168.1.104 " );
int port = 20000 ;
ipendpoint remoteep = New ipendpoint (IPaddress, port);
// Generate a TCP/IP socket.
Socket Client = New socket (addressfamily. interNetwork,
sockettype. stream, protocoltype. TCP);
// connect to the target terminal.
client. beginconnect (remoteep,
New asynccallback (connectcallback), client);
// wait until the Connection Program is complete. Connecdone is available in connectcallback. set () Statement
connectdone. waitone ();
//Send data to a remote terminal.
Send (client,"This is a test <EOF>");
Senddone. waitone ();
//Receives the returned data.
Receive (client );
Receivedone. waitone ();
//Write the response to the console.
Console. writeline ("Response received ed: {0}", Response );
//Release the socket.
Client. Shutdown (socketshutdown. Both );
Client. Close ();
Return 0;
}
Connection part callback:
Code
Private Static Void Connectcallback (iasyncresult AR)
{
//Obtain 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 ());
//The connection is complete and the main thread continues.
Connectdone. Set ();
}
Data Receiving:
Code
Private Static Void Receive (Socket Client)
{
// construct the container state.
stateobject state = New stateobject ();
state. worksocket = client;
// receives data from a remote target.
client. beginreceive (state. buffer, 0 , stateobject. buffersize, 0 ,
New asynccallback (effececallback ), state);
}
private static void effececallback (iasyncresult AR)
{
// obtain the state and socket objects from the asynchronous State object of the input parameter
stateobject state = (stateobject) ar. asyncstate;
Socket Client = state. worksocket;
// read data from a remote device
int bytesread = client. endreceive (AR);
If (bytesread > 0 )
{< br> /// data, storage.
state. SB. append (encoding. ASCII. getstring (state. buffer, 0 , bytesread ));
// Continue reading.
Client. beginreceive (State. buffer, 0 , Stateobject. buffersize, 0 ,
New Asynccallback (effececallback), State );
}
Else
{
// All data has been read.
If (State. sb. Length > 1 )
{
Response = State. sb. tostring ();
}
// Indicates that all data has been read.
Receivedone. Set ();
}
}
Send data:
Code
Private Static Void Send (Socket Client, string data)
{
// Format conversion.
Byte [] Bytedata = Encoding. ASCII. getbytes (data );
// Start sending data to a remote device.
client. beginsend (bytedata, 0 , bytedata. length, 0 ,
New asynccallback (sendcallback ), client);
}
Private Static VoidSendcallback (iasyncresult AR)
{
//Obtain the socket from the State object
Socket Client=(Socket) Ar. asyncstate;
// data transmission is completed.
int bytessent = client. endsend (AR);
console. writeline ( " sent {0} bytes to server. " , bytessent );
//Indicates that the data has been sent and the main thread continues.
Senddone. Set ();
}