11
22import java .io .BufferedInputStream ;
33import java .io .DataInputStream ;
4+ import java .io .DataOutputStream ;
45import java .io .IOException ;
6+ import java .io .OutputStream ;
57import java .net .ServerSocket ;
68import java .net .Socket ;
9+ import java .util .ArrayList ;
710import java .util .List ;
811
912public 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 ();
0 commit comments