Solved

UDP data mapping

Posted on 2004-10-14
12
352 Views
Last Modified: 2010-03-31
Now i'm doing socket programming using UDP and TCP/IP and now again i
need your help.

1. Can UDP packet in Java put into data structure like C? or packet map to a java object but sender is not using Java program or serialize object.

2. What is the easiest way to extract the UDP packet in Java?

assume the structure like this.


sampleMessageHeader
{
   MessageCode UInt 16
   TimeStamp UInt 32
   Message Char(12)
}

 
 
0
Comment
Question by:wikey
  • 5
  • 2
  • 2
12 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 63 total points
ID: 12316220
Hi wikey,
> 1. Can UDP packet in Java put into data structure like C? or packet
> map to a java object but sender is not using Java program or serialize object.
No

> 2. What is the easiest way to extract the UDP packet in Java?
Send the data in the format of a Java properties file. Then extract the information from the Properties object.

Cheers!

\tt
0
 

Author Comment

by:wikey
ID: 12316495
> 2. What is the easiest way to extract the UDP packet in Java?
> Send the data in the format of a Java properties file. Then extract the information from the Properties object.

but the sender is not using java, and already fixed the packet format. I need to follow their format like this

sampleMessageHeader
{
   MessageCode UInt 16
   TimeStamp UInt 32
   Message Char(12)
}
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 62 total points
ID: 12316778
Open a TCP/IP connection to the server and send the data as String. The read it on the server side and reconstruct the C struct.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:wikey
ID: 12317024
okay, let's clear something first...

1. The system got "sender" and "receiver"
2. I'm "only" doing the "receiver" part using Java and i do not know "what" programming "sender" uses. could be C, C++.
3. Sender will send UDP packet to receiver and the format is "binary data structure".  

4. I need a best way to extract the data from the packet in Java.

thank you....
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317259
The best way is to create a DatagramPacket from the incoming data:

byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet); // receive the bytes from the server(sender)
byte [] dataRead = packet.getData[];

Now you have the date read from the sender as a byte array (dataRead). You need to find a way to create the object from them, for example if you want to create a String, you can do:

String s = new String(dataRead);
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317262
> byte [] dataRead = packet.getData[];

should be

byte [] dataRead = packet.getData();

and I also assume that you have a DatagramSocket named "socket"
0
 
LVL 35

Expert Comment

by:girionis
ID: 12317263
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12320488
wikey,

I thought you were asking about the easiest way to go between native and Java.

There is one thing to keep in mind: byte order!

Please ensure that the values in the packet are in network byte order when you receive them, or if their not, that at least you know what the byte order is.

\t
0
 
LVL 35

Expert Comment

by:girionis
ID: 12544558
I'd say split the points among all participants.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 62
difference between sorce folder and folder in eclipise 3 28
CSV file parsing thru Java 13 34
Problem to Alipay 10 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question