Previous Thread
Next Thread
Print Thread
Rate Thread
#18436 03/28/04 09:43 PM
Joined: Mar 2004
Posts: 14
D
defy Offline OP
Junior Member
OP Offline
Junior Member
D
Joined: Mar 2004
Posts: 14
Hi there,
i'm writing a program for a course in the University and i have to use the protocol UDP. The general idea is that there is a server in the University that is listening in a specific port, let's say server_port, and i have to send over the internet packets using the UDP. Then the server sends back to me a packet in a specific port of my system.
The program seems to be working for the sending part but when it comes to receive a packet i have an IOException.
What i do follows
- create 2 buffers for the datagram packet that follows
byte[] packet_send = new byte[1024];
byte[] packet_receive = new byte[1024];

- take the address of the server
ia = InetAddress.getByName("SERVER_ADDRESS");

- create a datagram packet for sending
DP_send = new DatagramPacket(packet_send, packet_send.length, ia, server_port);

- create a datagram packet for receiving
DP_receive = new DatagramPacket (packet_receive, packet_receive.length);

- set parametres for the packet_receive
DP_receive.setAddress(client_address);
DP_receive.setPort(client_port);

- create a datagram socket in the specified port of my system
dr = new DatagramSocket(client_port);

- send a packet
dr.send(DP_send);

- set the time to wait for the receiving packet
dr.setSoTimeout(1000);

- wait for a packet
dr.receive(DP_receive);

After all these, in the last line i get an IOException. I don't know why...
Another thing. The packet i'm sending must have as data something specified, like E2345. I tried to set the data of the packet by using
packet_send = data.getBytes();
where the "data" is the string "E2345", but nothing happens.

Does anyone have an idea of how i can send and receive these packets?
Thanks a lot.

#18437 03/29/04 06:28 AM
Joined: Mar 2002
Posts: 1,136
P
UGN Elite Poster
Offline
UGN Elite Poster
P
Joined: Mar 2002
Posts: 1,136
First off, post the entire IOException. It'll give a lot of information that I can use to help you out.

All the data in a UDP packet is stored in a special format, though I forget the name. You need to specify data types, lengths, and all other kinds of crap for all the data in a UDP packet. It'd be a pain in the [censored], but fortunately, Java makes it easy.

When you're creating your packet, if you send data to the server, you don't store stuff in a byte array. You use a wrapper around a byte array, the ByteArrayOutputStream, in conjunction with an ObjectOutputStream. What this does is let you call a few methods on the OutputStream class, and it will fill up an array of bytes with all the information you need. Then you get the array from the object, and make that the data for your packet.

Here are some examples:

Code
// Create the object wrappers
ByteArrayOutputStream bas = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bas);

// Write some data to the byte array, using an Object wrapper to easily handle data types
out.writeInt(65);
out.writeUTF("UGN");

// Flush the streams
out.flush();
bas.flush();

// Get the bye array
byte packet_send[] = bas.toByteArray();
Likewise, when reading data from a UDP packet, you can't just grab the array of bytes. You could, but you'd need to handle all the extra data in there. So again, you use a wrapper around the data, which lets you easily manipulate it.

Code
ByteArrayInputStream bis = new ByteArrayInputStream(DP_receive.getData());
ObjectInputStream in = new ObjectInputStream(bis);

// Read the data from the input stream.  This assumes you know the ordering of the data (you know that an int 
// comes first, and a string second).  If it's an a different order, you'll get an exception
int num = in.readInt();
String s = in.readUTF();
Those are some small examples I just wrote up, I didn't test them, but I'm pretty sure they're error free. I don't have any exception handling in there - you get to do that on your own.

If you haven't figured out the exception yet, post the entire error, and I'll take a look at it, and we'll see if we can work it out.

Here are the JavaDocs for the stream wrappers you'll want to take a look at:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayOutputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayInputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html

#18438 03/29/04 10:48 PM
Joined: Mar 2004
Posts: 14
D
defy Offline OP
Junior Member
OP Offline
Junior Member
D
Joined: Mar 2004
Posts: 14
Thanks a lot pergesu!

I have now understood more things about the stored information in a packet using UDP.

The fact is that i figured out the Exception. I created only one DatagramSocket for the receiving port and i used it for sending packets. Now i created another DatagramSocket with no parametres and i use this one for the sending packet. If you would like to see it, this is the code:
DatagramSocket dr, ds;
dr = new DatagramSocket(client_port);
ds = new DatagramSocket();
ds.send(DP_send);
dr.receive(DP_receive);

Now, this is working. Of course, the rest program remains the same...

I'd like to ask you one more thing i don't get. I understand what your code for the sending packets is doing, but i don't get this: why for the same string, your array returns a different result from the packet_send = data.getBytes(); , where the "data" is the string. Shouldn't the printing result of these two arrays be the same?

And something last. I think that i understand what your code for the receiving packets is doing but i have a few questions. First of all, you store the data from the packet in a buffer "bis". Then you create an object from the class ObjectInputStream named "in". As parametres you pass the object "bis". Finally, you read the data with a method. Am i right till here? I don't get what you are saying about the order the data is stored in the packet. I tried to run your code in my code but i get an Exception
"IOException!java.io.StreamCorruptedException: invalid stream header".
No matter the order i write the methods i get this exception.
Can you explain this to me?

Thanks again!

#18439 03/30/04 03:08 AM
Joined: Mar 2002
Posts: 1,136
P
UGN Elite Poster
Offline
UGN Elite Poster
P
Joined: Mar 2002
Posts: 1,136
Quote:

