?
Solved

Encode - decode UDP binary packets

Posted on 2004-04-14
7
Medium Priority
?
1,345 Views
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.
0
Comment
Question by:chelince
[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
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
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);
0
 

Author Comment

by:chelince
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.  
0
 
LVL 86

Accepted Solution

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

byte whateverByteOfThePacket |= (1 << 7);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

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

Expert Comment

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

whateverByteOfThePacket |= (1 << 7);
0
 

Author Comment

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

Expert Comment

by:CEHJ
ID: 10836414
8-)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 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