A pipe is just a byte stream, even if you interpreted the read bytes correctly as a 16bit integer and as a 2 character string "11" which in ASCII is the bytes 31 31, then how do you tell "11" apart from the integer 12593? They have the same binary representation.
One option is to just keep reading until the pipe is terminated, this may be suitable say for transferring a file. But does not allow on-going back and forth messages.
write(mypipe, &random_int, sizeof(short));
close(mypipe); // finished writing
// recv
while (true)
{
short received_int;
if (read(mypipe, &received_int, sizeof(short)) == sizeof(short)) // Not handling the int getting split into two separate read's here
{
// Use received_int
}
else return; // 0 on close
}
This is why most protocols introduce the concept of messages on top of the stream (either in text or binary). For example you might have an 8bit "opcode" and say that 0 is "disconnect" and 1 is "message with integer", so to write an integer:
unsigned char opcode = 1;
short random_int = 55; // Make sure it actually a short, as you used sizeof(short)!
write(mypipe, &opcode, 1);
write(mypipe, &random_int, sizeof(short));
And to read you read the opcode first and then decide:
char opcode;
read(mypipe, &opcode, 1); // CHECK RETURN!
switch (opcode)
{
case 0: return; // Finished
case 1:
{
short received_int;
read(mypipe, &received_int, sizeof(short)); // CHECK RETURN!
// Use received_int
break;
}
}
You might look at some existing protocols on how they encode different things into the byte stream.