Skip to content

Commit 3803489

Browse files
committed
Initial commit
1 parent 601baec commit 3803489

7 files changed

+402
-1
lines changed

AcceptThread.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// MIT License
2+
3+
// Copyright (c) 2017 Matthew Chen, Arc676/Alessandro Vinciguerra
4+
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import java.io.*;
24+
import java.util.*;
25+
import java.net.*;
26+
27+
public class AcceptThread extends Thread{
28+
29+
public boolean running = true;
30+
31+
private ServerSocket sock;
32+
private Server server;
33+
34+
public AcceptThread(ServerSocket sock, Server server){
35+
this.sock = sock;
36+
this.server = server;
37+
}
38+
39+
public void run(){
40+
while (running){
41+
try {
42+
Socket cSock = sock.accept();
43+
server.addClientHandler(new ClientHandler(cSock, server));
44+
} catch (SocketException e){
45+
if (e.getMessage().equals("Socket closed")){
46+
running = false;
47+
break;
48+
}
49+
} catch (Exception e) {
50+
System.err.println("Oh noes!");
51+
e.printStackTrace();
52+
}
53+
}
54+
}
55+
56+
}

Client.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// MIT License
2+
3+
// Copyright (c) 2017 Matthew Chen, Arc676/Alessandro Vinciguerra
4+
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import java.io.*;
24+
import java.net.*;
25+
import java.util.*;
26+
27+
//port number: 4267
28+
29+
public class Client{
30+
31+
//terminal IO
32+
private Scanner sc;
33+
34+
//network IO
35+
private Socket sock;
36+
private PrintWriter out;
37+
private BufferedReader in;
38+
private int port;
39+
private String host;
40+
41+
private String username;
42+
43+
private MsgThread msgThread;
44+
45+
public Client(String[] args) {
46+
sc = new Scanner(System.in);
47+
if (args.length == 3){
48+
username = args[0];
49+
host = args[1];
50+
try {
51+
port = Integer.parseInt(args[2]);
52+
} catch (NumberFormatException e){
53+
System.err.println("Invalid port input. Using default port 4267.");
54+
port = 4267;
55+
}
56+
} else {
57+
while (true){
58+
System.out.print("Enter username: ");
59+
username = sc.nextLine();
60+
if (username.equals("server") || username.equals("global")){
61+
System.out.println("Error: reserved usernames cannot be used");
62+
} else {
63+
break;
64+
}
65+
}
66+
System.out.print("Enter host: ");
67+
host = sc.nextLine();
68+
try {
69+
System.out.print("Enter port number: ");
70+
port = Integer.parseInt(sc.nextLine());
71+
} catch (NumberFormatException e){
72+
System.err.println("Invalid port input. Using default port 4267.");
73+
port = 4267;
74+
}
75+
}
76+
try {
77+
sock = new Socket(host, port);
78+
out = new PrintWriter(sock.getOutputStream(), true);
79+
out.println(username);
80+
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
81+
msgThread = new MsgThread(in, null, username);
82+
msgThread.start();
83+
} catch (IOException e){
84+
System.err.println("IOException occurred.");
85+
return;
86+
}
87+
88+
run();
89+
}
90+
91+
private void run(){
92+
System.out.println("Begin chat with " + host + " on port " + port);
93+
String inLine = "";
94+
while (true){
95+
String toSend = sc.nextLine();
96+
out.println(username + ": " + toSend);
97+
if (toSend.equals("/disconnect") || !msgThread.running){
98+
System.out.println("Exiting...");
99+
break;
100+
} else {
101+
System.out.println(username + ": " + toSend);
102+
}
103+
}
104+
try {
105+
msgThread.running = false;
106+
sock.close();
107+
} catch (IOException e){
108+
System.err.println("err....");
109+
}
110+
System.out.println("Chat terminated");
111+
}
112+
113+
public static void main(String[] args) {
114+
new Client(args);
115+
}
116+
117+
}

ClientHandler.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// MIT License
2+
3+
// Copyright (c) 2017 Matthew Chen, Arc676/Alessandro Vinciguerra
4+
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import java.net.*;
24+
import java.io.*;
25+
26+
public class ClientHandler{
27+
public PrintWriter out;
28+
public BufferedReader in;
29+
public MsgThread msgThread;
30+
public String username;
31+
32+
public ClientHandler(Socket clientSocket, Server server){
33+
try{
34+
out = new PrintWriter(clientSocket.getOutputStream(), true);
35+
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
36+
username = in.readLine();
37+
server.broadcastToClients(username + " connected", "global");
38+
msgThread = new MsgThread(in, server, username);
39+
msgThread.start();
40+
}catch(Exception e){
41+
System.out.println("Error by the way");
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
public void send(String text){
47+
out.println(text);
48+
}
49+
50+
public void stopRunning(){
51+
msgThread.running = false;
52+
}
53+
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017
3+
Copyright (c) 2017 Matthew Chen, Arc676/Alessandro Vinciguerra
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

MsgThread.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// MIT License
2+
3+
// Copyright (c) 2017 Matthew Chen, Arc676/Alessandro Vinciguerra
4+
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
import java.io.*;
24+
import java.util.*;
25+
import java.net.*;
26+
27+
public class MsgThread extends Thread {
28+
29+
public boolean running = true;
30+
31+
private BufferedReader in;
32+
private Server server;
33+
private String username;
34+
35+
public MsgThread(BufferedReader in, Server server, String username){
36+
this.in = in;
37+
this.server = server;
38+
this.username = username;
39+
}
40+
41+
public void run(){
42+
String inLine = "";
43+
while (running){
44+
try {
45+
inLine = in.readLine();
46+
if (inLine == null || inLine.substring(inLine.indexOf(":") + 2).equals("/disconnect")){
47+
print(username + " disconnected");
48+
running = false;
49+
} else {
50+
print(inLine);
51+
}
52+
} catch(SocketException e){
53+
if(e.getMessage().equals("Socket closed")){
54+
System.out.println("Socket closed");
55+
running = false;
56+
}
57+
} catch (IOException e){
58+
e.printStackTrace();
59+
}
60+
}
61+
}
62+
63+
private void print(String text){
64+
if (server != null){
65+
server.broadcastToClients(text, username);
66+
} else {
67+
System.out.println(text);
68+
}
69+
}
70+
}

README.TXT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROJECT TITLE: Chat Program
2+
VERSION or DATE: 1.1
3+
AUTHORS: Matthew Chen, Arc676/Alessandro Vinciguerra
4+
LICENSE: MIT; See LICENSE for more details

0 commit comments

Comments
 (0)