Skip to content
Snippets Groups Projects
Commit 12c18e71 authored by tbavl's avatar tbavl
Browse files

Bavlsik Assignment 2 Updates

parent f6a735ea
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,8 @@ import java.io.PrintWriter; ...@@ -9,6 +9,8 @@ import java.io.PrintWriter;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -17,6 +19,7 @@ import java.util.Set; ...@@ -17,6 +19,7 @@ import java.util.Set;
* @author tbavlsik * @author tbavlsik
*/ */
public class BavlsikServer { public class BavlsikServer {
// the set clientWriters contains form all sockets the active PrintStream // the set clientWriters contains form all sockets the active PrintStream
private static Set<PrintWriter> clientWriters = new HashSet<>(); private static Set<PrintWriter> clientWriters = new HashSet<>();
...@@ -24,25 +27,38 @@ public class BavlsikServer { ...@@ -24,25 +27,38 @@ public class BavlsikServer {
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) {
ArrayList passcodes = new ArrayList<>(Arrays.asList(1, 29, 97));
System.out.println(BavlsikServer.class.getName() + " has started..."); // it helps debugging to put this on console first System.out.println(BavlsikServer.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 Handler handler = new Handler(listener.accept()); // create a new thread writing and reading to and from the new socket
OutputStream os = handler.socket.getOutputStream(); OutputStream os = handler.socket.getOutputStream();
PrintStream ps = new PrintStream(os); PrintStream ps = new PrintStream(os);
InetAddress remoteAddress = handler.socket.getInetAddress(); InetAddress remoteAddress = handler.socket.getInetAddress();
int remotePort = handler.socket.getPort(); int remotePort = handler.socket.getPort();
String message = remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " has connected to the server."; 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!"); BufferedReader reader = new BufferedReader(new InputStreamReader(handler.socket.getInputStream()));
for (PrintWriter writer : clientWriters) { ps.println("Enter a passcode:");
String passInput = reader.readLine(); // read the entire line as a string
int pass = Integer.parseInt(passInput.trim());
if (passcodes.contains(pass)) {
ps.println("Welcome " + remoteAddress.getHostName() + "=" + remoteAddress.getHostAddress() + ", " + remotePort + " to the group chat!");
for (PrintWriter writer : clientWriters) {
writer.println(message); writer.println(message);
} }
ps.flush(); ps.flush();
handler.start(); handler.start();
} else {
ps.println("Not a valid passcode.");
handler.socket.close();
}
} }
}catch (IOException e) { } catch (IOException e) {
System.err.println("Problem with " + BavlsikServer.class.getName() + " networking: " + e); System.err.println("Problem with " + BavlsikServer.class.getName() + " networking: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time // Provide more helpful information to user if exception occurs due to running twice at one time
...@@ -50,10 +66,11 @@ public class BavlsikServer { ...@@ -50,10 +66,11 @@ public class BavlsikServer {
System.err.println("*** Be sure to stop any other running instances of programs using this port!"); System.err.println("*** Be sure to stop any other running instances of programs using this port!");
} }
} }
} }
private static class Handler extends Thread { private static class Handler extends Thread {
public final Socket socket; public final Socket socket;
private PrintWriter out; private PrintWriter out;
private BufferedReader in; private BufferedReader in;
...@@ -74,7 +91,7 @@ public class BavlsikServer { ...@@ -74,7 +91,7 @@ public class BavlsikServer {
String message; 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); System.out.println(outputMessage);
for (PrintWriter writer : clientWriters) { for (PrintWriter writer : clientWriters) {
writer.println(outputMessage); writer.println(outputMessage);
...@@ -95,5 +112,5 @@ public class BavlsikServer { ...@@ -95,5 +112,5 @@ public class BavlsikServer {
} }
} }
} }
} }
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