Hello everyone.
I'm a bit rusty with java streams, but according to the doc, InputStreamReader is a bridge from byte streams to character streams. Since the sender is dealing with bytes, the receiver should do the same. This java receiver behaves as expected:
Code:
import java.net.Socket;
import java.io.*;
public class x {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 55555);
InputStream isr = s.getInputStream();
int r;
while((r = isr.read()) != -1) {
if (r >= 128) {
r |= 0xFFFFFF00;
}
System.out.println("read: "+ r);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
The input stream returns a value between 0 and 255 with -1 indicating end of stream. Anything over 128 needs to have the sign extended.
Is there a better way? I don't know.
On the sender:
Code:
SOCKET conn = accept(s, &saddr, &size);
char wbuff[30];
int ix = 0;
wbuff[ix++] = 1;
wbuff[ix++] = 0;
wbuff[ix++] = -1;
wbuff[ix++] = -2;
wbuff[ix++] = -3;
wbuff[ix++] = -111;
wbuff[ix++] = -112;
wbuff[ix++] = '\r';
wbuff[ix++] = '\n';
send(conn, wbuff, ix, 0);
The receiver prints
read: 1
read: 0
read: -1
read: -2
read: -3
read: -111
read: -112
read: 13
read: 10