Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help, Newbie trying to do UDP  (Read 1849 times)
0 Members and 1 Guest are viewing this topic.
Offline nech_neb

Junior Devvie

Java for LIFE !!!

« Posted 2003-07-06 16:29:22 »

Hi  all,

I'm trying to use UDP in my game...
but I'm having trouble finding good examples...

I'm using DatagramSocket right now for my
server and client code. The main thing I'm getting
stuck on is, I have no problem making a server
app, and client app, and having the client app
send packets to the server. But is there a way
for the server app to send stuff back to the client
based on the packets sent from the client to the

I think more specifically, from the server , I'm trying to
find a way to find out where the DatagramPacket
is sent from. I tried printing out the InetAddress
of the DatagramePacket, but it was of no use.

To my understanding...  UDP is not connection base...
Do does that mean I can't communicated back
and fourth using 1 instance of the UPD protocal?

I'm thinking if I should be using a DatagramChannel
on the server implementation instead... But I'm
having problems locating example code on the
internet. Also I have found serveral posts complaining
about the performance of the DatagramChannel

And advice would be appriciated.
Sorry for my amature knowledge in advance.
and much thanks.
Offline Backmask

Junior Devvie

586: The average IQ needed to understand a PC

« Reply #1 - Posted 2003-07-07 07:39:37 »

Never thouched UDP in Java until this weekend so nice timing on your question.

First I would recommend this java tutorial at

It explains what UDP is, how to use it in Java. Read especielly this part

as it explains how to create an server, client 2 way communication. But I give you a short code example below

String bufStr = "ping";
byte[] buf = bufStr.toBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length);

buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);

String t = new String(packet.getData()).trim();


//receive packet from client

//packet is the udp-packet the server received from the client
InetAddress address = packet.getAddress();  //Gets the clients ip number
int port = packet.getPort(); //get the port the client used to send the upp-packet

//Create a new packet containing data buf
packet = new DatagramPacket(buf, buf.length, address, port);
//sending the new packet back to the client.

Hope this helps.

Offline nech_neb

Junior Devvie

Java for LIFE !!!

« Reply #2 - Posted 2003-07-07 16:51:06 »

Ahh... I think MulticastSocket will solve my problems...
Once I solve some firewall issues...

Thanks for the link   Wink

On a side note...
Is there any advantage for switching to use
NIO communication rather than net ??

I've heard different stuff through out...
Was the package re-done using NIO ??

Lastly, has anyone read the book
Java NIO Author: Ron Hitchens
ISBN: 0596002882

Is it a good reference for NIO ??
I'm finding lack of good reference information
on the internet on NIO...
and that seems to be the trend from what I've read
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Backmask

Junior Devvie

586: The average IQ needed to understand a PC

« Reply #3 - Posted 2003-07-08 04:30:34 »

I havn't played around with nio yet but as I understand it, it is a completely new "design" of using sockets. The advantage is that you can don't need one thread for each client connected to your server. This is a big issue with servers that should handle over 1000 client connections at the same time. 1000 thread isn't something that will bost your server preformence exaclty. So if your writting a preformence critical server/client application look in to NIO(or if you just like to learn it) but as you said there is a lack of good documentation. Don't know anything about that book unfortunate.

Offline elias

Senior Devvie

« Reply #4 - Posted 2003-07-08 08:26:28 »

NIO is good for getting rid of threads, that's right. And yes, that gives better scalability for obscene amounts of clients - but the the absolute best part is avoiding the multithreading code, locking etc. That's why I always use NIO, even though I only expect a few clients on my server (<20).

- elias

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

bilznatch (24 views)
2015-08-04 11:03:17

SHC (39 views)
2015-08-01 03:58:20

Jesse (24 views)
2015-07-29 04:35:27

Riven (45 views)
2015-07-27 16:38:00

Riven (25 views)
2015-07-27 15:35:20

Riven (27 views)
2015-07-27 12:26:13

Riven (18 views)
2015-07-27 12:23:39

BurntPizza (41 views)
2015-07-25 00:14:37

BurntPizza (53 views)
2015-07-24 22:06:39

BurntPizza (33 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!