C # socket asynchronous communication Client

Source: Internet
Author: User

 

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 ();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.