3 import java.net.*; public class Server implements Runnable{ public void run() { final int PORT = 12345; try { ServerSocket serverSocket = new ServerSocket(PORT)); System.out.println("Server started. Waiting for a client..."); Socket clientSocket = serverSocket.accept(); System.out.println("Client connected: " + clientSocket); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Client: " + inputLine); out.println("Server received: " + inputLine); } } catch (IOException e) { e.printStackTrace(); } } } Server Runnable
Winter 2023 Copyright. These slides can only be used as study material for the class CSC308 at Cal Poly. They cannot be distributed or used for another purpose.