-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatServer.txt
115 lines (99 loc) · 3.82 KB
/
ChatServer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.*;
import java.net.*;
import java.util.ArrayList;
class ChatServer {
ArrayList<ChatClient> clients = new ArrayList<>();
ServerSocket serverSock;// server socket for connection
static Boolean running = true; // controls if the server is accepting clients
public static void main(String[] args) {
new ChatServer().go(); //start the server
}
public void go() {
System.out.println("Waiting for a client connection..");
Socket client = null;//hold the client connection
try {
serverSock = new ServerSocket(5555); //assigns an port to the server
//serverSock.setSoTimeout(50000); //100 second timeout
while(running) { //this loops to accept multiple clients
client = serverSock.accept(); //wait for connection
System.out.println("Client connected");
Thread t = new Thread(new ConnectionHandler(client));//create a thread for the new client and pass in the socket
t.start(); //start the new thread
}
}catch(Exception e) {
System.out.println("Error accepting connection");
//close all and quit
try {
client.close();
}catch (Exception e1) {
System.out.println("Failed to close socket");
}
System.exit(-1);
}
}
private void buildClientList(String username, int status, String ipAddress){
ChatClient client = new ChatClient(username,status,ipAddress);
if(!clients.contains(client)) {
clients.add(client);
}
}
//***** Inner class - thread for client connection
class ConnectionHandler implements Runnable {
private PrintWriter output;
private BufferedReader input;
private Socket client;
private boolean running;
private String clientInput;
private OutputStream outstream;
private byte[] serverOutput;
/* ConnectionHandler
* Constructor
* @param the socket belonging to this client connection
*/
ConnectionHandler(Socket s) {
this.client = s; //constructor assigns client to this
try { //assign all connections to client
//System.out.println(client.getInetAddress());
this.output = new PrintWriter(client.getOutputStream());
InputStreamReader Instream = new InputStreamReader(client.getInputStream());
this.input = new BufferedReader(Instream);
} catch (IOException e) {
e.printStackTrace();
System.out.println("test: " + e.getMessage());
}
running = true;
} //end of constructor
/* run
* executed on start of thread
*/
public void run() {
String msg = "";
String key;
while (running) {
try {
if (input.ready()) {
msg = input.readLine();
//this.output.write("test");
// System.out.println("test");
System.out.println("msg from client! " + msg);
//running=false;
}
} catch (IOException e) {
System.out.println("Failed to receive msg from the client");
e.printStackTrace();
}
}
//Send a message to the client
output.println("");
output.flush();
//close the socket
try {
input.close();
output.close();
client.close();
} catch (Exception e) {
System.out.println("Failed to close socket");
}
}
}
}