When using both PrintWriter & Scanner, and InputStream & OutputStream on the same socket, odd thing happens. When call InputStream.read() after call Scanner.nextLong(), InputStream.read() can't receive data exactly. It seems Scanner caches the input. The solution is send and receive something before call InputStream.read(). Sequence is important like send/receive/send/receive, not like send/send/receive/receive for client: Scanner.nextLong() PrintWirter.println() InputStream.read() for server PrintWriter.println() Scanner.nextLine() OutputStream.write()
artilla's personal blog. Sorry for broken English, just in practice