Skip to content
Snippets Groups Projects
Commit 87ea1dfe authored by Simon32220's avatar Simon32220
Browse files

Homework2: Adding handshake with password

parent 34bc8694
No related branches found
No related tags found
No related merge requests found
...@@ -8,7 +8,8 @@ import java.net.ServerSocket; ...@@ -8,7 +8,8 @@ import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
/** /**
* * Homework1: Open a socket and waiting for a client to connect to the socket.
*
* @author simonschnitzler * @author simonschnitzler
*/ */
public class SchnitzlerUnicastNetworking { public class SchnitzlerUnicastNetworking {
......
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
## Description ## Description
Modification of TcpExample3 and adding some code to implement a chat room with multiple clients. Modification of TcpExample3 and adding some code to implement a chat room with multiple clients.
The 'SchnitzlerServer' class sets up a server that listens for incoming client connections on port 2317. When a client connects, the server creates a new handler thread for managing communication with that client. Each client's messages are broadcast to all other connected clients. The 'SchnitzlerServer' class sets up a server that listens for incoming client connections on port 2317. When a client connects, the server creates a new handler thread for managing communication with that client. For a successful connection a password must be entered. After that each client's messages are broadcast to all other connected clients.
The 'SchnitzlerClient' class connects to a server running on localhost at port 2317. It sends user input from the console to the server and displays messages received from the server. The 'SchnitzlerClient' class connects to a server running on localhost at port 2317. It sends user input from the console to the server and displays messages received from the server.
\ No newline at end of file
...@@ -8,7 +8,8 @@ import java.net.Socket; ...@@ -8,7 +8,8 @@ import java.net.Socket;
import java.util.Scanner; import java.util.Scanner;
/** /**
* * Homework2: Client which connect to a server and prints all incoming messages of the server to the console.
*
* @author simonschnitzler * @author simonschnitzler
*/ */
public class SchnitzlerClient { public class SchnitzlerClient {
...@@ -21,25 +22,28 @@ public class SchnitzlerClient { ...@@ -21,25 +22,28 @@ public class SchnitzlerClient {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Socket socket = new Socket(LOCALHOST, 2317); Socket socket = new Socket(LOCALHOST, 2317);
new Thread(new Reader(socket)).start(); Thread connection = new Thread(new Reader(socket));
connection.start();
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) { while (scanner.hasNextLine() && connection.isAlive()) {
out.println(scanner.nextLine()); out.println(scanner.nextLine());
} }
} }
private static class Reader implements Runnable { private static class Reader implements Runnable {
private BufferedReader in; private BufferedReader in;
private final Socket socket;
public Reader(Socket socket) throws IOException { public Reader(Socket socket) throws IOException {
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); this.socket = socket;
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
} }
@Override @Override
public void run() { public void run() {
try { try (socket){
String message; String message;
while ((message = in.readLine()) != null) { while ((message = in.readLine()) != null) {
System.out.println(message); System.out.println(message);
......
...@@ -3,8 +3,6 @@ package MV3500Cohort2024JulySeptember.homework2.Schnitzler; ...@@ -3,8 +3,6 @@ package MV3500Cohort2024JulySeptember.homework2.Schnitzler;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
...@@ -13,7 +11,8 @@ import java.util.HashSet; ...@@ -13,7 +11,8 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
* * Homework2: Chatserver, which is waiting for clients to connect. The clients need to enter a password for a successfull connection. After a successfull connection all messages will be sent to all clients.
*
* @author simonschnitzler * @author simonschnitzler
*/ */
public class SchnitzlerServer { public class SchnitzlerServer {
...@@ -27,20 +26,15 @@ public class SchnitzlerServer { ...@@ -27,20 +26,15 @@ public class SchnitzlerServer {
System.out.println(SchnitzlerServer.class.getName() + " has started..."); // it helps debugging to put this on console first System.out.println(SchnitzlerServer.class.getName() + " has started..."); // it helps debugging to put this on console first
try (ServerSocket listener = new ServerSocket(2317)) { try (ServerSocket listener = new ServerSocket(2317)) {
while (true) { while (true) {
Handler handler = new Handler(listener.accept()); // create a new thread writing and reading to and from the new socket Socket socket = listener.accept();
OutputStream os = handler.socket.getOutputStream(); int remotePort = socket.getPort();
PrintStream ps = new PrintStream(os); InetAddress remoteAddress = socket.getInetAddress();
String message = remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " tries to connect to the server.";
InetAddress remoteAddress = handler.socket.getInetAddress();
int remotePort = handler.socket.getPort();
String message = remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " has connected to the server.";
System.out.println(message); System.out.println(message);
ps.println("Welcome " + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " to the group chat!"); Handler handler = new Handler(socket);
for (PrintWriter writer : clientWriters) { handler.start();
writer.println(message);
}
ps.flush();
handler.start();
} }
}catch (IOException e) { }catch (IOException e) {
System.err.println("Problem with " + SchnitzlerServer.class.getName() + " networking: " + e); System.err.println("Problem with " + SchnitzlerServer.class.getName() + " networking: " + e);
...@@ -67,19 +61,49 @@ public class SchnitzlerServer { ...@@ -67,19 +61,49 @@ public class SchnitzlerServer {
try { try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true); out = new PrintWriter(socket.getOutputStream(), true);
synchronized (clientWriters) { InetAddress remoteAddress = socket.getInetAddress();
int remotePort = socket.getPort();
out.println("Speak, friend, and enter!");
String answer = in.readLine();
if("Mellon".equals(answer)){
String message = remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " has connected to the server.";
System.out.println(message);
out.println("Welcome " + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " to the group chat!");
for (PrintWriter writer : clientWriters) {
writer.println(message);
}
out.flush();
synchronized (clientWriters) {
clientWriters.add(out); clientWriters.add(out);
} }
String message; while ((message = in.readLine()) != null) {
while ((message = in.readLine()) != null) { String outputMessage = socket.getInetAddress().getHostName() + "=" + socket.getInetAddress().getHostAddress() + ", " + socket.getPort()+ ": " + message;
String outputMessage = socket.getInetAddress().getHostName() + "=" + socket.getInetAddress().getHostAddress() + ", " + socket.getPort()+ ": " + message; System.out.println(outputMessage);
for (PrintWriter writer : clientWriters) {
writer.println(outputMessage);
}
}
String outputMessage = socket.getInetAddress().getHostName() + "=" + socket.getInetAddress().getHostAddress() + ", " + socket.getPort()+ " has disconnected!";
System.out.println(outputMessage); System.out.println(outputMessage);
for (PrintWriter writer : clientWriters) { for (PrintWriter writer : clientWriters) {
writer.println(outputMessage); writer.println(outputMessage);
} }
} }
else{
try (socket) {
String message = remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + ": Connection denied!";
System.out.println(message);
out.println(message);
socket.close();
}catch (IOException e) {
System.out.println("Couldn't close a socket, what's going on?");
}
}
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error handling client: " + e); System.out.println("Error handling client: " + e);
} finally { } finally {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment