Skip to content

Commit 0f5bb41

Browse files
committed
Updated chat
1 parent f0178b6 commit 0f5bb41

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

Diff for: src/sockets/chat/Chatroom.java

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

Diff for: src/sockets/chat/ProcessInput.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sockets.chat;
2+
3+
import java.util.Scanner;
4+
5+
// Implementing runnable allows us to run it in a separate thread
6+
public class ProcessInput implements Runnable {
7+
Scanner sin;
8+
9+
// Takes in a scanner from socket
10+
public ProcessInput(Scanner sin) {
11+
this.sin = sin;
12+
}
13+
14+
@Override
15+
public void run() {
16+
try {
17+
// Task 2: Parse the custom header. ("I am s-chenrob")
18+
// String name = sin.nextLine(); ...
19+
20+
String name = sin.nextLine();
21+
System.out.println(name);
22+
while (true) {
23+
// EDIT CODE BELOW THIS LINE
24+
25+
String line = sin.nextLine();
26+
27+
// Task 0: Print the line to the console.
28+
System.out.println(line);
29+
// Task 1: Print it with some styling, e.g. "> hello"
30+
31+
// EDIT CODE ABOVE THIS LINE
32+
}
33+
} catch (Exception e) {
34+
// Task 5: Better error handling?
35+
e.printStackTrace();
36+
}
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)