2.1.3 send and receive data

Source: Internet
Author: User
Two Methods for sending data and receiving data can be used to Send data using the Socket-class Send method and the NetworkStream-class Write method to Receive data using the Socket-class Receive method and the NetworkStream-class Read method Socket. the following is a prototype of the Send method of the class: publicintSend (byte [] buffer) publicintSend (byte []

Two Methods for sending data and receiving data can be used to Send data using the Socket-class Send method and the NetworkStream-class Write method to Receive data using the Socket-class Receive method and the NetworkStream-class Read method Socket. the following is a prototype of the Send method of the class: public int Send (byte [] buffer) public int Send (byte []

Two Methods for sending and receiving data

You can use the Send method of the Socket class and the Write method of the NetworkStream class to Send data.

The Receive method of the Socket class and the Read method of the NetworkStream class can be used to Receive data.

The following is a prototype of the Send method of the Socket class:

Public int Send (byte [] buffer)

Public int Send (byte [] buffer, SocketFlags)

Public int Send (byte [] buffer, int size, SocketFlags)

Public int Send (byte [] buffer, int star, int end, SocketFlags)

The Receive method and Send method have the same definitions;

The Write method prototype of the NetworkStrem class:

Public override void Write (byte [] buffer, int offset, int size)

The Read and Write methods have the same definitions.

The following is the Demo code:

Socket bbb = socket. accept (); // byte [] bytee = new byte [64]; string send = "aaaaaaaaaaaaaa"; bytee = System. text. encoding. UTF8.GetBytes (send. toCharArray (); bbb. send (bytee, bytee. length, 0); // The byte [] byter = new byte [64]; bbb. receive (byter, byter. length, 0); string rec = System. text. encoding. UTF8.GetString (byter); HostInformation. appendText ("Socket received data:" + rec + "/r/n"); // NetworkStream sends data NetworkStream ns = new NetworkStream (bbb ); byte [] ccc = new byte [512]; string sendmessage = "aaaaaaaaaaaaaaaaaaaaaaaaa"; ccc = System. text. encoding. UTF8.GetBytes (sendmessage. toCharArray (); ns. write (ccc, 0, ccc. length); // NetworkStream receives Data byte [] ddd = new byte [512]; ns. read (ddd, 0, ddd. length); string readmessage = System. text. encoding. UTF8.GetString (ddd); HostInformation. appendText ("NetworkStream received data:" + readmessage + "/r/n ");

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.