?
Solved

UDP data mapping

Posted on 2004-10-14
12
Medium Priority
?
373 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 252 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 248 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

777 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