working multi client

This commit is contained in:
andrzej 2024-11-12 10:13:53 +01:00
parent 53c846a386
commit d4572677f5
5 changed files with 5 additions and 4 deletions

BIN
Client.class Normal file

Binary file not shown.

View File

@ -10,7 +10,7 @@ class Client {
public static void main(String[] args) { public static void main(String[] args) {
// establish a connection by providing host and port // establish a connection by providing host and port
// number // number
try (Socket socket = new Socket("localhost", 1234)) { try (Socket socket = new Socket("localhost", 8080)) {
// writing to server // writing to server
PrintWriter out = new PrintWriter( PrintWriter out = new PrintWriter(

BIN
Server$ClientHandler.class Normal file

Binary file not shown.

BIN
Server.class Normal file

Binary file not shown.

View File

@ -13,8 +13,8 @@ class Server {
Socket client = server.accept(); Socket client = server.accept();
System.out.println("New client: " + client.getInetAddress().getHostAddress()); System.out.println("New client: " + client.getInetAddress().getHostAddress());
ClientHandler clientsock = new ClientHandler(client); ClientHandler clientSock = new ClientHandler(client);
new Thread(clientSock.start()); new Thread(clientSock).start();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -31,7 +31,7 @@ class Server {
private static class ClientHandler implements Runnable { private static class ClientHandler implements Runnable {
private final Socket clientSocket; private Socket clientSocket;
public ClientHandler(Socket socket) { public ClientHandler(Socket socket) {
this.clientSocket = socket; this.clientSocket = socket;
@ -59,6 +59,7 @@ class Server {
} }
if (in != null) { if (in != null) {
in.close(); in.close();
System.out.println("closing socket " + clientSocket.getInetAddress().getHostAddress());
clientSocket.close(); clientSocket.close();
} }
} catch (IOException e) { } catch (IOException e) {