Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] java.net and java.nio question

Started by mrhodes May 7, 2005 at 10:50 AM 11 replies 3.9k views
Original Post
mrhodes
mrhodes
Hey guys and gals, I'm starting out with java coming from a c/c++ background and I'm researching the networking capabilities of java. So far I like what I see. Much easier to get going with it. My question though is this... I started reading about Sockets and the java.net classes and all seemed well. Then I discovered java.nio and to me that seems much better for server related programs with multiple clients. Most tutorials I find don't seem to use the nio classes though. I would like to know if by using the channel / buffer / selector approach do I still need to use the java.net classes ? Or are they all "built-in" to the New I/O classes. Thanks for any answers :) Mike PS: Sorry that simple question is so wordy ;)
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net
BobV
BobV
NIO is newer; hence N(ew)IO. That's perhaps why you don't see as many tutorials. I recommend the book 'Developing Games in Java'. It's got a very good section on using NIO in a game server. I recommend ignoring the old sockets and learn NIO. NIO is non blocking so you can handle all your channels in 1 thread.
TheBluMage
TheBluMage
Hmm. I thought the 'N' in NIO stood for native. Could be wrong though.
Son of Cain
Son of Cain
Hi,

Though it's 'N' of "New", it does not mean "immature" =D

The book suggested is just great, really worth buying. The source code of all the books' examples is here. The NIO framework is described in chapter 6.
a.k.a javabeats at yahoo.ca
Aldacron
Aldacron
Quote:
Original post by BobVNIO is non blocking so you can handle all your channels in 1 thread.


NIO supports nonblocking IO, but you can use blocking IO if you'd like.

Glass_Knife
Glass_Knife
I've done some non-blocking IO, and written client and servers using this stuff. Here is the best place to start...
http://forum.java.sun.com/thread.jsp?forum=4&thread=459338
There is tons of examples, including a full blown server if you care to study the code (there is lots). However, for simple things, I would just do it with blocking calls. All of the java methods that can block have a timeout, and I believe there is a way to see how much data you can read before it blocks, and just read that much. Even if you use the NBIO stuff, it will still need to be in its own thread because calling selector.select() blocks until something happens. It would be easier to make a thread, and write the code. To get the server running bug free took me three weeks.

Again, if you do try, I've done it, so feel free to ask any questions.
Hope this helps.
mrhodes
mrhodes
Glass_Knife:
Thanks for the offer to help out.... I do have some questions about making my server and client programs. I plan on making my server in C/C++ on linux and my client in Java, an applet to be specific. btw, the program I'm making here is a video conferencing application. I'm currently reading about Buffers and so far I haven't found what I'm looking for. I'd like to define my own packet format as a structure in C. Then use that to send data back and forth from server to applet. Now, I'm just wondering how I am going to take the packet data out of a ByteBuffer when I read it from a SocketChannel in Java. This must be possible, right? Another thing, to use channels in nio, do both ends have to be channels, or can I do this with a c/c++ server, and java applet?

I'll also check out the link you gave me... thanks

Thanks for any advice :)

Michael Rhodes
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net
Son of Cain
Son of Cain
About the C++ and Applet NIO implementation, that must be possible with wrapping methos written for JNI. Maybe you could grab a reference for the winsock and wrap it as a SocketChannel?

