• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

UDP

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the Java Tutorial:

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

...Ariel
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic