C # Sending data from Socket asynchronous communication Client

Source: Internet
Author: User

C # Main program of Socket asynchronous communication client: [c-sharp] # public static int Main (String [] args) # {## IPAddress 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; # C # connection part of the Socket asynchronous communication client Callback: [c-sharp] 1. private static void ConnectCallback (IAsyncResult ar) 2. {3. 4. // obtain the socket from the state object. 5. socket client = (Socket) ar. asyncState; 6. 7. // complete the connection. 8. client. endConnect (ar); 9. 10. console. writeLine ("Socket connected to {0}", 11. client. remoteEndPoint. toString (); 12. 13. // The connection is complete and the main thread continues. 14. connectDone. set (); 15 .} C # Socket asynchronous communication client: [c-sharp] # 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 object 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) #{# // data is stored. # 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 () ;#}# // indicator signal indicating that all data has been read. # receiveDone. set () ;#}#}www.2cto. comC # send data from Socket asynchronous communication client: [c-sharp] view plaincopy www.2cto.com 1. private static void Send (Socket client, String data) 2. {3. // format conversion. 4. byte [] byteData = Encoding. ASCII. getBytes (data); 5. 6. // start sending data to a remote device. 7. client. beginSend (byteData, 0, byteData. length, 0, 8. new AsyncCallback (SendCallback), client); 9 .} 10. 11. private static void SendCallback (IAsyncResult ar) 12. {13. 14. // obtain socket 15 from the state object. socket client = (Socket) ar. asyncState; 16. 17. // complete data transmission. 18. int bytesSent = client. endSend (ar); 19. console. writeLine ("Sent {0} bytes to server. ", bytesSent); 20. 21. // indicates that the data has been sent and the main thread continues. 22. sendDone. set (); 23. 24 .}

Related Article

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.