Wiiter mit Client/Server, minor changes

This commit is contained in:
Sebastian Lenzlinger 2022-03-22 15:47:07 +01:00
parent 59a92fa145
commit e8157a6a91
2 changed files with 12 additions and 5 deletions

View File

@ -33,7 +33,11 @@ public class ClientHandler implements Runnable{
while(socket.isConnected()) { while(socket.isConnected()) {
try { try {
msg = in.readLine(); msg = in.readLine();
if( msg.equals("QUIT")){
broadcastMessage("Client: " + clientUserName + " has left the Server");
}
broadcastMessage(msg); broadcastMessage(msg);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -51,7 +55,7 @@ public class ClientHandler implements Runnable{
client.out.newLine(); client.out.newLine();
client.out.flush(); client.out.flush();
} else { } else {
client.out.write("Message +" + msg + "* sent!"); client.out.write("Message: **" + msg + "** sent!");
client.out.newLine(); client.out.newLine();
client.out.flush(); client.out.flush();
} }

View File

@ -3,11 +3,13 @@ package ch.unibas.dmi.dbis.cs108.Multiplayer.Server;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.Scanner;
public class Server { public class Server {
private static final int gamePort = 42069; private static final int gamePort = 42069;
private ServerSocket serverSocket; private ServerSocket serverSocket;
Scanner sc = new Scanner(System.in);
public Server(ServerSocket serverSocket) { public Server(ServerSocket serverSocket) {
this.serverSocket = serverSocket; this.serverSocket = serverSocket;
@ -15,15 +17,16 @@ public class Server {
public void startServer() { public void startServer() {
try { try {
while (!serverSocket.isClosed()) {
Socket socket = serverSocket.accept();
System.out.println("Port 42069 open on "); System.out.println("Port 42069 open on ");
while (!serverSocket.isClosed()) {
if(sc.next().equalsIgnoreCase("quit")) {
this.closeServerSocket();
}
Socket socket = serverSocket.accept();
ClientHandler nextClient = new ClientHandler(socket); ClientHandler nextClient = new ClientHandler(socket);
Thread th = new Thread(nextClient); Thread th = new Thread(nextClient);
th.start(); th.start();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();