Skip to content

Commit 51687d6

Browse files
committed
Updating all files
1 parent 79ce0ff commit 51687d6

File tree

5 files changed

+44
-36
lines changed

5 files changed

+44
-36
lines changed

src/.DS_Store

0 Bytes
Binary file not shown.

src/ChatClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ private void haveChat(){
3636
line = this.inputStream.readLine();
3737
outputStream.writeUTF(line);
3838
outputStream.flush();
39-
String inline = otherInputStream.readLine();
40-
if(!inline.equals("")){
41-
System.out.println(inline);
39+
synchronized(otherInputStream){
40+
String inline = otherInputStream.readLine();
41+
if(!inline.equals("")){
42+
System.out.println(inline);
43+
}
4244
}
4345
} catch(IOException ioe){
4446
System.out.println("Error: " + ioe.getMessage());

src/ChatServer.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private void start() {
3838
}
3939

4040
private void close() throws IOException {
41-
helper.close();
41+
helper.interrupt();
4242
if (service != null) {
4343
service.close();
4444
}
@@ -54,13 +54,13 @@ private void close() throws IOException {
5454
*/
5555
public void run() {
5656
// TODO Auto-generated method stub
57+
boolean done = false;
5758
try {
58-
boolean done = false;
5959
while (!done) {
60-
try {
60+
System.out.println("Waiting...");
61+
helper.join();
6162
List<Connection> conns = helper.getConnections();
62-
System.out.println(done);
63-
synchronized(conns){
63+
System.out.println(conns);
6464
List<String> text = new ArrayList<String>();
6565
for(int i=0; i<conns.size(); i++){
6666
Connection curr = conns.get(i);
@@ -70,8 +70,8 @@ public void run() {
7070
conns.set(i, null);
7171
} else {
7272
DataInputStream input = curr.getInputStream();
73-
line = input.readUTF();
74-
System.out.println(line);
73+
//line = input.readUTF();
74+
//System.out.println(line);
7575
}
7676
text.add(line);
7777
System.out.println(text);
@@ -91,18 +91,16 @@ public void run() {
9191
}
9292
}
9393
}
94-
}
95-
96-
} catch (IOException ioe) {
97-
done = true;
98-
System.out.println("IOException... exiting");
99-
}
94+
helper.start();
10095
}
10196
close();
102-
} catch (IOException e) {
103-
// TODO Auto-generated catch block
104-
e.printStackTrace();
105-
}
97+
} catch (IOException ioe) {
98+
done = true;
99+
System.out.println("IOException... exiting");
100+
} catch (InterruptedException e) {
101+
// TODO Auto-generated catch block
102+
e.printStackTrace();
103+
}
106104
}
107105

108106
public static void main(String args[]) {

src/ChatServerHelper.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,54 @@
88

99
public class ChatServerHelper implements Runnable{
1010

11-
public Thread thread = null;
11+
private Thread thread = null;
1212
private List<Connection> connList = null;
1313
private ServerSocket service = null;
14-
private boolean stopServer = false;
1514

1615
public ChatServerHelper(ServerSocket service){
1716
this.service = service;
1817
connList = new ArrayList<Connection>();
19-
if (thread == null) {
20-
thread = new Thread(this);
21-
thread.start();
22-
}
18+
this.start();
2319
}
2420

2521
@Override
2622
public void run() {
2723
// TODO Auto-generated method stub
28-
while (!stopServer) {
2924
try {
3025
System.out.println("Looking for new socket");
3126
Socket newSocket = service.accept();
3227
Connection newConnection = new Connection(newSocket);
33-
synchronized (connList) {
34-
connList.add(newConnection);
35-
}
28+
connList.add(newConnection);
3629
} catch (SocketTimeoutException e) {
3730

3831
} catch (IOException e) {
3932
// TODO Auto-generated catch block
4033
e.printStackTrace();
4134
}
42-
}
35+
System.out.println("Done looking for new socket");
4336
}
4437

45-
public List<Connection> getConnections(){
38+
public synchronized List<Connection> getConnections(){
4639
return connList;
4740
}
4841

49-
public void close(){
50-
stopServer = true;
42+
public void interrupt(){
43+
if(thread!=null){
44+
thread.interrupt();
45+
}
46+
}
47+
48+
public void join() throws InterruptedException{
49+
if(thread!=null){
50+
thread.join();
51+
}
52+
}
53+
54+
public void start(){
55+
if (thread == null || !thread.isAlive()) {
56+
thread = new Thread(this);
57+
thread.start();
58+
}
5159
}
5260

5361
}

src/Connection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public Connection(Socket socket){
2222
}
2323
}
2424

25-
public DataInputStream getInputStream(){
25+
public synchronized DataInputStream getInputStream(){
2626
return streamIn;
2727
}
2828

29-
public DataOutputStream getOutputStream(){
29+
public synchronized DataOutputStream getOutputStream(){
3030
return streamOut;
3131
}
3232

0 commit comments

Comments
 (0)