Talkers

A Talker is a server, similar to a MUD or IRC, that lets people log in and chat. Unlike IRC, it isn't distributed across relay servers, and unlike a MUD, it doesn't necessarily include rooms or descriptions of objects. The Talker that is implemented here is very simple, but extensible.

A Simple Server

It's important that you understand how to code a simple server. This server will respond to TCP connections on the given port. The code fragment below is illustrative of how to create and handle connections with a single-threaded, single-user, blocking server.
try
{
    // create a new server socket
    ServerSocket server = new ServerSocket(5555);

    // Socket that represents the current connection
    Socket connection;

    // Buffered readers and writers to communicate
    // with the user over the socket
    BufferedReader in;
    BufferedWriter out;

    // loop forever
    while (true)
    {
        // accept a connection on the server socket and
        // pass it to the current connection variable
        // This will block until a connection is made.
        connection = server.accept();

        // get the I/O from the connection and assign
        // them to the buffered readers and writers
        in = new BufferedReader(new BufferedInputStream(connection.getInputStream());
	out = new BufferedWriter(new BufferedOutputStream(connection.getOutputStream());

	// do something with the socket
        process(in, out);
 
        // done processing? close the socket
        connection.close();

        // null out variables so the garbage collector has a chance
        in = null;
        out = null;
        connection = null;
    }
}

Server Sockets

Java makes creating a server socket very easy (especially compared to languages like C). Server sockets are end points for communication initiated by a client socket. Server sockets are assigned to a particular port number (1 to 65k) and may not occupy a port number to which another server socket has been bound. Additionally, only the root or administrator account should bind to ports below 1024. Examples of common port numbers that are associated with protocols include:
Service Port
Telnet 23
SMTP 25
HTTP 80
POP3 110
HTTPS 443

The accept Call

ServerSocket.accept() blocks--that is, thread execution halts until a connection is made on the server socket by the client socket. It returns a Socket which represents the connection to the server.

The Proper Care and Feeding of Sockets

Socket implements the methods needed to send and receive data (via I/O streams), as well as manage some aspects of the socket lifecycle. Note that Socket will not directly indicate if it has lost the connection so it is up to the server and the client to signal end of session or catch I/O exceptions that indicate that the socket has been disconnected.