|
| 1 | +package sockets.chat; |
| 2 | + |
| 3 | +import java.io.PrintStream; |
| 4 | +import java.net.InetAddress; |
| 5 | +import java.net.ServerSocket; |
| 6 | +import java.net.Socket; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +/* |
| 10 | + * README |
| 11 | + * |
| 12 | + * How to use: |
| 13 | + * Make sure you and your friend are on the same network, e.g. BSDSecure |
| 14 | + * Have one person host the server, another person connect to the server |
| 15 | + * |
| 16 | + * Ctrl + f for "Task" in both Chatroom.java and ProcessInput.java to make the chatroom actually work. |
| 17 | + * If you complete all 6 tasks (0 - 5), you get candy after break O: |
| 18 | + */ |
| 19 | +public class Chatroom { |
| 20 | + static final int PORT = 8080; |
| 21 | + static Scanner in; |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + // See |
| 25 | + // https://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html |
| 26 | + // for more information about InetAddress |
| 27 | + InetAddress inetAddress = InetAddress.getLocalHost(); |
| 28 | + System.out.println("IP Address: " + inetAddress.getHostAddress()); |
| 29 | + System.out.println("Host Name: " + inetAddress.getHostName()); |
| 30 | + |
| 31 | + System.out.println("What do you want to do"); |
| 32 | + System.out.println("1. Host a server"); |
| 33 | + System.out.println("2. Connect to a server"); |
| 34 | + |
| 35 | + in = new Scanner(System.in); |
| 36 | + |
| 37 | + switch (in.nextInt()) { |
| 38 | + case 1: |
| 39 | + hostServer(); |
| 40 | + break; |
| 41 | + case 2: |
| 42 | + System.out.print("Give me an ip to connect to: "); |
| 43 | + connect(in.next()); |
| 44 | + break; |
| 45 | + default: |
| 46 | + System.out.println("Not an option :/"); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static void hostServer() throws Exception { |
| 52 | + InetAddress inetAddress = InetAddress.getLocalHost(); |
| 53 | + System.out.println("Started a new server on " + inetAddress.getHostAddress()); |
| 54 | + ServerSocket ss = new ServerSocket(PORT); |
| 55 | + |
| 56 | + Socket s = ss.accept(); |
| 57 | + |
| 58 | + // Task 4: Try making it so you can accept multiple clients |
| 59 | + process(s); |
| 60 | + |
| 61 | + /* |
| 62 | + * while(true) { Socket s = ss.accept(); |
| 63 | + * |
| 64 | + * // The "() -> process(s)" is a lambda expression new Thread(() -> |
| 65 | + * process(s)).start(); } |
| 66 | + */ |
| 67 | + } |
| 68 | + |
| 69 | + public static void connect(String ip) throws Exception { |
| 70 | + Socket s = new Socket(ip, PORT); |
| 71 | + |
| 72 | + process(s); |
| 73 | + } |
| 74 | + |
| 75 | + public static void process(Socket s) throws Exception { |
| 76 | + System.out.println("Connected to " + s.getRemoteSocketAddress()); |
| 77 | + System.out.println(); |
| 78 | + |
| 79 | + Scanner sin = new Scanner(s.getInputStream()); |
| 80 | + PrintStream sout = new PrintStream(s.getOutputStream()); |
| 81 | + |
| 82 | + // Task 3: Prompt the user for a username to use |
| 83 | + // Send a header establishing who we are |
| 84 | + sout.println("I am " + System.getProperty("user.name")); |
| 85 | + |
| 86 | + // Multithreading magic allows us to both read and write at the same |
| 87 | + // time! |
| 88 | + new Thread(new ProcessInput(sin)).start(); |
| 89 | + |
| 90 | + Scanner in = new Scanner(System.in); |
| 91 | + while (true) { |
| 92 | + String line = in.nextLine(); |
| 93 | + sout.println(line); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments