Tesseract
Tesseract

Reputation: 8139

TCP connection drops first byte

I'm trying to send a few bytes from Windows 7 to a virtual Ubuntu machine (oracle virtual box) using Java 7 64 bit server vm. This code runs on Windows

ServerSocket server = null;
try {
    server = new ServerSocket(1024);
} catch(Exception e) {
    e.printStackTrace();
}
new Thread() {
    @Override
    public void run() {
        while(true) {
            try {
                Socket so = server.accept();
                //Thread.sleep(10);
                OutputStream out = so.getOutputStream();
                out.write(42);
                out.write(43);
                out.flush();
                out.close();
                so.close();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}.start();

On Ubuntu I run this

public class Client {
    public static void main(String[] args) throws Exception {
        Socket so = new Socket(args[0], Integer.parseInt(args[1]));
        InputStream in = so.getInputStream();
        int b = in.read();
        while(b >= 0) {
            System.out.println(b);
            b = in.read();
        }
    }
}

For some reason the first byte is sometimes dropped. However if I put a Thread.sleep in the server code it alway works correctly? Why is that happening?

Upvotes: 1

Views: 185

Answers (1)

Stephen C
Stephen C

Reputation: 718768

I cannot see anything wrong with the client or server programs.

That leaves us grasping at explanations like:

  • a bug in the operating system at one end,
  • a bug in the virtual networking setup,
  • real network errors, or
  • ... a hypothetical JVM bug.

None of these explanations can be justified without more information / evidence.


The other possibility is that something is interfering with the output going to your console. Try running the client with output redirected to a file ...

Upvotes: 3

Related Questions