aspose file tools*
The moose likes Beginning Java and the fly likes UDP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "UDP" Watch "UDP" New topic
Author

UDP

Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi guys,

Could anyone please provide me some tutorial or links to some sample code (that works) on the subject on UDP?

Head First Java only has the sample TCP/IP.

Cheers!


amerzil co-ed student<br />"Praise be the Code"
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi all,

There are many codes on UDP but there are no explanations. It seems to be more complicated that TCP/IP.

For now, I just want a simple UDP server and UDP client. Basically, what I'm trying to understand is create a UDP application listening for something from Machine A for a particular message, do something with that message, and send the reformatted message to Machine B.

The application continually listens for a specific message.

Thanks again.
Karen
Ariel Ortiz
Ranch Hand

Joined: May 14, 2004
Posts: 121
Check the Java Tutorial:

http://java.sun.com/docs/books/tutorial/networking/datagrams/index.html

...Ariel
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Thank you for the reply.

I will have a look at the code and carefully study it.

For now, I would think that if I write the client in java, it doesn't care what language the other programs on the network is written.

My application would be written in Java and will communicate, via UDP protocol, with other applications written in C++.

With that said, my application would pretty much sit in between two machines written in C++. Basically, my java program more like "translates" the messages. Should my Java program be written as a server, or just plain client? The flow of data is only one-way, i.e. receive message from machine A, translates it, then forwards this to Machine B.


Thank you once again.
Karen
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi Guys,

Technically, it's not really one-way.

Consider the setup I have:

MachineA <----->MyMachine----->MachineB

MachineA sends message to MyMachine. MyMachine acknowledges message received from MachineA. MyMachine then translates the message and then forwards it to MachineB.

MachineA and machineB applications are written in C++.
MyMachine application would be written in Java.


I'm asking for some guidance.

Cheers to all,
Karen
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi all,

This code I got from the Sun site on datagrams.
I stripped it to the extent that only relevant code (I think) is included.
I'm doing it one step at a time else I'll just get drowned with too much info beyond what I can chew.

In the code below I can see that it responds to the client (lets call it MachineA) that sends a message.

Now, this time, I also want to be able to send the same message (for now) to another client (let's call it MachineB). I don't know how I could do this. Please feel free to modify the code.

I also put the reading and sending in a while loop, that loops indefinitely. I don't know if this is the correct way of doing it. I want the Server to indefinitely listen for, receive messages from machineA, acknowledge the messages from MachineA, and send messages to machineB. If it's not the right way, again, please feel free to modify the code.

The server only sends messages to MachineB only after translating the message received from MachineA. Again, just for this demonstration, I just need to forward the same message to MachineB.

This is the flow of messages (assuming all packets are valid):

Listen for packet...

MachineA ---send message---> TestServerMain
MachineA <---acknowledge----- TestServerMain

After the acknowledgement, MyServer translates the message received from MachineA
Then...

TestServerMain----send translated message----> MachineB

Go back and listen for packet.

Also, when the application is terminated, will the socket actually close, as the socket.close() is inside the while loop.





Thanks you.
Karen
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi guys,

I suppose to send a message to another machine (in the example code above, MachineB) is to create another DatagramPacket. Is this the right approach?

So, basically, the code would be expanded:





If this is the case, then how, if I was to hardcode the address, is it written in string form? Are there any conversions I need to do? For example, if the address is "202.20.0.4", how should this be written to pass to the DatagramPacket as the address argument?


I desperately need your help. I have to get this right to demonstrate to our class.
Thanks to all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UDP