Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  implementing network  (Read 7483 times)
0 Members and 1 Guest are viewing this topic.
Offline sunsett

Senior Member




ribbit!


« Reply #30 - Posted 2006-03-02 19:39:28 »

Typically, you would want the client to send a message to the server telling it that it wants to disconnect.  If the connection terminates abruptly you should receive an exception.  The way I deal with this in UDP was to have my own "expiration" for clients that don't tell the server that they're disconnecting and it will remove that client forcefully.

-Matt Hicks
Offline Kova

Senior Member





« Reply #31 - Posted 2006-03-02 20:10:23 »

Yeah, quit message... ok.
But in abnormal drop of connection, I don't get Exception using SocketChannel. Client, telnet (also testing with it) or whatever disconnects, closing SocketChannel on his side, ... and server goes beserk with events. Since TCP maintains connection alive I have expected something like Exception or closing event... but java docs tell so little and I'm tired of reading those few tutorials there are online. I can't implement timeout since server is bombed with events immidietly. Hmm... now I started to wonder.. what kind of event is that anyway? Maybe it's close event hidden in read event form! Got to go test it!

Edit: I printed out bits and it's event for OP_READ, but buffer is empty. So when channel.read(readBuffer) returns 0 or -1 it is sign that connection is down and to close channel afterall. Java doc is so missguiding about this, they could wrote that also, not just techinical crap.
Offline Jeff

JGO Coder




Got any cats?


« Reply #32 - Posted 2006-03-03 06:10:23 »

Edit: I printed out bits and it's event for OP_READ, but buffer is empty. So when channel.read(readBuffer) returns 0 or -1 it is sign that connection is down and to close channel afterall.

On your system for that situation.

This sounds liek something that would be very OS dependant so I would not count on it unless you actually test it on all your targets,.

And OS dependant behavior is *never* Javadoc'd for that reason.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #33 - Posted 2006-03-03 12:09:46 »

dang... you're possibly right. I'm using Win XP btw.
So does anyone actually knows how to test if connection has been closed on other side?
Offline sunsett

Senior Member




ribbit!


« Reply #34 - Posted 2006-03-03 16:46:29 »

Like I said previously, I just handle timeouts instead (each machine sends NOOPs if no message has been broadcast over that connection for a while so a timeout doesn't occur because of lack of activity).  You will get an exception if you try to send a message back to the closed connection and that can also be a way to determine its closed state.

-Matt Hicks
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #35 - Posted 2006-03-03 17:05:16 »

Jeff, it may sound like OS-specific behaviour, but it's not.

The SocketChannel javadoc:

Quote
public abstract long read(ByteBuffer[] dsts,
                          int offset,
                          int length)

Returns:
The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream



When the channel is closed while writing, you get an IOE
When reading, you get the remaining bytes, then -1
When idling, the channel becomes readable, then returns -1 on read.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sunsett

Senior Member




ribbit!


« Reply #36 - Posted 2006-03-03 18:33:40 »

Riven,

I think what Jeff was conveying was the 0 or -1 when the connection is closed, not just when the end of that particular stream has been reached.

Detecting when the socket is closed on reading is not something you can reliably do since it's a passive operation.

-Matt Hicks
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #37 - Posted 2006-03-03 19:00:32 »

I think Riven was saying the channel becomes readable, i.e. the select() returns the key as "readable" - so its not really passive since you'll have something blocking on select() or polling on selectNow() somewhere. When the socket gets flagged as readable you go and read it and the -1 is returned - since you've been told the channel is readable and that infact a -1 is returned you now know the channel is dead.

Kev

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #38 - Posted 2006-03-03 20:23:41 »

Indeed, it's not passive!

I said: "the channel becomes readable", which is active.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sunsett

Senior Member




ribbit!


« Reply #39 - Posted 2006-03-03 22:31:22 »

My mistake.

I might go back and update my code to use that instead.  Roll Eyes

-Matt Hicks
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #40 - Posted 2006-03-03 23:49:56 »

Thank you all for helping.
Matt, as I said when connection closes you get bombarded with read events until you close the connection on your side... and can't do anything else... i couldn't even get windows task manager to open until I switched to java application and closed it (thus killing server and read events). So i doubt you have that problem assuming you at least tested it for abnormal connection closing.

... and when I thought you tried to convince me to use JGN instead of learning how to write my own networking Smiley ... just kidding  Tongue
Offline sunsett

Senior Member




ribbit!


« Reply #41 - Posted 2006-03-04 15:13:34 »

You're using NIO for your read events?  In JGN I don't have that problem.  I essentially call the update method that checks to see if there are any packets awaiting delivery (checking to see if the size is greater than 0) and if so I read it, otherwise I ignore and just return control back to the thread that called the update method.

Only a fool would say their code is perfect....there's always more to learn and always more to improve. :-p

I can't say I'm a networking professional really myself.  In fact, the desire to write JGN was inspired really from my lack of desire to write networking as odd as that might seem.  I wanted to create a project that would contain all the features necessary for any networking game I might write so that once it's stable I don't have to concern myself with networking anymore.  Roll Eyes

-Matt Hicks
Pages: 1 [2]
  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.

ctomni231 (39 views)
2014-07-18 06:55:21

Zero Volt (36 views)
2014-07-17 23:47:54

danieldean (29 views)
2014-07-17 23:41:23

MustardPeter (32 views)
2014-07-16 23:30:00

Cero (47 views)
2014-07-16 00:42:17

Riven (48 views)
2014-07-14 18:02:53

OpenGLShaders (38 views)
2014-07-14 16:23:47

Riven (37 views)
2014-07-14 11:51:35

quew8 (33 views)
2014-07-13 13:57:52

SHC (70 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!