Java 7 64 ビット サーバー vm を使用して、Windows 7 から仮想 Ubuntu マシン (Oracle 仮想ボックス) に数バイトを送信しようとしています。このコードは 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();
Ubuntuではこれを実行します
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();
}
}
}
何らかの理由で最初のバイトがドロップされることがあります。ただし、サーバー コードに Thread.sleep を配置すると、常に正しく動作しますか? なぜそれが起こっているのですか?