Java Chat Server - Class ServerSocket, Low-Level

The purpose of this assignment was to:

The description and my solution of the Java Chat Server -assignment can be viewed and/or downloaded at the bottom of this page.

What is a Low-level Connection?

See this section in the Java Chat Client -assignment: Low-level Connections with Stream Sockets and TCP

What does Client-Server and Server-side mean?

See this section in the Java Chat Client -assignment: Client-Server Paradigm, Client-Side and Server-Side

What is Java's Class ServerSocket?

Java's class ServerSocket implements sockets on the server-side.

The ServerSocket class has all you need to code servers in Java. It's constructors creates new ServerSocket objects, and it's methods listens for connections on specified ports and returns a Socket object when a connection is up.

The basic steps of a server using Class ServerSocket:

  1. A new ServerSocket is created on a specified port with the ServerSocket( ) constructor.
  2. The ServerSocket listens for incoming connection attempts on that port with its accept( ) method. The accept( ) method blocks until a client tries to make a connection. When a client tries to make a connection the accept( ) method unblocks and returns a Socket object connecting the client and the server.
  3. Depending on the server type, either the Socket's getOutputStream( ) method, getInputStream( ) method, or both of them, are called to get the output and input streams to be used in the communication.
  4. The server and the client communicate according to a protocol they both agreed-upon until it's time for the connection to be closed.
  5. Either the server, the client, or both of them, close the connection.
  6. The server then returns to step 2, i.e. listens for new connections.

Threads are usually a good choice

Since a thread places a smaller load on the server than a complete process, a good practice is to create a thread for each client.

If, however, the protocol used is very simple and allows the server to close the connection when it's finished, it might be more efficient for the server to process client requests immediately without creating a thread.

Just like with sockets for clients using Java's Socket class, the actual work of the ServerSocket class is performed by an instance of the SocketImpl class.

Assignment Description

Create a Java application that implements a Chat Server that distributes incoming client messages to all connected clients. Use Java's class ServerSocket and class Thread.

My Solution, Assignment Files

«  Previous Next  »