Solved

UDP data mapping

Posted on 2004-10-14
12
348 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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