Declaration: This blog post describes how to disable the output stream in socket.
For convenience, we use the dout in dataoutputstream dout = new dataoutputstream (New bufferedoutputstream (mysocket. getoutputstream (); as the endorsement of the socket output stream. Similarly, DIN is the endorsement of the input stream.
Operations that can cause dout to be disabled include:
1. Call dout. Close (); or din. Close (); because this type of stream is used to close the socket, the input and output streams are unavailable.
2. Call socket. Close ();
3. Call socket. shutdownoutputstream (); To disable dout unilaterally. At this time, din can be used normally.
Below, I will perform three tests on closing the output stream in the socket:
Test output stream disabling 1: Is socket disabled?
Output stream closed Test 2: Can the stream be restarted?
Output stream close test 3: Is the data in the output buffer discarded or sent? The test results are as follows:
Test 1: dout. Close (); will cause the socket to be closed, but socket. shutdownoutputstream () will not.
Test 2: No. An exception is thrown!
Test 3: discard
Client Program:
Package COM. test2; import Java. io. *; import java.net. *;/*** @ classname: sockettest * @ Description: In the test socket, after the stream is closed, will the socket be closed? Can I reactivate a stream? Is the data in the output cache sent out or discarded? * @ Author jogging Android * @ date 2011-11-12 11:15:21 */public class sockettest {socket mysocket; dataoutputstream dout; public static void main (string [] ARGs) {New sockettest ();} public sockettest () {// Test 1 of the output stream closure: Is the socket closed? Test1 (); // closes the output stream. Test 2: Can the stream be enabled again? Test2 (); // test of closing the output stream 3: Is the data in the output buffer discarded or sent? Test3 ();} private void test1 () {// Test 1 of closing the output stream: Is the socket disabled? System. Out. println ("\ n ***** can be used to close the output stream. Is the socket disabled? * ** \ N "); try {mysocket = new socket (" 27.154.122.233 ", 9999);} catch (unknownhostexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} Try {dout = new dataoutputstream (New bufferedoutputstream (mysocket. getoutputstream (); // The following statement is mainly used to prove that the socket is in the enabled status system. out. println ("the output stream has just been opened. Is the socket closed? "+ Mysocket. isclosed (); mysocket. shutdownoutput (); system. Out. println (" use shutdownoutput to close the output stream. Is the socket closed? "+ Mysocket. isclosed (); dout. Close (); system. Out. println (" Close the output stream. Is the socket closed? "+ Mysocket. isclosed ();} catch (ioexception e) {e. printstacktrace () ;}} private void Test2 () {// close the output stream Test 2: Can the output stream be re-enabled after shutdownoutputstream is used? System. Out. println ("\ n *** can the output stream be restarted after shutdownoutputstream is used? * ** \ N "); try {mysocket = new socket (" 27.154.122.233 ", 9999);} catch (unknownhostexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} Try {dout = new dataoutputstream (New bufferedoutputstream (mysocket. getoutputstream (); mysocket. shutdownoutput (); // restart the output stream dout = new dataoutputstream (mysocket. getoutputstream (); dout. writeutf ("Can I reopen it? "); // Clear the output cache to ensure that the message can reach the server dout when the dout channel is okay. flush ();} catch (ioexception e) {e. printstacktrace ();} finally {try {mysocket. close ();} catch (ioexception e) {e. printstacktrace () ;}} private void test3 () {// close the output stream Test 3: Is the data in the output buffer discarded or sent? System. Out. println ("\ n *** is the data in the output buffer discarded or sent? * *** \ N "); try {mysocket = new socket (" 27.154.122.233 ", 9999);} catch (unknownhostexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} Try {dout = new dataoutputstream (New bufferedoutputstream (mysocket. getoutputstream (); dout. after writeutf ("shutdownoutput, CAN data be sent out? "); Mysocket. shutdownoutput ();} catch (ioexception e) {e. printstacktrace ();}}}
Server programs:
/*** @ Title: serversockettest. java * @ package COM. test1 * @ Description: Todo (this file is the sever test end of "socket, what happens after the stream is closed) * @ author jogging Android * @ date 2011-11-12 11:31:05 * @ version V1.0 */package COM. test1; import Java. io. *; import java.net. *; public class serversockettest extends thread {private serversocket myserversocket; private final int Port = 9999; public static void main (string [] ARGs) {serversockettest SST = new serversockettest (); sst. start ();} public serversockettest () {// initialize a servesocket end try {myserversocket = new serversocket (port);} catch (ioexception e) {e. printstacktrace () ;}} public void run () {While (true) {system. out. println ("I'm a server, I'm listening on port 9999 .... "); try {Socket socket = myserversocket. accept (); datainputstream din = new datainputstream (New bufferedinputstream (socket. getinputstream (); string msgin = din. readutf (); system. out. println (msgin. trim ();} catch (ioexception e) {e. printstacktrace ();}}}}
Note:
In test3 (), because dout = new dataoutputstream (newbufferedoutputstream (mysocket. getoutputstream (); buffered is used, so in dout. if the dout. flush (); data will exist in the output cache and will not be sent out.
If our team's dout statement is dout = new dataoutputstream (mysocket. getoutputstream (); then the data will be sent immediately. (Unless the other party does not call read () to read data, and the data volume exceeds the input cache of the other party. However, in this case, dout. writeutf (); will be blocked .)
The following figure shows the console output of the client and server after the program is run:
----------------------------------Client--------------------------
Java.net. socketexception: Socket output is shutdown
At java.net. Socket. getoutputstream (unknown source)
At com. test2.sockettest. Test2 (sockettest. Java: 66)
At com. test2.sockettest. <init> (sockettest. Java: 22)
At com. test2.sockettest. Main (sockettest. Java: 15)
* *** Can I disable the output stream in two ways? Is the socket disabled? ***
When the output stream is just opened, is the socket closed? False
Use shutdownoutput to close the output stream. Is the socket disabled? False
Close is used to close the output stream. Is the socket closed? True
* *** Can the output stream be restarted after shutdownoutputstream is used? ***
* ** Is the data in the output buffer discarded or sent? ****
--------------------------------- Server ------------------------------
I'm a server, and I'm listening on port 9999 ....
I'm a server, and I'm listening on port 9999 ....
Java. Io. eofexception
At java. Io. datainputstream. readunsignedshort (unknown source)
At java. Io. datainputstream. readutf (unknown source)
At java. Io. datainputstream. readutf (unknown source)
At com. test1.serversockettest. Run (serversockettest. Java: 37)
Java. Io. eofexception
At java. Io. datainputstream. readunsignedshort (unknown source)
At java. Io. datainputstream. readutf (unknown source)
At java. Io. datainputstream. readutf (unknown source)
At com. test1.serversockettest. Run (serversockettest. Java: 37)
Java. Io. eofexception
At java. Io. datainputstream. readunsignedshort (unknown source)
I'm a server, and I'm listening on port 9999 ....
At java. Io. datainputstream. readutf (unknown source)
At java. Io. datainputstream. readutf (unknown source)
At com. test1.serversockettest. Run (serversockettest. Java: 37)