I/O input stream flush ()

Source: Internet
Author: User

Today, when I learned about Java Socket communication, I wrote a demo test. However, after writing data, the system never received the data. After a long time, it was found that the input stream did not call flush () to refresh.

package SocketTests;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;class Server {    public static void main(String[] args) throws IOException {        new Server(new ServerSocket(3001)).run();    }    private ServerSocket server;    Socket s=null;    public Server(ServerSocket server) {        super();        this.server = server;    }    public void run()    {        try {            while(true)            {                s=server.accept();                InputStream is=s.getInputStream();                BufferedReader br=new BufferedReader(new InputStreamReader(is));                char[]buf=new char[1024];                int i;                while((i=br.read(buf))>0)                System.out.println("server receive: "+new String(buf));            }        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally        {            try {                if(server!=null)                server.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}
Package sockettests; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. outputstreamwriter; import java.net. inetaddress; import java.net. serversocket; import java.net. socket; import Java. util. optional; public class tcptest {public static void main (string [] ARGs) Throw S ioexception {new client (new socket (inetaddress. getlocalhost (), 3001 )). run () ;}} class client {private Socket Client; Public client (Socket Client) {super (); this. client = client;} public void run () {try {outputstream OS = client. getoutputstream (); bufferedwriter BW = new bufferedwriter (New outputstreamwriter (OS); bytes consumed = new bytes (system. in); string STR; while (partial. hasnextline ()){ STR = large. nextline (); BW. write (STR); system. out. println ("client write:" + Str); BW. flush (); // refresh the buffer of the stream. This line of code does not exist, data is not written into it, and the server calls BW. readline () will remain congested} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} finally {try {If (client! = NULL) client. Close () ;}catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}}

When learning the IO stream, in the example I wrote, after the input stream is written, the close () method is called directly to close it. The stream will be automatically refreshed before it is closed! In the above example, after the client writes data, it does not close, but waits for the next input; in this way, when the server calls BR shortly. readline () is always congested, so no data can be read !!

 

I/O input stream flush ()

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.