I'd like to ask you one more thing i don't get. I understand what your code for the sending packets is doing, but i don't get this: why for the same string, your array returns a different result from the packet_send = data.getBytes(); , where the "data" is the string. Shouldn't the printing result of these two arrays be the same?
No, they shouldn't be the same. When you retrieve the data from the packet, using the getData() method, you get an array of bytes. A String is an object, and has some extra data associated with it. Also, you're dealing with bytes, rather than characters. In Java, a character is stored in memory as two bytes, rather than just one.

Quote:

And something last. I think that i understand what your code for the receiving packets is doing but i have a few questions. First of all, you store the data from the packet in a buffer "bis". Then you create an object from the class ObjectInputStream named "in". As parametres you pass the object "bis". Finally, you read the data with a method. Am i right till here?
All the data in a UDP packet is transferred in a special format, called UTF. Or it should, anyway, but you can certainly do it a bit differently if you want. The packets you receive from the server are being transferred in this format, using the same technique as we used to write data to a packet earlier, using a ByteArrayOutputStream and an ObjectOutputStream. When reading the data, you need to do the reverse. You need to use the wrapper classes to automatically handle the extra header data that comes in the packet. The stream classes provide a very easy mechanism of doint that.

The first thing we do is create a ByteArrayInputStream, which wraps around an array of bytes. You don't have any real reason to use it, other than the fact that it provides an InputStream object, which you can use as a parameter to another InputStream. The InputStream that we use is an ObjectInputStream. What it does is allow you to read objects from any kind of stream, you don't have to worry about what kind of stream you're reading from, be it a file, socket, or byte array. You just create the stream and say you want to read an integer, and it figures everything out and gives you an integer. So that's why we use an ObjectInputStream - it provides easy access to the underlying stream, so you don't have to bother with dirty details.

Quote:

I don't get what you are saying about the order the data is stored in the packet. I tried to run your code in my code but i get an Exception
"IOException!java.io.StreamCorruptedException: invalid stream header".
No matter the order i write the methods i get this exception.
Can you explain this to me?
That's exactly the exception I was talking about. You can't just plug the code I wrote into your own code - you'll need to modify it some.

Here's the basic format of the data in a UDP packet:
typeCode:length:value:typeCode:length:value

It's not exactly like that, but it gives you an idea. So here's an example:
int:2:67:string:4:wolf

The data in the packet is an integer and a string, in that order. So what happens when I call readInt(), is it checks out the stream, sees that the type code matches the one expected (int), and that it has a length of 2. Then it picks off the next two characters of a string, converts them to an int, and returns it to you. Then it does it for the string.

Now say you call readUTF() first. It looks at the stream, and sees that the first type code is for an int. It's expecting a string, so it throws an exception, and you don't get anything.

So what you need to know is the order of the data in the packet. You need to know that the first field is an int, and the second is a string. Keep in mind that's not how it is, that's just an example. You need to take a look at the assignment, and it should tell you what order to expect the data.

#18440 03/31/04 10:49 PM
Joined: Mar 2004
Posts: 14
D
defy Offline OP
Junior Member
OP Offline
Junior Member
D
Joined: Mar 2004
Posts: 14
Ok, now i think i' m starting to get it right!

I read your information about the stored data in an packet using the UDP. It was pretty clear and now i' m testing it out with some simple examples.

Thanks a lot pergesu!


Link Copied to Clipboard
Member Spotlight
Posts: 35
Joined: August 2003
Forum Statistics
Forums41
Topics33,840
Posts68,858
Average Daily Posts0
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Where and how do you torrent?
by danni75 - 03/01/24 05:58 AM
Animation,
by JohanKaariainen - 08/15/19 01:18 AM
Blackbeard.....
by Gremelin - 10/03/18 07:02 PM
my old account still exists!
by Crime - 08/10/18 02:47 PM
Okay WTF?
by HenryMiring - 09/27/17 01:45 AM
The History Thread...
by Gremelin - 08/11/17 12:11 PM
My friend NEEDS your HELP!
by Lena01 - 07/21/17 12:06 AM
I'm having fun with this guy.
by gabithompson730 - 07/20/17 01:50 AM
I want to upgrade my phone
by gabithompson730 - 07/20/17 01:49 AM
Doom 3
by Cyrez - 09/11/14 08:58 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666 - 08/22/14 09:21 AM
AIM scene 99-03
by lavos - 09/02/13 08:06 AM
Planetside 2
by Crime - 03/04/13 07:10 AM
Beta Testers Wanted
by Crime - 03/04/13 06:55 AM
Hello Everyone
by Gremelin - 02/12/12 06:01 PM
Tracfone ESN Generator
by Zanvin Green - 01/18/12 01:31 PM
Python 3 issue
by Testing - 12/17/11 09:28 PM
tracfone airtime
by Drache86 - 07/30/11 03:37 AM
Backdoors and the Infinite
by ZeroCoolStar - 07/10/11 03:52 AM
HackThisZIne #12 Releaseed!
by Pipat2 - 04/28/11 09:20 PM
gang wars? l33t-wars?
by Gremelin - 04/28/11 05:56 AM
Consolidate Forums
by diggin2deep - 04/21/11 10:02 AM
LAN Hacking Noob
by Gremelin - 03/12/11 12:42 AM
Top Posters
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Likes Received
Ghost 2
Crime 1
Ice 1
Dartur 1
Cyrez 1
Powered by UBB.threads™ PHP Forum Software 7.7.5