publicstaticvoidmain(String[] args) { try (ServerSocketserverSocket=newServerSocket(PORT)) { System.out.println("Server is listening on port " + PORT);
while (true) { try (Socketsocket= serverSocket.accept(); BufferedReaderin=newBufferedReader(newInputStreamReader(socket.getInputStream())); PrintWriterout=newPrintWriter(socket.getOutputStream(), true)) {
System.out.println("New client connected");
String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println("Received from client: " + inputLine); out.println("Server received: " + inputLine); } } catch (IOException e) { System.err.println("Error handling client connection: " + e.getMessage()); } } } catch (IOException e) { System.err.println("Could not listen on port " + PORT + ": " + e.getMessage()); } } }