Java Sockets
A socket is one end of a network conversation between two programs. The server side waits on a port, and a port is a number the two sides agree on.
An echo server handles one client
The server binds port 0 and prints the port the system picked, after the word PORT. It reads one line, prints what arrived, and sends the same line back with an Echo label.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(0);
try {
System.out.println("PORT " + server.getLocalPort());
Socket client = server.accept();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
String line = in.readLine();
System.out.println("Received: " + line);
String reply = "Echo: " + line;
out.println(reply);
System.out.println("Replied: " + reply);
} finally {
client.close();
}
} finally {
server.close();
}
}
}The client starts the conversation
The client takes the port number the server printed as its first argument. It connects to that port on the same machine, sends the hello server line, and prints the reply line it receives.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws Exception {
int port = Integer.parseInt(args[0]);
Socket socket = new Socket("127.0.0.1", port);
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("hello server");
System.out.println(in.readLine());
} finally {
socket.close();
}
}
}Example
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(0);
try {
System.out.println("PORT " + server.getLocalPort());
Socket client = server.accept();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
String first = in.readLine();
System.out.println("Received: " + first);
out.println("Echo: " + first);
String second = in.readLine();
System.out.println("Received: " + second);
if (second.equals("who are you")) {
out.println("I am Echo, the server");
} else {
out.println("Unknown question");
}
System.out.println("Served 1 client");
} finally {
client.close();
}
} finally {
server.close();
}
}
}Key points
- A ServerSocket bound to port 0 lets the system pick a free port.
- The accept method waits until a client connects, then returns a socket for that one client.
- A socket carries an input stream for reading and an output stream for writing.
- Each side reads lines from its partner and writes lines back.
- Closing a socket ends that conversation.
In this example, the server holds one conversation of two lines with one client. The first line earns an echo reply, the second line asks for a name, and the server answers with its name line before it prints the served count.
Note
The server and client here share one machine, but the same code works across machines with a different address.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A client connects to a waiting ServerSocket. What does the accept call return?