WebSocket method for transmitting more than 126 bytes of data

Source: Internet
Author: User


Previously, the WebSocket server was implemented by using the Java socket to simulate the HTTP transmission and successfully shook with the client.

http://shuxiayeshou.blog.51cto.com/4452347/1762152


However, one problem is that transferring data from the server to more than 126 bytes is not possible, the connection is automatically disconnected also reported error

InputStream In=socket.getinputstream (); OutputStream Out=socket.getoutputstream (); byte[] Buff=new Byte[3072];int    Hasread=0;while (Hasread=in.read (Buff) >0) {byte[] pushhead=new byte[2];    Pass the data header String x= "data to be transferred ..." before passing the data.            PUSHHEAD[0]=BUFF[0];    The first byte of the data header array is the first byte of the data to be received//the second byte is the byte array length to transfer the data into bytes pushhead[1]= (byte) x.getbytes ("UTF-8"). Length;            Out.write (Pushhead);  First pass the data header Out.write (x.getbytes ("UTF-8"); Pass Data Again}

The above code is OK when the passed data is converted to a byte array with a length less than 126, but the error is >=126.


Because the byte range is -128~127, the transmitted data length is greater than 125 to error


It's going to be a different approach.

Transmission data length is divided into three cases, that is, there are three kinds of range

    1. Less than 126, then the above code is OK.

    2. Greater than 125 is less than 65536 then the second byte of the data header is (byte) 126, and the third fourth byte is the data length

    3. Greater than 65535 data header the second byte is stored (byte) 127, followed by 8 bytes of stored data length


This is said in the plane, look at the code bar (the code is also flat ( ╯°д°) ╯ (┻━┻)


Inputstream in=socket.getinputstream (); Outputstream out=socket.getoutputstream (); byte[] buff= New byte[3072];int hasread=0;while (Hasread=in.read (Buff) >0) {    byte[]  pushhead;    //Pass this data header before passing the data     string x= "data to be transferred ...";         int length=x.getbytes ("UTF-8") .length;         if (length<126) {        pushhead=new  byte[2];        pushhead[0]=buff[0];    // The first byte of the data header array is the first byte of the received data         //the second byte is the byte array length to transfer the data into bytes          pushhead[1]= (Byte) x.getbytes ("UTF-8") .length;         out.write (Pushhead);             //first pass the data header   &nbsP; }    else if (length>125 && length<65536) {         pushHead=new Byte[4];         Pushhead[0]=buff[0];        pushhead[1]= (Byte) 126;         pushhead[2]= (Byte) ((length>>8)  & 0xff);         pushhead[3]= (Byte) (LENGTH&NBSP;&AMP;&NBSP;0XFF);         out.write (Pushhead);     }    else if (length> 65535) {        pushhead=new byte[4];         pushhead[0]=buff[0];        pushhead[1]= (Byte) 127;        pushhead[2]= (Byte) ((length>>56)  & 0xFF ); &NBSP;&NBSP;&NBSP;&NBsp;    pushhead[3]= (Byte) ((length>>48)  & 0xff);         pushhead[4]= (Byte) ((length>>40)  & 0xff);         pushhead[5]= (Byte) ((length>>32)  & 0xff);         pushhead[6]= (Byte) ((length>>24)  & 0xff);         pushhead[7]= (Byte) ((length>>16)  & 0xff);         pushhead[8]= (Byte) ((length>>8)  & 0xff);         pushhead[9]= (Byte) (LENGTH&NBSP;&AMP;&NBSP;0XFF);         out.write (Pushhead);    }         out.write (X.getbytes ("UTF-8");   //re-passing data}


Such a large number of data can also be transmitted once, the original can only transmit no more than 126 bytes of data, to divide the data into batches, the client received after the assembly or other methods of processing. But currently only try to transfer the string, have not tried the picture, back to try

This article is from the "under the Tree Beast" blog, please make sure to keep this source http://shuxiayeshou.blog.51cto.com/4452347/1766112

WebSocket method for transmitting more than 126 bytes of data

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.