java udp client to C server
Ok, I've been at this for 3 days now. I'm just lost. So, I'll be honest,
this is a project but we've only been in the class for a week and a half
and the professor has assigned us this horrendous assignment without
really teaching us how. So, we have to do a UDP and TCP client server
setup. The TCP client has to be in another language (I'm using Java since
I'm familiar with it) Anyways, I can send the message just fine but we are
required to use a struct in C and he wants us to receive the message with
the struct. So, I'm basically having serialization issues. This is how I'm
packing it in Java. *Note this works perfectly when doing C client to C
server (as it should since it's a packed struct on both ends).
String message;
short tml;
char op;
short rid;
message = "Yo";
tml = 7;
op = '1';
rid = 2;
sendData2 = message.getBytes();
byte[] sendData = new byte[sendData2.length + 5];
sendData[0] = (byte)((tml >> 8) & 0xff );
sendData[1] = (byte)(tml & 0xff);
sendData[2] = (byte)((rid >> 8) & 0xff );
sendData[3] = (byte)(rid & 0xff);
sendData[4] = (byte)op;
sendData[5] = sendData2[0];
sendData[6] = sendData2[1];
Receiving in C
if ((numbytes = recvfrom(sockfd, &cm, MAXBUFLEN-1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);}
I can get the message just fine but I seem to be having issues serializing
the short and the char. I've tried the method above (both big endian and
little endian even though I know it's supposed to be Big Endian, desperate
times, desperate measures lol) But I just can't get the C server to decode
the byte array properly. I get a consistent response of
Message = "Yo"
rid = 21042
tml = 21040
op = 4
It seems I'm sending SOMETHING consistently, but I'm either not
serializing it properly or I'm not decoding it or something on the C
server. But I'm sending it exactly how the struct in C should be receiving
it. So yea, I'm stuck. I can supply the files if someone wants the
prebuilt code for testing.
Yes, it must be a struct. Yes, I know that's bad but it's our assignment.
Any help is GREATLY appreciated.
Oh yea, here's the struct in C
#define MAXBUFLEN 100
struct __attribute__((__packed__)) clientMessage
{
short tml;
short rid;
char op;
char message[MAXBUFLEN-5];
};
No comments:
Post a Comment