About using the Buffers, maybe this will help? (taken from http://brackeen.com/javagamebook/)

/** * NIOUtility.java */package net.java.dev.bta.network.nio.common;import java.nio.*;import java.nio.channels.*;/** * * Miscelanious of methods to work with NIO * From: http://brackeen.com/javagamebook/ */public class NIOUtility {        /**     *     * First, it writes the Header. Then, it writes the GameEvent into the     * given ByteBuffer. That's when the Channel writes out the data.     * @param event The event to be written     * @param writeBuffer The ByteBuffer to be prepared     */    public static void prepBuffer(GameEvent event, ByteBuffer writeBuffer) {                writeBuffer.clear();        writeBuffer.putInt(0); // clientId?                int sizePos = writeBuffer.position();        writeBuffer.putInt(0); // placeholder for payload size                // write event        int payloadSize = event.write(writeBuffer);                // insert the payload size in the placeholder spot        writeBuffer.putInt(sizePos, payloadSize);                // prepare for a channel.write        writeBuffer.flip();            }            /**     * Writes the contents of a ByteBuffer to the given SocketChannel     * @param channel The Channel where the data will be written     * @param writeBuffer The ByteBuffer containing the data to be written     */    public static void channelWrite(SocketChannel channel, ByteBuffer writeBuffer) {                long nbytes = 0;        long toWrite = writeBuffer.remaining();                // loop on the channel.write() call since it will not necessarily        // write all bytes in one shot        try {            while (nbytes != toWrite) {                nbytes += channel.write(writeBuffer);                                try {                    Thread.sleep(Globals.CHANNEL_WRITE_SLEEP);                } catch (InterruptedException e) {}            }        } catch (ClosedChannelException cce) {        } catch (Exception e) {        }                // get ready for another write if needed        writeBuffer.rewind();            }        /**     * Write a String to a ByteBuffer,     * Prepended with a short integer representing the length of the String     * @param buff The ByteBuffer to receive the String     * @param str The String to be written on the ByteBuffer buff     */    public static void putStr(ByteBuffer buff, String str) {        if (str == null)            buff.putShort( (short) 0 );        else {            buff.putShort( (short) str.length() );            buff.put( str.getBytes() );        }    }        /**     * Read a String from a ByteBuffer that was written w/the putStr method     * @param buff The ByteBuffer to read from     */    public static String getStr(ByteBuffer buff) {        short len = buff.getShort();        if (len == 0)            return null;        else {            byte[] b = new byte[ len ];            buff.get(b);            return new String(b);        }    }    }
a.k.a javabeats at yahoo.ca
Glass_Knife
Glass_Knife
Quote:
Original post by mrhodes
I'm currently reading about Buffers and so far I haven't found what I'm looking for.

What exactly are you looking for about buffers?
Quote:
Original post by mrhodes
Now, I'm just wondering how I am going to take the packet data out of a ByteBuffer when I read it from a SocketChannel in Java.

The ByteBuffer has ways to get the data out. Here is an example of getting bytes out.
byte[] data = new byte[buffer.limit()];buffer.get( data );

Quote:
Original post by mrhodes
Another thing, to use channels in nio, do both ends have to be channels, or can I do this with a c/c++ server, and java applet?

The channel in java has nothing to do with the server in C++. When your applet gets the data, it is just bytes. The applets doesn't know how the bytes got sent, but it needs to get them out.

If you are doing an applet for the client, then there is no reason to use non-blocking IO. It is very complicated, and can be hard to debug. I would just make a thread and have the client listen for data comming in. You can use the timeout to check if it is time to exit the thread. Because of security issues with applets, you can only connect back to host, and since you can only have one connection, there is no reason to make this more complicated. If your client was going to connect to 20 servers, then I could understand.

Hope this helps...
tebriel
tebriel
Yea, it seems Sun (and alot of junk on the internet) gives lots of examples of how to use the parts of NIO, but almost nothing about how to put it all together and use it in an actual application. The best thing I've seen are stupid little "echo servers" which are all over the place and leave alot up to the developer to figure out for anything more complicated. (And an "echo server" is about as NON-complex as it can get.) At least as far as what I've seen.

If you want to learn NIO, the online resources and Sun stuff is ok...I did manage to "figure it out" using only that, but a book would have been a better help for this topic. I'd say definitely get an actual book about this topic--just MAKE SURE it is a new enough/updated book that it actual uses NIO. A Java NIO book that includes networking, pure Java NIO networking-only book, or other book that includes Java NIO network stuff.
Glass_Knife
Glass_Knife
Quote:
Original post by Tebriel
If you want to learn NIO, the online resources and Sun stuff is ok...I did manage to "figure it out" using only that, but a book would have been a better help for this topic.


The problem is that when someone who has never done any networking stuff before sits down and tries it, they begin to realize all that is involved. TCP/IP vs UDP. Client and server code. Blocking or non-blocking. Single or multiple threads. On and on... When I said I would write a simple messaging system at work, everyone laughed. I didn't understand, until I was in the middle of it. Having said that, I think the hard part about networking code is not the networking, but the mulit-thread issues. There is just no way to do it in a single thread. I wrote the code about 6 months ago, and now...
// exampleClientConnection conn = new ClientConnection();conn.addIOListener( myMessageHandler );conn.start();conn.connect( /*address*/ );// .. I'm done!!!


I guess that once you figure something out, its not hard. It took a while, but I could write another server now in a couple of days if I needed to.
tebriel
tebriel
I'm sure that's part of it.

Let me rephrase my sentence a bit for emphasis, though:

If you want to learn NIO, the online resources and Sun stuff is, well...ok....meh

i.e. It leaves some things to be desired, let's put it that way. It's rare that internet resources ever fail you totally, but this is one area that I think you need to actually pay someone (buy their book) to have them teach it to you properly.

Falls into the same category as the "Java Look and Feel" theme things...there's so much work to do it (correctly) that not many people bother with doing it for free. We're agreeing, because you said that there's alot of work involved with networking. Yep. And Java's a pretty damn high level language, just think about how much fun it is in C. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.