Encode - decode UDP binary packets

Posted on 2004-04-14
Last Modified: 2012-06-21
I have a Java UDP server and client that currently communicate sending ASCII strings.  I need to be able to send binary packets instead from the client and have the server decode the packet and send back ACKNOWLEDGE  response packets also in binary format.  How can I accomplish this?  I found several examples for converting to and from text or binary files, but nothing specific for UDP packets.   I only have a basic Java programming knowledge, so please bear with me. Thanks in advance.
Question by:chelince
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
LVL 86

Expert Comment

ID: 10822301
All you need to do is to put the Strings in, or take them out, as byte[]. For the default encoding:

// In
String s = "Hello World!";
byte[] bytes = s.getBytes();
// now copy into buffer

// Out

byte[] bytes = ............ (got out of packet buffer)
String s = new String(bytes);

Author Comment

ID: 10826256
This turned out to be more complicated than it seemed at first.  The incoming UDP packet is 8 bytes long, my task is to receive it and send back an ACK by setting the MSB.  There may or may not be text data in the packet, the reason I was not seeing any data is that the first incoming packet has none, the remote sender is waiting for an ACK.  Your answer works for plain test, but now I have to get into setting bits.  If you can't help me I'll still give you the points and start a new question.  
LVL 86

Accepted Solution

CEHJ earned 80 total points
ID: 10826480
You can set the MSB thus

byte whateverByteOfThePacket |= (1 << 7);
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 86

Expert Comment

ID: 10826523
(assuming you mean Most Significant Bit by 'MSB')
LVL 86

Expert Comment

ID: 10826542
and actually you're not likely to see it thus in your code. It's more likely to be

whateverByteOfThePacket |= (1 << 7);

Author Comment

ID: 10835297
Thanks CEHJ, both suggestions worked and got me going.  
LVL 86

Expert Comment

ID: 10836414

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
ejb example issues 3 53
Java: anonymous class 4 38
Notify sent to other threads in Java 9 43
How to fix  socket closed error 11 27
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 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