Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  NIO Object Streams  (Read 1816 times)
0 Members and 1 Guest are viewing this topic.
Offline Reynard

Senior Newbie




Java games rock!


« Posted 2003-04-25 19:11:20 »

Hey all!

I'm writing a 3D tennis game at the moment for my final-year college project, and I'm trying to get it to work over a network. To be honest, I was unaware of NIO until quite recently, but it seemed very straightforward and was exactly what I was looking for, so I went ahead and started writing the network code using it. Unfortunately, I've hit a problem.

Basically, I have a Message class which encapsulates all the data I need to send for each event - message ID, player/ball positions and directions, etc. I want to create an Object stream across the socket channel, so I can serialise instances of this class and transmit them back and forth. Whenever I try to set up the stream, however, I get an IllegalBlockingModeException. I've checked through the code, and found that the connection is set up correctly and no exceptions are thrown UNTIL I attempt to create an Object Stream on the channel. I had assumed that the ObjectStream would behave exactly as it would when created on a regular Socket, but would return immediately with null if there was no incoming data. From this exception, however, it seems that Object streams are blocking-specific.

My question is - how can I set up object input/output streams on a non-blocking channel? Are there some permission issues to take into account that I'm not aware of, or are there updated Object stream classes which are designed specifically for NIO? Any help would be much appreciated!

Thanks,
Reynard.

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  
SocketChannel sChannel;

public void setupConnection(boolean client)
{      ServerSocketChannel servChannel;

      try
      {      if(!client)
            {      servChannel = ServerSocketChannel.open();
                  servChannel.configureBlocking(true);
                  servChannel.socket().bind(new InetSocketAddress(4096));

                  sChannel = servChannel.accept();
                  sChannel.configureBlocking(false);

                  servChannel.close();
            }
            else
            {      sChannel = SocketChannel.open();
                  sChannel.configureBlocking(false);
                  sChannel.connect(new InetSocketAddress(hostName, 4096));

                  while(!sChannel.finishConnect())
                        Thread.sleep(25);
            }

            in = new ObjectInputStream(Channels.newInputStream(sChannel));
            out = new ObjectOutputStream(Channels.newOutputStream(sChannel));                        
      }
      catch(Exception e)
      {      System.err.println("Error while establishing connection. Terminating... \n" + e.getMessage());
            System.exit(-1);
      }
}


I should also mention that the ServerSocketChannel in the above example is discarded because this is a straightforward 2-player game - once the first connection is accepted, there is no need to listen for any more. As such, I configured the ServerSocketChannel to block intentionally - I assumed that this wouldn't cause any problems, since I could just set the resulting SocketChannel to non-blocking.
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #1 - Posted 2003-04-25 19:30:21 »

I'm no expert at this (yet Wink), but I think your problem is that you're trying to use java.io streams which inherently block. If you want to do non-blocking I/O, you should use the SocketChannel read/write methods. In fact, I'd go as far as to say you should write your own serialize/deserialize methods on the Message object that return/accept ByteBuffers.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline Reynard

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-04-25 21:21:39 »

Yeah, I figured as much  :-/  But here's a whole NEW thing; I just tried using the traditional Socket and ServerSocket, and the connection itself again worked fine. BUT, when I tried instantiating object input/output streams using the socket's streams, the program stopped dead! I tried wrapping a BufferedReader and PrintWriter around the streams, just to see what'd happen - and it worked perfectly.  Huh It's beginning to look like I'll have to communicate using Strings and such, which is exactly what I wanted to avoid in the first place.  Sad

Does anyone see what might be causing the problem here?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
Socket sChannel;
ServerSocket servChannel;

ObjectInputStream in;
ObjectOutputStream out;

public void setupConnection(boolean client)
{
      try
      {      if(!client)
            {      servChannel = new ServerSocket(4096);
                  sChannel = servChannel.accept();
            }
            else
                  sChannel = new Socket(hostName, 4096);

            in = new ObjectInputStream(sChannel.getInputStream());
            out = new ObjectOutputStream(sChannel.getOutputStream());                        
      }
      catch(Exception e)
      {      System.err.println("Error while establishing connection. Terminating... \n" + e.getMessage());
            System.exit(-1);
      }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Reynard

Senior Newbie




Java games rock!


« Reply #3 - Posted 2003-04-25 21:50:25 »

Aha - it turns out that creating the ObjectInputStream before the ObjectOutputStream can cause programs to hang. I just changed the order, and it worked.
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.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
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!