Class 20 — Wednesday, November 4

Two programs that talk to each other: a server and a client, one client at a time, then a thread for each client, and what the clients share.

Two programs on one machine that talk to each other: a server that waits for clients, and a client that connects to it. After the break, many clients at once, and what they share.

  1. A server and a client — predict first
  2. A protocol of your own
  3. Two clients at once — keyboards down
  4. A thread for each client
  5. Four robots — predict first
  6. What the clients share

Today’s work goes in your copy of cmsc-230-threads: new files in src, tests in test. If you have none, make one: on https://github.com/cu-cs-labs/cmsc-230-threads, Use this template, Private, then clone it in IntelliJ.

1. A server and a client

Keyboards down. Write in the Journal first, then run it.

A server waits for clients on a port, a number that says which program on a machine a connection is for. A client connects to a machine and a port; localhost is the machine the client itself runs on. Each side then holds a socket, with an input stream and an output stream that are read and written like a file, a line at a time.

import java.io.*;
import java.net.*;
import java.util.*;

// answers every line a client sends with "echo: " and the line, one client at a time
public class EchoServer {
    public static void main(String[] args) throws IOException {
        try (ServerSocket server = new ServerSocket(6230)) {
            System.out.println("waiting on port 6230");
            while (true) {
                Socket client = server.accept();
                System.out.println("a client connected");
                serve(client);
                System.out.println("the client left");
            }
        }
    }

    // talks to one client until it leaves
    static void serve(Socket client) throws IOException {
        try (Scanner in = new Scanner(client.getInputStream());
             PrintWriter out = new PrintWriter(client.getOutputStream(), true)) {
            while (in.hasNextLine()) {
                String line = in.nextLine();
                out.println("echo: " + line);
            }
        }
    }
}
import java.io.*;
import java.net.*;
import java.util.*;

// sends each line you type to the server on this machine, and prints its answer
public class EchoClient {
    public static void main(String[] args) throws IOException {
        try (Socket server = new Socket("localhost", 6230);
             Scanner in = new Scanner(server.getInputStream());
             PrintWriter out = new PrintWriter(server.getOutputStream(), true);
             Scanner keyboard = new Scanner(System.in)) {
            System.out.println("connected: type a line, or bye to leave");
            while (keyboard.hasNextLine()) {
                String line = keyboard.nextLine();
                if (line.equals("bye")) {
                    break;
                }
                out.println(line);
                System.out.println(in.nextLine());
            }
        }
    }
}

accept() waits until a client connects. nextLine() waits until a whole line arrives. The true in new PrintWriter(..., true) sends each line the moment println writes it.

The server is running. You start the client, type hi, then bye. What does each of the two programs print, in order?

The server prints waiting on port 6230, then a client connected when the client starts, and the client left after bye. Then it waits again: its while (true) goes back to accept() for the next client.

The client prints connected: type a line, or bye to leave, then echo: hi. On bye it leaves the loop and closes its socket, which is what ends the server’s while (in.hasNextLine()).

A client started with no server running stops at once with ConnectException: Connection refused: nobody is waiting on that port.

Two different programs, such as the server and the client: run each from the arrow beside its main. Each gets a tab of its own in the Run window, and they run side by side. You type into the client’s tab.

The same program twice, such as two clients: IntelliJ offers to stop the one that is running instead. To let it run a second, run the program once, then Run ▸ Edit Configurations…, pick it on the left, and Modify options ▸ Allow multiple instances. Each run then opens a tab of its own.

Stopping: the red square in a tab stops that program alone. A server still running from before makes a new one fail with BindException: Address already in use, so stop the old one first.

2. A protocol of your own

Pairs. 2.1 and 2.2, then 2.3 and 2.4 if you are done.

2.1 Run the two

Put both files in src. Run EchoServer, then EchoClient, each from the arrow beside its main; section 1’s note on running multiple programs says more. Type a few lines into the client’s tab, then bye, then run the client again.

If Windows asks whether to let Java through its firewall, Cancel is fine: everything today stays on this machine.

Done when the second run of the client is answered too, by the same server.

2.2 A calculator

A protocol is what the two sides agree to say. Ours: a client sends ADD 2 3 or MUL 4 5, and the server answers 5 or 20. A line it cannot answer gets a line that starts with ERROR and says why.

A new file, Calculator.java, with

static String answer(String line)

the answer to one line; line.split(" ") gives its words. Then CalcServer: EchoServer with Calculator.answer(line) sent back in place of the echo. EchoClient works unchanged, since it prints whatever line comes back.

Your test, in test/CalculatorTest.java, calls answer itself, with no server running: that is why answer is a method of its own. ADD and MUL on numbers of your own, worked out first; and ADD 2 answers a line starting with ERROR, assertTrue(answer.startsWith("ERROR")).

Done when the client gets 5 for ADD 2 3, and your test is green.

2.3 One bad line (if you are done)

From the client, send ADD two 3. What happens to the server? Start the client again: what happens now? Then make answer catch the NumberFormatException that Integer.parseInt throws, and answer with an ERROR line instead.

Your test: ADD two 3 and ADD 99999999999 1 both answer an ERROR line.

2.4 Division (if you are done)

DIV 7 2 answers 3, and DIV 1 0 answers an ERROR line rather than stopping anything.

Your test: both of those, and one division of your own.

When the section ends: commit and push.

3. Two clients at once

Keyboards down.

First, let IntelliJ run the client twice: section 1’s note on running multiple programs says how.

EchoServer is running, and client 1 is connected: it sent hi, got its echo, and sends nothing more. The server is inside serve for client 1, waiting for its next line, and not at accept(). Now client 2 starts.

What happens to client 2, and why?

