• Post Reply Bookmark Topic Watch Topic
  • New Topic

DatagramPacket address parameter  RSS feed

 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I'm a bit confused with the address parameter of the DatagramPacket class.
I realize the type is a byte structure.



Obviously, it would throw an exception because the address is type byte, which can only take up to 127.

But how do I get it to take addresses in the example?
For now, I coded it like this:



Is this correct? Or is the first code correct, but I have to do more manipulation? If so, could someone please help me?

Karen
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going to move this to the IO forum. If there is little traffic or answers there, then we will move it to Java In General (Intermediate).

I think this is a little above beginner level.

Mark
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a typo error in my code.

It should read:


I do hope someone could provide me an answer to this. I've googled the web on this specific question but could not get an answer. I'm sure this is the place to get a definitive answer.

Karen
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi -

I'm not sure what you're assignment is supposed to be, but the main problem is that (unlike C/C++), the Java integer types "long", "int", "short" and "byte" are *signed* only. There's no such thing as an "unsigned byte" in Java.

As such, the values for "byte" are -128 through 127.

Which is why your program dies with a numeric error when you try to assign a value of "202" to a byte array.

Basically, if you want to manipulate a value greater than 127, you should use a variable type wider than a "byte". If you want to *store* data in the most compact format possible, then you should *pack* it into (narrow, byte) storage and *unpack* it (when you're ready to actually the use it).

In this case, "packing" and "unpacking" would simply be casting as you assign from a variable of one type to a variable of the other.

One (of several) solutions is to use a 16-byte (signed!) short, instead of an 8-bit (signed) byte. For example:



This example runs with the following output:


You'll notice I also changed the "result" declaration to "String[]" (which you mentioned above), and changed the for loop to "i < result.length" (so the loop will run from 0 to 3, inclusive, and terminate at "4").

'Hope that helps .. PSM

PS:
A final note: manipulating the contents of your 4-part IPV4 address is one problem (a problem that has more to do with Java datatypes and little to do with Java networking per se).

I presume what you're really after is some working sockets code.

I'd strongly encourage you to download, build, play with and generally familiarize yourself with the UDP Send and Receive "hello world" programs here:

http://www.oreilly.com/catalog/javanut/examples/
[ July 15, 2005: Message edited by: Paul Santa Maria ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!