File tree Expand file tree Collapse file tree
src/main/java/org/example Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33public class App {
44 public static void main (String [] args ) {
5- System .out .println ("Hello There!" );
6-
5+ SocketServer .main ();
76 }
87}
Original file line number Diff line number Diff line change 1+ package org .example ;
2+
3+ import java .io .IOException ;
4+ import java .net .ServerSocket ;
5+ import java .net .Socket ;
6+
7+ public class SocketServer {
8+
9+ static void main () {
10+ int port = 3000 ;
11+
12+ try (ServerSocket serverSocket = new ServerSocket (port , 64 )) {
13+
14+ System .out .println ("Server started at port: " + serverSocket .getLocalPort ());
15+
16+ while (true ) {
17+ Socket socket = serverSocket .accept ();
18+ Thread .ofVirtual ().start (() -> handleClient (socket ));
19+ }
20+
21+ } catch (IOException e ) {
22+ throw new RuntimeException (e );
23+ }
24+ }
25+
26+ static void handleClient (Socket socket ) {
27+ try {
28+ socket .close ();
29+ } catch (IOException e ) {
30+ e .printStackTrace ();
31+ }
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments