Solved

UDP data mapping

Posted on 2004-10-14
12
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 129
arguments to jar 5 40
printf performancy 11 86
Java array 21 86
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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