To understand the simple Java Socket Thread sever, we need to know the Networking and Thread. This post presents a simple sockets-based program to introduce the concepts of sockets and multi-threaded programming.

First, you need to know What are Sockets and know how client and server communicates. Second, if you only have single thread of sever, your client is waiting in queue. So a multi-threaded program performs multiple tasks at one time such as fielding simultaneous requests from many client programs.

The Socket Communications can give you a whole picture.

But only Threads server is not enough. So we can add Thread Pool to recycle our threads. You can create your own thread pool, like this or use Java 1.5 Thread Pool.

Update: This post uses Thread per connection, if you want to know how to do Thread per request, you can check “How NIO web server can rescue Reverse Ajax”.

Reference:
1. Server Thread Code for Java
2. Sockets programming in Java
3. Advanced Socket Programming
4. A process-driven approach to avoid thread death
5. Programming Java threads in the real world, Part 8
6. Sockets programming in Java
7. Write your own threaded discussion forum: The communications and server components, part 2

About these ads