Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  Socket stays open?  (Read 2189 times)
0 Members and 1 Guest are viewing this topic.
Offline Xyle
« Posted 2009-01-04 06:20:03 »

I have a very simple console Client Server chat going, but when I close the Client by pressing the x button, the Server still shows that the socket is open by checking socket.isConnected() and if(in == null), neither one of these checks work after I close the client window with x. Anyone have any tips for this?

I have also tried setting the socket.keepAlive(true) and sending a message to the client, but after client closes, I still dont have anything showing the client has closed. No exceptions thrown and no messages from my handler.

Thanks for any help provided.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Renoria

Junior Member




...


« Reply #1 - Posted 2009-01-04 10:12:00 »

I use mina and use sessionClosed to determine whether a connection is closed.
Offline Xyle
« Reply #2 - Posted 2009-01-04 10:15:53 »

Ok, after alot of research I found there are 2 alternatives to handling the Client Closing.

1 - Create a pinger method from the server to the client, the client recieves the ping and sends its own message back to the server, once recieved at the server the variables get reset and after a bit of time the server resends the ping. If the server doesnt recieve the ping response back from the client, the server closes the socket. This is the solution I chose to incorporate.

2 - Use the Runtime Class addShutdownHook() which would monitor the client and when the client shuts down, send a message to server letting server know the client shut down and close the socket on the server appropriately.

Thanks for looking into this and I hope the posting has helped anyone with similiar problems.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Xyle
« Reply #3 - Posted 2009-01-04 10:18:41 »

thanks Renoria,

The reason I chose to not use any other api's is to try to keep the sending and recieving of messages as simple as possible in order to use this architecture to build multiplayer games down the road on top of it. I do appreciate your response and if I ever incorporate a fully functioning chat I will look into Mina to use.

Thank you.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Renoria

Junior Member




...


« Reply #4 - Posted 2009-01-04 10:21:50 »

In my game the shutdownhook won't work because theres 2 servers, LoginServer and GameServer. If you're disconnected from the GameServer you return to the loginserver. If your'e gonna implement this kind of distributed handling you should use either Ping or Mina.
Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2009-01-05 02:44:32 »

I have a very simple console Client Server chat going, but when I close the Client by pressing the x button, the Server still shows that the socket is open by checking socket.isConnected() and if(in == null), neither one of these checks work after I close the client window with x. Anyone have any tips for this?

I have also tried setting the socket.keepAlive(true) and sending a message to the client, but after client closes, I still dont have anything showing the client has closed. No exceptions thrown and no messages from my handler.

Thanks for any help provided.

Read up on the java doc and on tcp.

Unless you close the socket on iether side fin never gets send and thus you won't see anything happening on the other side, that is untill it times out.
If you wait long enough it will work. if you want immediate feedback, clean up after you. Unload/release resources, close the socket manually.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Xyle
« Reply #6 - Posted 2009-01-05 03:12:58 »

Thanks for the response,

The reason for the post was to handle unexpected client closing, either through client accidently clicking the x, client pc crashing, or lost internet connection.

I am using an intermittent pinging from the server that the client must respond to or get disconnected on the server side and I like how it is working.

Thank you for the response.

Life is just a game, learn to play!
------------------------------------------
╬-YellzBellz Games!-╬ Cheesy
Offline Mr_Light

Senior Member




shiny.


« Reply #7 - Posted 2009-01-05 04:00:12 »

The reason for the post was to handle unexpected client closing, either through client accidently clicking the x, client pc crashing, or lost internet connection.
From a server point of view this will always be a time out.

I am using an intermittent pinging from the server that the client must respond to or get disconnected on the server side and I like how it is working.
You can challange the client for an ack if you haven't send or recieved data from it for a while. If it doesn't recieve the data for whatever reason (including automatic resends) the socket will time out. The client doesn't actually have to 'respond'.  Though you probably don't need it, in any game your going to send state 'often' enough. Though with a chat program I suppose there is an senario where you can end up sending nothing indefinitely.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
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.

atombrot (21 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (65 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!