Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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  
  Shutting down the network loop  (Read 1096 times)
0 Members and 1 Guest are viewing this topic.
Offline JackalSama

Junior Newbie





« Posted 2005-09-03 13:32:10 »

Hi there!

I'm trying to make a little chat server using Swing and NIO.  I've got the main class based on JFrame, and a ServerBase object (based on the code from http://java.about.com/gi/dynamic/offsite.htm?zi=1/XJ&sdn=java&zu=http%3A%2F%2Fwww.owlmountain.com%2Ftutorials%2FNonBlockingIo.htm) that does all the networking jazz.  The problem is that when I shut the main window, the program doesn't actually exit.

This is what the main networking method looks like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
   public void acceptConnections() throws IOException, InterruptedException
   {
      SelectionKey acceptKey = selectableChannel.register( selector, SelectionKey.OP_ACCEPT );
      System.out.println( "Acceptor loop..." );
      while( ( keysAdded = acceptKey.selector().select() ) > 0 )
      {
         System.out.println( "Selector returned " + keysAdded + " ready for IO operations" );
         
         Set readyKeys = selector.selectedKeys();
         Iterator i = readyKeys.iterator();
         while( i.hasNext() )
         {
            SelectionKey key = (SelectionKey)i.next();
            i.remove();
           
            if( key.isAcceptable() )
            {
               ServerSocketChannel nextReady = (ServerSocketChannel)key.channel();
               System.out.println( "Processing selection key" +
                  " read=" + key.isReadable() +
                  " write=" + key.isWritable() +
                  " accept=" + key.isAcceptable()
                  );
               
               SocketChannel channel = nextReady.accept();
               channel.configureBlocking( false );
               SelectionKey readKey = channel.register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE );
               readKey.attach( new ChannelCallback( channel ) );
            }
            else if( key.isReadable() )
            {
               SelectableChannel nextReady = (SelectableChannel)key.channel();
               System.out.println( "Processing selection key" +
                  " read=" + key.isReadable() +
                  " write=" + key.isWritable() +
                  " accept=" + key.isAcceptable()
                  );
               
               readMessage( (ChannelCallback)key.attachment() );
            }
            else if( key.isWritable() )
            {
               ChannelCallback callback = (ChannelCallback)key.attachment();
               // Have to find a way to actually ... you know... do stuff.
               String message = "What is your name?";
               ByteBuffer buf = ByteBuffer.wrap( message.getBytes() );
               int nBytes = callback.getChannel().write( buf );
            }
         }
      }
     
      System.out.println( "End acceptor loop." );
   }


From what I can tell, it just sort of sits at the 'while( blah blah )' until it gets something over the network, right?  But since I haven't written a client yet, that never happens.  hehe

For that matter, how can I hook up an event handler of some sort to catch when the application is shutting down, so that I can clean all this up?

Thanks in advance~
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #1 - Posted 2005-09-03 16:56:32 »

Have you tried
1  
theJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

?

Otherwise the program would continue running (independently from all the other stuff going on)
Offline JackalSama

Junior Newbie





« Reply #2 - Posted 2005-09-03 17:05:05 »

Yeah, the frame is already set to close like that...

I worked around it by putting my test server into a console app  Tongue

Actually, now I'm running into a brand new slew of problems...  I can find a bunch of tutorials and whatnot about creating a server using NIO, but not a client...  Do people not use NIO for writing clients?  I'm very new to NIO...  (and it's been a while since I've done anything in Java anyway Wink)

I'm going to take a trip to the book store today to see if they've got any books of interest there!  Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #3 - Posted 2005-09-12 21:04:21 »

You can find some basic info around here - it's a common subject of discussion. Not having used NIO myself, it is said that large servers (100 connections and such) must use them to get good performance. Also some say that the "nio way" is nicer than ordinary io/net, for clients too. It shouldn't be critical for client purposes though.

I find it very strange that your program doesn't close correctly. Are you absolutely sure that everything is set correctly?
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.

Gibbo3771 (9 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
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!