Skip to content

Commit 79ce0ff

Browse files
committed
Trying to have multiple threads to allow for multiple clients, but
something is going wrong with the blocking when server Socket calls .accept()
1 parent 7206c80 commit 79ce0ff

File tree

5 files changed

+149
-19
lines changed

5 files changed

+149
-19
lines changed

bin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/ChatClient.class
33
/ChatServer.class
44
/JavaChat.class
5+
/ChatServerHelper.class
6+
/Connection.class

src/ChatClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import java.io.DataInputStream;
55
import java.io.DataOutputStream;
66
import java.io.IOException;
7+
import java.io.InputStream;
78
import java.io.InputStreamReader;
89
import java.net.Socket;
910

1011
public class ChatClient {
1112
private Socket mySocket = null;
1213
private BufferedReader inputStream = null;
1314
private DataOutputStream outputStream = null;
15+
private BufferedReader otherInputStream = null;
1416
private String endLine = ".bye";
1517

1618
public ChatClient(String serverName, int portNum){
@@ -34,6 +36,10 @@ private void haveChat(){
3436
line = this.inputStream.readLine();
3537
outputStream.writeUTF(line);
3638
outputStream.flush();
39+
String inline = otherInputStream.readLine();
40+
if(!inline.equals("")){
41+
System.out.println(inline);
42+
}
3743
} catch(IOException ioe){
3844
System.out.println("Error: " + ioe.getMessage());
3945
}
@@ -43,6 +49,8 @@ private void haveChat(){
4349
private void start() throws IOException{
4450
inputStream = new BufferedReader(new InputStreamReader(System.in));
4551
outputStream = new DataOutputStream(this.mySocket.getOutputStream());
52+
otherInputStream = new BufferedReader(
53+
new InputStreamReader(this.mySocket.getInputStream()));
4654
}
4755

4856
private void stop(){

src/ChatServer.java

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
11

22
import java.io.BufferedInputStream;
33
import java.io.DataInputStream;
4+
import java.io.DataOutputStream;
45
import java.io.IOException;
6+
import java.io.OutputStream;
57
import java.net.ServerSocket;
68
import java.net.Socket;
9+
import java.util.ArrayList;
710
import java.util.List;
811

912
public class ChatServer implements Runnable {
1013
private ServerSocket service = null;
11-
private Socket socket = null;
12-
private DataInputStream streamIn = null;
14+
private ChatServerHelper helper = null;
1315
private Thread thread = null;
1416

1517
public ChatServer(int portNum) {
1618
try {
1719
System.out.println("Creating a Chat Server on portNum: " + portNum);
1820
service = new ServerSocket(portNum);
21+
service.setSoTimeout(1000);
1922
System.out.println("Chat Server Started");
2023
System.out.println("Waiting for a client");
2124
System.out.println(service.getInetAddress());
2225
System.out.println("Success!");
26+
helper = new ChatServerHelper(service);
2327
start();
2428
} catch (IOException e) {
2529
System.out.println(e);
2630
}
2731
}
28-
32+
2933
private void start() {
3034
if (thread == null) {
3135
thread = new Thread(this);
3236
thread.start();
3337
}
3438
}
35-
36-
private void open() throws IOException {
37-
this.streamIn = new DataInputStream(new BufferedInputStream(
38-
this.socket.getInputStream()));
39-
}
4039

4140
private void close() throws IOException {
42-
if (streamIn != null) {
43-
streamIn.close();
44-
}
45-
if (socket != null) {
46-
socket.close();
47-
}
41+
helper.close();
4842
if (service != null) {
4943
service.close();
5044
}
@@ -61,16 +55,47 @@ private void close() throws IOException {
6155
public void run() {
6256
// TODO Auto-generated method stub
6357
try {
64-
socket = service.accept();
65-
open();
6658
boolean done = false;
6759
while (!done) {
6860
try {
69-
String curLine = streamIn.readUTF();
70-
System.out.println(curLine);
71-
done = curLine.equals(".bye");
61+
List<Connection> conns = helper.getConnections();
62+
System.out.println(done);
63+
synchronized(conns){
64+
List<String> text = new ArrayList<String>();
65+
for(int i=0; i<conns.size(); i++){
66+
Connection curr = conns.get(i);
67+
String line = "";
68+
if(curr == null || curr.isSocketClosed()){
69+
System.out.println("Socket is closed");
70+
conns.set(i, null);
71+
} else {
72+
DataInputStream input = curr.getInputStream();
73+
line = input.readUTF();
74+
System.out.println(line);
75+
}
76+
text.add(line);
77+
System.out.println(text);
78+
}
79+
for(int i=0; i<conns.size(); i++){
80+
Connection curr = conns.get(i);
81+
if(curr == null){
82+
83+
} else {
84+
DataOutputStream output = curr.getOutputStream();
85+
for(int j=0; j<conns.size(); j++){
86+
if(j!=i){
87+
output.writeUTF(text.get(j));
88+
output.writeUTF("\n");
89+
output.flush();
90+
}
91+
}
92+
}
93+
}
94+
}
95+
7296
} catch (IOException ioe) {
7397
done = true;
98+
System.out.println("IOException... exiting");
7499
}
75100
}
76101
close();

src/ChatServerHelper.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.io.IOException;
2+
import java.net.ServerSocket;
3+
import java.net.Socket;
4+
import java.net.SocketTimeoutException;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
9+
public class ChatServerHelper implements Runnable{
10+
11+
public Thread thread = null;
12+
private List<Connection> connList = null;
13+
private ServerSocket service = null;
14+
private boolean stopServer = false;
15+
16+
public ChatServerHelper(ServerSocket service){
17+
this.service = service;
18+
connList = new ArrayList<Connection>();
19+
if (thread == null) {
20+
thread = new Thread(this);
21+
thread.start();
22+
}
23+
}
24+
25+
@Override
26+
public void run() {
27+
// TODO Auto-generated method stub
28+
while (!stopServer) {
29+
try {
30+
System.out.println("Looking for new socket");
31+
Socket newSocket = service.accept();
32+
Connection newConnection = new Connection(newSocket);
33+
synchronized (connList) {
34+
connList.add(newConnection);
35+
}
36+
} catch (SocketTimeoutException e) {
37+
38+
} catch (IOException e) {
39+
// TODO Auto-generated catch block
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
45+
public List<Connection> getConnections(){
46+
return connList;
47+
}
48+
49+
public void close(){
50+
stopServer = true;
51+
}
52+
53+
}

src/Connection.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.BufferedInputStream;
2+
import java.io.DataInputStream;
3+
import java.io.DataOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.net.Socket;
7+
8+
9+
public class Connection {
10+
private DataInputStream streamIn = null;
11+
private Socket socket = null;
12+
private DataOutputStream streamOut = null;
13+
14+
public Connection(Socket socket){
15+
this.socket = socket;
16+
try {
17+
this.streamIn = new DataInputStream(new BufferedInputStream(
18+
this.socket.getInputStream()));
19+
this.streamOut = new DataOutputStream(socket.getOutputStream());
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
public DataInputStream getInputStream(){
26+
return streamIn;
27+
}
28+
29+
public DataOutputStream getOutputStream(){
30+
return streamOut;
31+
}
32+
33+
public boolean isSocketClosed(){
34+
return this.socket.isClosed();
35+
}
36+
37+
public void close() throws IOException{
38+
if (streamIn != null) streamIn.close();
39+
if (socket != null) socket.close();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)