I am fairly new to programming in both Java and C and need some help.
So I have a C application that sends out structures over UDP :
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char**argv)
{
int sockfd,n;
struct sockaddr_in servaddr,cliaddr;
struct dataType {
char name[4];
unsigned short did;
unsigned short sid;
unsigned short type:4,pri:2,cb:2,flags:8;
unsigned char pblock;
unsigned char tblock;
unsigned short mess;
unsigned int window:24;
};
struct dataType sample_header;
strcpy(sample_header.name,"TEST");
sample_header.did=23;
sample_header.sid_id=1;
sample_header.type=1;
sample_header.pri=01;
sample_header.cb=1;
sample_header.flags=18;
sample_header.pblock=10;
sample_header.tblock=20;
sample_header.mess3;
sample_header.window=123890;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(6120);
sendto(sockfd, (char *)&sample_header, (sizeof(struct dataType)),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
}
Now I need to be able to receive this data over UDP in Java and populate an object with these values. I can get the first string (name) but not sure how to go about getting the remaining items.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.nio.*;
public class UDPReceive {
public static void main(String args[]) {
try {
int port = 6120;
DatagramSocket dsocket = new DatagramSocket(port);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dsocket.receive(packet);
byte[] data = packet.getData();
String msg1 = new String(data, 0, 5);
System.out.println("Here is SOME :" + msg1);
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
}
}
}
I had a look at Google’s protocol buffers but it obviously requires change on both sides.
I also need to be able to eventually send out this data from Java object to UDP and back into C structure.
Many Thanks.
You should not in general write out C structures to the network due to endian and bit packing issues. Instead you want to go through a marshalling and encoding process where you write out each structure memory in a portable network encoding format. The other side reads in the portable network format and stores it however it needs to.
This is how all Internet standard RFCish protocols work. They define order, encodings, endianness, and other semantics of all of the fields. Yes, they sometimes arrange it so that a carefully constructed structure can be overlaid on the network buffer, but even then they often need to perform htons() or friends on the buffers to get them into the correct endian-ness.
I recommend taking this approach.
Reading your structure, it is not laid out in a manner which promotes being sent directly on the network. It would seem very likely that there are holes in the structure, for example, which would be very difficult for other systems to decode properly.