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