Client 2 is not refused: new Socket succeeds at once, and so does sending hello. Its nextLine() then waits, with no answer, until client 1 sends bye; and then echo: hello arrives at once.

It is let in because the operating system takes connections, not the program. Once new ServerSocket(6230) has run, the system answers anyone who connects to port 6230 and puts the connection in a queue; accept() only takes the next one out of the queue. The lines a client sends wait in the system too, until someone reads them. So what decides between refused and waiting is whether a ServerSocket exists, not whether accept() is running. With no server at all, nothing is listening, and the system refuses, as in section 1.

It waits because the server has one thread, and that thread is inside serve for client 1. It gets back to accept() only when client 1 leaves; then client 2’s connection comes out of the queue, hello is already there to read, and the echo goes back at once.

4. A thread for each client

Pairs. 4.1, then 4.2 if you are done.

4.1 ThreadedEchoServer

A new file: EchoServer with one change. Instead of calling serve(client) itself, main starts a thread that does, and goes straight back to accept():

new Thread(() -> serve(client)).start();

A Runnable cannot throw an IOException, so serve now catches it: its try gets a catch (IOException e) that prints e.getMessage(), and throws IOException comes off its first line.

Done when two clients connected at once each get their echo at once.

4.2 One bad client (if you are done)

Make CalcServer start a thread for each client the same way. Take 2.3’s catch out of answer for a moment, and from one of two connected clients send ADD two 3. What happens to that client, to the other one, and to the server? Answer in a comment, then put the catch back.

When the section ends: commit and push.

5. Four robots

Keyboards down. Write in the Journal first, then run it.

A server with a thread for each client, counting every line any client sends, and four robot clients that each send it 100,000 lines at once. Two new files in src:

import java.io.*;
import java.net.*;
import java.util.*;

// counts every line from every client; COUNT answers with the count so far
public class CountServer {
    static int count;

    public static void main(String[] args) throws IOException {
        try (ServerSocket server = new ServerSocket(6230)) {
            System.out.println("waiting on port 6230");
            while (true) {
                Socket client = server.accept();
                new Thread(() -> serve(client)).start();
            }
        }
    }

    // counts one client's lines until it leaves
    static void serve(Socket client) {
        try (Scanner in = new Scanner(client.getInputStream());
             PrintWriter out = new PrintWriter(client.getOutputStream(), true)) {
            while (in.hasNextLine()) {
                String line = in.nextLine();
                if (line.equals("COUNT")) {
                    out.println(count);
                } else {
                    count++;
                }
            }
        } catch (IOException e) {
            System.out.println("a client failed: " + e.getMessage());
        }
    }
}
import java.io.*;
import java.net.*;
import java.util.*;

// four robot clients at once, each sending 100,000 lines to CountServer; then
// one more connection asks for the count
public class Robots {
    public static void main(String[] args) throws Exception {
        Thread[] robots = new Thread[4];
        for (int r = 0; r < robots.length; r++) {
            robots[r] = new Thread(() -> {
                try (Socket s = new Socket("localhost", 6230);
                     Scanner in = new Scanner(s.getInputStream());
                     PrintWriter out = new PrintWriter(s.getOutputStream(), true)) {
                    for (int i = 0; i < 100_000; i++) {
                        out.println("hello");
                    }
                    out.println("COUNT");
                    in.nextLine();      // the server has read all of this robot's lines
                } catch (IOException e) {
                    System.out.println("a robot failed: " + e.getMessage());
                }
            });
            robots[r].start();
        }
        for (Thread robot : robots) {
            robot.join();
        }
        try (Socket s = new Socket("localhost", 6230);
             Scanner in = new Scanner(s.getInputStream());
             PrintWriter out = new PrintWriter(s.getOutputStream(), true)) {
            out.println("COUNT");
            System.out.println("the server counted " + in.nextLine() + " lines of 400000");
        }
    }
}

Start CountServer, then Robots. How many of the 400,000 lines will the server count? Why?

Fewer. Six runs here counted 396963, 392769, 394152, 383562, 397389 and 394534.

Each client’s thread runs count++ on the one count, and two threads lose each other’s updates, as two threads did in Class 16. The robots are separate connections, but on the server they are threads of one program sharing one field. A server with a thread for each client is a program with many threads, and what its clients share needs a lock.

6. What the clients share

Pairs. 6.1, then 6.2 and 6.3 if you are done.

6.1 One lock for the count

Move count++ into a method of its own, static synchronized void add(), so that a test can call it and every client’s thread takes the same lock in it. Which object’s lock is that? COUNT reads count under the same lock.

A synchronized block inside add would work too, but it would cover the whole method anyway: the same lock, held just as long, in more lines.

Your test, in test/CountServerTest.java: set CountServer.count to 0, then four threads each call add() 100,000 times; the count is 400,000.

Done when Robots prints 400000 three runs in a row, and your test is green.

6.2 Names (if you are done)

NameServer, with a thread for each client and three commands:

  • NAME alice gives this client a name, and answers OK.
  • HELLO answers hello, alice, or ERROR no name yet.
  • WHO answers the names of every client connected now, on one line.

A client’s own name belongs to that client alone: a local variable in serve. The names of everyone connected belong to all of them: a class Lobby, with join(name), leave(name) and who(), and one Lobby object that every client’s thread uses. When a client leaves, serve takes its name out.

Your test, in test/LobbyTest.java: two names join, who() lists both, one leaves, and who() lists the other.

6.3 Taken (if you are done)

NAME bob, while a connected client is already bob, answers ERROR bob is taken. join returns false and changes nothing when the name is taken, and true when it adds it.

Your test: two threads each join name0 to name999 into one Lobby; the joins that return true add up to 1,000. Without a lock, ten runs here came to between 1,026 and 1,179.

When the section ends: commit and push.