Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  Code request: NIO sockets  (Read 3353 times)
0 Members and 1 Guest are viewing this topic.
Offline kenrod

Senior Newbie




Penfold?


« Posted 2002-11-10 20:36:53 »

Could anybody post some code to handle non-blocking Sockets with 1.4's NIO, as opposed to the 'traditional' approach of starting a new Thread for each connection (which quickly breaks under hundreds of Threads)?

A simple chat server (similar to the thousands of 'traditionally' written ones that are already out there) would be ideal.

Thanks.
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #1 - Posted 2002-11-11 08:18:52 »

I have that code, but too much to post here....  Embarrassed

Have you checked the common tutorials? I started with this one:

http://www.owlmountain.com/tutorials/NonBlockingIo.htm

Does not take you very far, but it's a beginning.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline markuskidd

Junior Devvie


Medals: 1



« Reply #2 - Posted 2002-11-11 16:18:00 »

Is it really going to be worth the trouble of switching to NIO rather than just having a List of Sockets and checking to see whether there is something available() before doing something with it?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #3 - Posted 2002-11-11 19:06:58 »

The question arises when to call available()!
You won't want a server to loop and poll, or to act in timeslices. So you have to have one thread per connection hanging in a blocking read. Buhhhh.....

NIO is definitely worth the effort, together with the beautiful ByteBuffer which I think are much easier to handle than DataOutputStreams e.g.

Effort has to be taken in any case, so why not NIO?

kenrod, check the tutorial and in case problems arise beyond it, contact me privately. As I said, I have some code here willing to share, but it's not suitable for a forum.

BTW, take care! ByteBuffers have nasty bugs in 1.4.0 (at least one). Make sure to have 1.4.1! (Took me hours to find out...)

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline markuskidd

Junior Devvie


Medals: 1



« Reply #4 - Posted 2002-11-11 19:28:11 »

Quote

The question arises when to call available()!
You won't want a server to loop and poll, or to act in timeslices. So you have to have one thread per connection hanging in a blocking read. Buhhhh.....


Well, I guess that's what I've got at the moment. One thread polling all of my sessions several times a second to see if there is anything available(). That's how I answered the question Cheesy though I don't know yet how my Session class handle under stress, so I'm interested in someone's experience re: the difference between the two.

Quote

NIO is definitely worth the effort, together with the beautiful ByteBuffer which I think are much easier to handle than DataOutputStreams e.g.

Effort has to be taken in any case, so why not NIO?
...
BTW, take care! ByteBuffers have nasty bugs in 1.4.0 (at least one). Make sure to have 1.4.1! (Took me hours to find out...)


That is one reason I'm wary about trying to learn. I have been frusterated enough times with J2SE libraries not working quite as advertised to generally avoid using features where there are not well-documented tutorials available. I'm still a newbie! I have enough of my *own* bugs to worry about.

You're a great resrouce for me :hehe:. Anything concrete/numerical you can offer up about the advantages of me switching over right now? I'm writing a game server for a game which is not very bandwith hungry (you can run two instances of the client over a 56k connection simultaneously) but I hope to be able to support 100-200 players on Pentium II-class machines. Worth the trouble of being preemptive you think?
Offline Herkules

Senior Devvie




Friendly fire isn't friendly!


« Reply #5 - Posted 2002-11-13 07:44:45 »

Quote

Anything concrete/numerical you can offer up about the advantages of me switching over right now? Worth the
trouble of being preemptive you think?


I started from scratch and not even tried traditional Socket code. So I have nothing to compare with.

Maybe, your server need kind of a heartbeat anyway - then its completely reasonable to check all incoming lines frequently. Otherwise its a waste of CPU resources.
Depending on how fast you make your server poll the incoming lines, this might be a source of a significant additional latency. If you poll 10/s, you add an artificial average of about 50ms, which is more than the network itself might have caused.
So it really depends on your needs. If your turnbased e.g.....

With NIO, you handle packages immediately when they arrive. This has other drawbacks of course, for its difficult for the server to accumulate messages before sending them out again.


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline SpongeBob

Junior Devvie




Who lives in a pinnapple under the sea


« Reply #6 - Posted 2002-12-26 01:51:33 »

opposed to the 'traditional' approach of starting a new Thread for each connection

Thread pool.  Get 10 threads for 100 connections.  NIO is nice too.
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.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (37 views)
2014-12-09 22:41:13

BurntPizza (72 views)
2014-12-08 04:46:31

JscottyBieshaar (34 views)
2014-12-05 12:39:02

SHC (46 views)
2014-12-03 16:27:13

CopyableCougar4 (42 views)
2014-11-29 21:32:03

toopeicgaming1999 (110 views)
2014-11-26 15:22:04

toopeicgaming1999 (96 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
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!