Skip to main content
gamedev.net
🔒 Locked

[java] Networking library

Started by apocalypstic 9 replies 2.4k views
Original Post
apocalypstic
apocalypstic
Does there exist a good networking library for Java? Something like RakNet for C++ ? I have googled for half an hour now and all that I could find was QuickServer.org, which only supports the server side.
5MinuteGaming
5MinuteGaming
I don't mean to be rude or anything apocalypstic but why are you looking for a networking library for Java when the Java API support is quiet enough for anything that you want to do.

If you want support for interfacing with a certain Protocol or perhaps an already created Server architecture then you should have been more specific.

so I would suggest that you look into the java.net package if you are looking for a higher level API then you will have to be more specific in your search.
Son of Cain
Son of Cain
Yes, the API provides all that is necessary to build your networkin' code.

But if you're going to use asynchronous networking, and you don't want to mess around with the poor Sun documentation on Java NIO, I strongly advise you to give MINA a go. It's easy to use, protocol based, and it has wonderful "performance".

But if you don't have the need for asynchronous networking, I agree with 5MinuteGaming here - the java.net package has all that is necessary to build a reliable network framework for your game / application.
a.k.a javabeats at yahoo.ca
5MinuteGaming
5MinuteGaming
Well, as far as support equivalent to RakNet for C++ is concerned I could not find a java API with specific support for game networking.

MINA I would argue is not the answer for Game Networking and is an abstraction on top of java.net package. It does offer the benefit of hidding the synchronization issues from the developer which can be a pain especially with netorking code.

Depending on your networking needs and what applications you wish to develop MINA may be a good choice. However I would not recommend it for the average game programmer. Also the javadocs for mina aren't impressive the tutorial gives a skim through of how MINA's is to be used but not quiet complete.

As far as java.nio goes I would say that it has more complete javadocs and thus more documentation than MINA. The wiki for MINA is a little weak and only explains the basics of the entire API which is left for you to expirement with.

In addition the java API also has the RMI package which is something to look into.

apocalypstic:
I am curious as to what exactly you are looking for in a networking library?
apocalypstic
apocalypstic

Quote:

I am curious as to what exactly you are looking for in a networking library?


I am looking for something that is packet based like RakNet, not stream based. My main gripe with the existing java.net sockets is that they dont seem to support binary transmission, ie you have to append an "\n" onto everything because it gets sent down the stream.

I think Im just gonna have to do with the java sockets for my small project. It will be a good learning experience :p
5MinuteGaming
5MinuteGaming
Quote:
Original post by apocalypstic

Quote:

I am curious as to what exactly you are looking for in a networking library?


I am looking for something that is packet based like RakNet, not stream based. My main gripe with the existing java.net sockets is that they dont seem to support binary transmission, ie you have to append an "\n" onto everything because it gets sent down the stream.

I think Im just gonna have to do with the java sockets for my small project. It will be a good learning experience :p


I'm a little confused with the statement that Sockets don't seem to support binary transmission. Sockets use the Input and Output streams which support sending arrays of bytes or single bytes which you can manipulate with binary operators and pack them to use less space. However with the current technology and hardware it is less efficient to do bit or byte packing it takes about as long if not longer to pack the data than to just send the bytes across a network.

java.net also supports packet based networking you can use the DatagramSocket to send DatagramPackets which are basically just a group of bytes however the draw back to using DatagramSockets is that you are not garraunteed that the socket will be received. So you end up having to implement a way to notify the client/server when packets are received at the other end.

Hope that helps!


Aldacron
Aldacron
Quote:
Original post by apocalypstic
I am looking for something that is packet based like RakNet, not stream based.


RakNet's packets are an intermediate layer between the user code and the networking layer. Their existence is just the result of a design decsion. RakNet could just as easily use internal headers and not provide 'packets' for the user at all. Implementing a packet protocol yourself is nothing, even on top of java.net's streams. No matter the newtorking API you use, all you need is a layer that can translate packets into a format the API understands, and vice-versa:

packets < -- > translation layer < -- > networking API

Quote:
My main gripe with the existing java.net sockets is that they dont seem to support binary transmission, ie you have to append an "\n" onto everything because it gets sent down the stream.


You aren't seeing the big picture :) You can transmit binary data with java.net. One way is to pack the values into output/input streams yourself, and another is to use ObjectOutputStreams and ObjectInputStreams. But really any modern Java networking app should be using NIO, at least on the server. NIO has several adavantages over java.net - one is that you can use non-blocking IO and don't need a thread per client; another is that SocketChannels allow you to use ByteBuffers, which can be more efficient than the streams from the IO package because they can be allocated in native memory (they also make it easy to store data from your objects with all of the put and get methods).

Getting down and dirty with NIO is not all that easy though because of the dearth and quality of documentation online. There are some decent introductory books out there, but I haven't seen anything that shows you more than the basics. That should be enough to get you started. The rest is just trial and error. If you do decide to go with the old java.net package instead, I think you'll find the book Java Network Programming (the Manning book) very useful. It has some really practical examples along with great explanations. There's a section on packing binary data into IO streams, a great section on multithreading, info on how to develop your own IO streams, and more. O'Reilly has a book by the same name, but I find the Manning book more informative. I do recommend NIO over java.net, though.
Son of Cain
Son of Cain
That's why my advice was to take a look at MINA. It's built on top of Java NIO, and its code could at least give you some ideas, if you don't want to use its "custom-protocol" based approach.

It's a fact that it is quite undocumented, but keep in mind this is a new project from Apache, at its initial state. If you have the spirit - and the time - you could even help improving it.
a.k.a javabeats at yahoo.ca
hplus0603
hplus0603
Quote:
why are you looking for a networking library for Java when the Java API support is quiet enough for anything that you want to do


I can think of several reasons:

- You might want a library that provides higher-level primitives (i e "object replication" not "sending bits and bytes").
- You might think the serialization support of Java is too verbose for games use.
- You might want to make a different trade-off in latency and reliability than most other uses of Java.

There is a project by Sun called Project Darkstar, also known as the Sun Game Server. However, this is mostly aimed at a transactional view of games (RPG interactions, skill uses, etc), not at fast-paced massively multiplayer physics with dozens of updates per player per second. (More details on the javagaming darkstar message board)

enum Bool { True, False, FileNotFound };
SteveTaylor
SteveTaylor
Do you want something connection-based like TCP/IP, or a connectionless protocol? If you're not sure, what sort of game are you making?

If you decide to use TCP/IP and want the performance of asynchronous I/O without the bloat of Apache Mina, you could have a look at my small library that is quite small and easy to use.

Topic Locked

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

Sign in to reply to this topic.