Today in learning socket communication, write a simple demo, run a problem, the client writes the data and calls the Flush method, the server side did not receive the data. The
Client main code is as follows:
Socket socket = new Socket (ipaddress, 2017);
BufferedReader inreader = new BufferedReader (new InputStreamReader (
Socket.getinputstream ()));
PrintWriter writer = new PrintWriter (Socket.getoutputstream ());
BufferedReader systemreader = new BufferedReader (
new InputStreamReader (system.in));
String Inputtext = Systemreader.readline ();
while (!inputtext.equals ("over")) {
mcommonprint.print ("Client:" + inputtext);
Writer.print (inputtext);
Writer.flush ();
Mcommonprint.print ("Server:" + inreader.readline ());
Inputtext = Systemreader.readline ();
}
Writer.close ();
Systemreader.close ();
Inreader.close ();
Socket.close ();
The main server-side code is as follows:
BufferedReader Inreader = new BufferedReader (New InputStreamReader (
Socket.getinputstream ()));
Mcommonprint.print ("clinet:" + inreader.readline ());
PrintWriter writer = new PrintWriter (Socket.getoutputstream ());
BufferedReader systemreader = new BufferedReader (new InputStreamReader (system.in));
String Inputtext = Systemreader.readline ();
while (!inputtext.equals ("over")) {writer.print (inputtext);
Writer.flush ();
Mcommonprint.print ("Server:" + inputtext);
Mcommonprint.print ("clinet:" + inreader.readline ());
Inputtext = Systemreader.readline ();
} systemreader.close ();
Writer.close ();
Inreader.close ();
Socket.close (); Serversocket.close ();
The information on the Internet is said to have not called the PrintWriter Flush () method, but I clearly called ....
I tried it. If you call the Writer.close () method directly after Writer.print (), then the server can get the data ...
But still wood has to solve the problem ...
Finally looked at the other people wrote the code, found the problem ...
Other netizens call the Writer.println () method, and I call the Writer.print () method, so I replaced the Writer.println () method, the result of the problem solved.
But why.
Then looked at the source code ...
Print () method source code:
public void print (String s) {
if (s = = null) {
s = "null";
}
Write (s);
}
println () method source code:
public void println (String x) {
synchronized (lock) {
print (x);
println ();
}
The println () in which the newline () is invoked ():
private void newline () {
try {
synchronized (lock) {
ensureopen ();
Out.write (lineseparator);
if (AutoFlush)
Out.flush ();
}
}
catch (Interruptedioexception x) {
thread.currentthread (). interrupt ();
}
catch (IOException x) {
trouble = true;
}
}
From the source you can see that the print () method and the Write () method is not much different from the println () method with a out.write (lineseparator), added a separator, and can set automatic flush , because the default autoflush=false can be set to True when the PrintWriter is created.
PrintWriter writer = new PrintWriter (Socket.getoutputstream (), true);
In this view, is the reason for this separator ...
And then found the code:
Inreader.readline ()
ReadLine () is a row to read data, and the way to identify a row is to use a separator character.
Official Description:
Reads a line of text. A line is considered to being terminated by any one of the line feeds (' \ n '), a carriage return (' \ R '), or a carriage return fol Lowed immediately by a linefeed
That is, if you do not encounter a newline or carriage return separator, you will read the data all the time.
But the print () and write () methods do not add a separator, so there is a problem.
Solution:
1. Change the WRITER.PRINTLN (Inputtext) to Writer.println (inputtext+ "\ n"), and add the separator manually.
2. Use the println () method.
However, you invoke the flush () method regardless of which method you use.