我也是在做socket的C/S测试的时候发现这个问题的(使用write的时候并没有互相进行通信,但是换成println之后就可以了)。
那么就来看看write和println到底有啥区别吧,先看源码:
println(String x):
/**
* Prints a String and then terminates the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x the <code>String</code> value to be printed
*/
public void println(String x) {
synchronized (lock) {
print(x);
println();
}
}
可见,println()方法,在print()之后输出了一个新的一行,再看看print()干了些啥:
/**
* Prints a string. If the argument is <code>null</code> then the string
* <code>"null"</code> is printed. Otherwise, the st