views:

627

answers:

9

I am trying to write a simple networked chat program in Java. I have almost no networking experience. I was wondering what resources I should begin looking at (beside here of course).

Sticking with the core Java API would be best for now.

+11  A: 

I found a great tutorial into networking and java from sun's own website: http://download.oracle.com/javase/tutorial/networking/TOC.html

The socket section even has you write a mini client / server chat demo.

zxcv
+2  A: 

Nio or the traditional way with ServerSocket or Socket See java.net package

Nio docs here.

Iulian Şerbănoiu
For the original poster's knowledge, NIO supports non-blocking IO, whereas the traditional form doesn't. Blocking IO is single threaded; non-blocking is multithreaded. If you're transferring files over a chat client and still want users to be able to type, NIO.
Dean J
+1  A: 

Sun's Java API and official tutorials are probably the best place to get your feet wet.

Magsol
A: 

Thought of this one too, but you were faster finding it :-)

Edit: Man... Stackoverflow is so damn fast... My post was thought as a response to the first entry and after posted there are two more. Awesome!

GHad
A: 

Google is your friend. Search for "java socket programming tutorial" or something like that and you'll get lots of results, including the one suggested by zxcv as well as these:

http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html

http://www.cafeaulait.org/books/jnp/javanetexamples/index.html

Alan Krueger
+1  A: 

It's much more straight-forward than you would think. Honestly I'd just start browsing through the javadocs for the nio package. They should even contain mini-tutorials and source code.

Beyond that, java.sun.com should be littered with tutorials.

If you don't understand sockets---well I could send you to a reference but it's easier to just tell you--sockets are a way 2 programs talk to each other. They are just a unique number that (when combined with your IP address) give you a unique path to a program. So if I "Listen" on port (socket) 1000, then another program connects to port 1000, anything the connecting program sends, the listening program receives.

Use a high port number (higher than, say, 5000) because there are many programs that assign their own port.

This is how virtually everything on your computer communicates.

You might want to read a really brief intro to socket communications if the API is still confusing.

Bill K
+1  A: 

I recommend you to first learn networking. If you have time read the Tanenbaum book, the greatest reference in networking. If you want a quick leard, here is a road map:

  • OSI layers
  • UDP and TCP/IP
  • Sockets
  • Broadcast and Multicast
  • Network security

Then go with Java: Socket, ServerSocket, DatagramSocket, RMI, etc.

Marcio Aguiar
A: 

"Head First Java" is a great beginners book and they do a tutorial on creating a simple chat program.

http://oreilly.com/catalog/9780596004651/

Corey Goldberg
+1  A: 

Here's a pretty basic, easy to read Java networking tutorial too:

http://tutorials.jenkov.com/java-networking/index.html

Jakob Jenkov