Solved

UDP data mapping

Posted on 2004-10-14
12
331 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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
 

Author Comment

by:wikey
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:girionis
Comment Utility
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
Comment Utility
> 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
Comment Utility
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
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
Comment Utility
I'd say split the points among all participants.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now