Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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 1645 times)
0 Members and 1 Guest are viewing this topic.
Offline nech_neb

Junior Duke




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
server??

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 Duke




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 java.sun.com
http://java.sun.com/docs/books/tutorial/networking/datagrams/index.html

It explains what UDP is, how to use it in Java. Read especielly this part
http://java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
//CLIENT CODE
String bufStr = "ping";
byte[] buf = bufStr.toBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.send(packet);

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

String t = new String(packet.getData()).trim();
System.out.println(t);


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
/SERVER CODE

//receive packet from client
socket.receive(packet);

//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.
socket.send(packet);


Hope this helps.

Offline nech_neb

Junior Duke




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 java.net 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
here...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Backmask

Junior Duke




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 Duke





« 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!