UDP Java client to C Server
I've been at this for 3 days. So, for my sanity sake. Help me get this
resolved.
My solution at the bottom of this is correct. I was printing out the
short's incorrectly in C. Learn from my mistake
printf("Short: %hi \n", cm.tml) NOT printf("Short: %hi \n", &cm.tml)
I need this from java
String message = "Yo";
short tml = "7";
char op = '1';
short rid = "2";
to be packed up and sent to a C server via UDP where a struct like this
struct __attribute__((__packed__)) clientMessage
{
short tml;
short rid;
char op;
char message[MAXBUFLEN-5];
};
Received like this
if ((numbytes = recvfrom(sockfd, &cm, MAXBUFLEN-1, 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);}
can print out like this and be correct
printf("Message: %s \n", &cm.message);
printf("OP: %s \n", &cm.op);
printf("RID: %s \n", &cm.rid);
printf("TML: %s \n", &cm.tml);
Because this must work with C to C (which is does) and Java to C (which it
doesn't b/c I can't pack it up correctly).
Please and thank you. I'll be staring at this post so if you need more
info please ask.
As requested
my best attempt so far was this
byte[] sendData2 = new byte[1024];
sendData2 = message.getByte();
sendData[0] = (byte)(tml & 0xff );
sendData[1] = (byte)((tml >> 8) & 0xff);
sendData[2] = (byte)(rid & 0xff );
sendData[3] = (byte)((rid >> 8) & 0xff);
sendData[4] = (byte)(op);
sendData[5] = sendData2[0];
sendData[6] = sendData2[1];
With this attempt:
Message = "Yo" OP = "1Yo" RID and TML appear null. It does seem to be
sending 7 bytes though.
No comments:
Post a Comment