I am having abit of a problem here and would like to see if anyone know where the error is.
I am creating a server which shall recieve messages from the client and send the message back to all connected clients.
The problem at hand is I got the recieving part and sending part sortof working, it works when there is only one client connected, but when i do connect another client to the server, and try to write a message it doesn't work- it doesn't even show up in the client, it simple disappeares.
Another problem I have is the use of a graphical interface for the server (the client works as it should when i tried on another working server)
I would like it to start up with the server but then i have no clue how to recieve messages into the area in the server.
The server
import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.Vector; import javax.swing.*; public class ChatServer { private static Socket clientSocket = null; private static ServerSocket serverSocket = null; // static ServerGUI gui = new ServerGUI(); static Vector<ClientThread> _clients = new Vector<ClientThread>(); public static void main(String[] args) { // The default port int listening_port = 2000; /** Check whether the program has any extra arguments at start * otherwise go with the default port **/ if ( args.length < 1 ) { System.out.println("Usage: java ChatServer \n"); } else { listening_port = Integer.parseInt(args[0]); } try { serverSocket = new ServerSocket(listening_port); System.out.println("Server started using port: "+ listening_port); } catch ( IOException ioe ) { System.out.println("Can't start server on port: "+ listening_port + "\nServer Terminated!"); System.exit(-1); } // Always accepts connection from clients. // Add Clients into the vector. while (true) { try { clientSocket = serverSocket.accept(); ClientThread _temp = new ClientThread(clientSocket); _clients.add(_temp); _temp.start(); // gui.write("Client Connected\n"); break; } catch (IOException ioe) { System.out.println(ioe); } } } }
The GUI
import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class ServerGUI { // Declare graphical components private JFrame frame = new JFrame("CHATT SERVER"); private JTextArea _textArea; private JScrollPane _scrollPane; ServerGUI() { _textArea = new JTextArea(); _textArea.setEditable(false); _textArea.setLineWrap(true); _scrollPane = new JScrollPane(_textArea); frame.add(_scrollPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } public void write( String _message ) { _textArea.append(_message + "\n"); } }
The ClientThread for handling threads of clients
import java.io.*; import java.net.*; public class ClientThread extends Thread { private static BufferedReader in = null; private static PrintStream out = null; private static Socket clientSocket = null; ServerGUI gui = new ServerGUI(); public ClientThread( Socket _clientSocket ) { this.clientSocket = _clientSocket; } public void run() { String line; try { in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); out = new PrintStream(clientSocket.getOutputStream()); while( !isInterrupted() ) { line = in.readLine(); out.println(line); gui.write(line); out.flush(); } in.close(); out.close(); clientSocket.close(); } catch(IOException e) { System.err.println("Error message: "+ e); } } }
Hopefully someone can help abit where the problem resides.