-1

I thought it might be byte ordering but it doesn't look like it. I am not sure what else it could be.

Java client on linux

private static final int CODE = 0;
Socket socket = new Socket("10.10.10.10", 50505);
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
output.writeInt(CODE);

c server also on linux

int sd = createSocket();
int code = -1;
int bytesRead = 0;
int result;

while (bytesRead < sizeof(int))
{
    result = read(sd, &code + bytesRead, sizeof(int) - bytesRead);
    bytesRead += result;
}

int ntolCode = ntohl(code); //test for byte order issue

printf("\n%i\n%i\n%i\n", code, ntolCode, bytesRead);

Which prints out:

-256
16777215
4

Not sure what else to try.

Solution

This solution is not intuitive in the least for me, but thanks for the down votes anyway!

Java side

Socket socket = new Socket("10.10.10.10", 50505);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
int x = 123456;
ByteBuffer buff = ByteBuffer.allocate(4);
byte[] b = buff.order(ByteOrder.LITTLE_ENDIAN).putInt(x).array();
out.write(b);

C side

int sd = createSocket();

char buff[4];
int bytesRead = 0;
int result;
while (bytesRead < 4){
    result = read(sd, buff + bytesRead, sizeof(buff) - bytesRead);
    if (result < 1) {
        return -1;
    }
    bytesRead += result;
}

int answer = (buff[3] << 24 | buff[2] << 16 | buff[1] << 8 | buff[0]);

I am still interested in a simpler solution if anyone has anything, preferably using BufferedWriter if that is possible.

Nicolas
  • 331
  • 4
  • 13

2 Answers2

4

The problem is here:

&code + bytesRead

This will increment the address of code in steps of 4 (sizeof code), not 1. You need a byte array, or some typecasting.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Good observation, but only an issue if `read()` doesn't read all 4 bytes on first call, which seems unlikely. I do however agree that reading to a byte array first is better. – Andreas Jul 02 '16 at 23:14
  • It frequently reads less than 4 bytes in one read call for what ever reason. Thanks, I will give that a try. – Nicolas Jul 02 '16 at 23:19
-3

You forgot to design and implement a protocol! You wrote one piece of code that sends data in one format and another piece of code that receives data in an entirely different format. Decide on a format, document that format, then write code that sends in that format, then write code the receives in that format.

Do not skip the documentation step. That is the most important one. Document precisely what bytes will be used to communicate the information.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Why do you think they use "entirely different formats"? – Andreas Jul 02 '16 at 23:22
  • He is sending a four byte integer in network byte order. You can't claim that isn't a protocol. – user207421 Jul 02 '16 at 23:23
  • @EJP However he's sending, he's sending it somehow. But if he hasn't documented it, there's no way to say whether that's how he's supposed to be sending it or not. Happening to do something some particular way is not a protocol. A protocol is a specification for how things *should* happen. – David Schwartz Jul 02 '16 at 23:25