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
  • 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);
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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: 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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
tomcat not starting 6 68
going to wrong jsp page 2 33
What browser will run Java? 7 55
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 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