Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Network / Socket programming tutorial [closed]

I'm going to create a project in some month in a course I'm having and for that purpose I would like to read and try making some small test programs regarding the network / socket programming in Java.

Ideally, what I'm looking for is a tutorial with description and a end program to create where I can between two computers send and resive small and simple text messages. Don't have to be any GUI or something like that just the simple version.

I've read some articles from Sun's own homepage and in one of my Java Book but I seem to be missing the last piece of the puzzle to really understand that "network programming".

In addition, I don't know if I'm mixing things now but to my understanding can the multithreading paradigm also be applied to the network / socket programming.

Therefore, my question is, if anyone knows about a good tutorial or educational link for me to get an overview of this topic. I’m not totally new to Java but haven’t got any experience in this type of programming.

Thanks - Emil

like image 798
Emil Devantie Brockdorff Avatar asked Feb 23 '10 08:02

Emil Devantie Brockdorff


3 Answers

Here is a nice sample chapter from Java Network Programing. There's also some other parts available here, and also sample sources are available.

If you'd decide to buy this book, I'd say these chapters are enough for now:

  • Chapter 2. Basic Network Concepts
  • Chapter 3. Basic Web Concepts
  • Chapter 4. Java I/O
  • Chapter 5. Threads
  • Chapter 6. Looking Up Internet Addresses
  • Chapter 7. URIs and URLs
  • Chapter 9. Sockets for Clients
  • Chapter 10. Sockets for Servers

Don't bother about NIO yet; it's quite advanced.

In addition, I don't know if I'm mixing things now but to my understanding can the multithreading paradigm also be applied to the network / socket programming.

Yes, indeed it's a very important aspect. You'll find out why once you've read the sample chapter.

Have fun!

like image 79
Enno Shioji Avatar answered Oct 17 '22 09:10

Enno Shioji


I've only read the C version of this book, but likely Java version is going to be equally short and good: Calvert and Donahoo's TCP/IP Sockets in Java: Practical Guide for Programmers. Even if you learned socket programming in C, you could probably adapt to Java's implementation fairly quickly.

alt text

like image 3
Eugene Yokota Avatar answered Oct 17 '22 10:10

Eugene Yokota


For basic steps you might wan't to read this tutorial Sockets programming in Java. In this model the most people used one thread to handle one network connection.

In java 1.4 NIO was introduced, this makes intros a bit more complex: Java NIO Tutorial.

NIO allows to use a Selector which allows you to handle many connections in one thread, which leads to higher performance.

like image 2
stacker Avatar answered Oct 17 '22 10:10

stacker