Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 843 times)
0 Members and 1 Guest are viewing this topic.
Offline JackalSama

Junior Newbie





« Posted 2005-09-03 15: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 Member




Java games rock!


« Reply #1 - Posted 2005-09-03 18: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 19: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!
Try the Free Demo of Revenge of the Titans
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #3 - Posted 2005-09-12 23: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.196 seconds with 21 queries.