Java Socket parameters: oobinline and urgentdata

Source: Internet
Author: User

TCP emergency pointers are generally not recommended and different TCP/IP implementations are also different, generally, if you have urgent data, you would rather establish a new TCP/IP connection to send data, so that the other party can handle it urgently. However, although the sendurgentdata parameter data is of the int type, only the low bytes of the int type are sent, and the other three bytes are ignored. The Code below demonstrates how to use the so_oobinline option to send single-byte data.

Package socket; import Java. io. *; import java.net. *; import Org. apache. log4j. logger; public class test_oobinline {Private Static logger = logger. getlogger (test_oobinline.class); public static void main (string [] ARGs) throws unknownhostexception, ioexception {Socket socket = new socket ("127.0.0.1", 1234); socket. setoobinline (true); outputstream out = socket. getoutputstream (); outputstreamwriter outwriter = new outputstreamwriter (out); outwriter. write (67); // send the character "C" outwriter to the server. write ("Hello world \ r \ n"); socket. sendurgentdata (65); // send the character "A" socket to the server. sendurgentdata (322); // send the character "B" outwriter to the server. flush (); socket. sendurgentdata (214); // send the Chinese character "medium" socket to the server. sendurgentdata (1, 208); socket. sendurgentdata (185); // send the Chinese character "country" socket to the server. sendurgentdata (1, 250); socket. close () ;}} class server {public static void main (string [] ARGs) throws exception {serversocket = new serversocket (1234); system. out. println ("the server has been started, Port: 1234"); While (true) {Socket socket = serversocket. accept (); socket. setoobinline (true); inputstream in = socket. getinputstream (); inputstreamreader inreader = new inputstreamreader (in); bufferedreader breader = new bufferedreader (inreader); system. out. println (breader. readline (); system. out. println (breader. readline (); socket. close ();}}}

Result:

 
The server has been started. Port: 1234 abchello World China


It can be seen from the figure that although 322 is distributed in two bytes, its low byte is still 66.
Use flush in the client class to send data in the buffer to the server. We can find a problem from the output. In the client class, we send 'C', "Hello World" R "N", 'A', and 'B' to the server successively '. On the serverProgramThe console displays abchello world. This phenomenon indicates that after the sendurgentdata method is used to send data, the system will immediately send the data. However, to use write to send data, you must use the flush method to actually send the data.
Send a "Chinese" string to the server in the client class. Because "medium" consists of 214 and 208 bytes, and "country" consists of 185 and 250 bytes, these four bytes can be sent separately to send the "China" string.
Note:When you use the setoobinline method to open the so_oobinline option, note that you must use the setoobinline method to open this option at both the client and the server. Otherwise, sendurgentdata cannot be used to send data.

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.