Im going to attach my 3 programs here which they are actually interlinked, The ClientServerSession is actually one that is running the leg work, the ClientOnServer is the user interface that shows up but actually its the ClientServerSession that is running, the user basically inputs his name, and in the event if he sends a message, the message is suppose to go thru and then message gets accepted into ClassOnServer which ClassOnServer will activate the sendToAll() and broadcast it to every client that is connected. Here are the codes.
ClassOnServer
import java.io.*; import java.net.*; import java.util.ArrayList; public class ClassOnServer { public static final int portNumber = 5000; private ArrayList<Socket> clientList; Socket aSock = new Socket(); ClassOnServer aConsultant; Writer out; public static void main(String [] args) { try { ClassOnServer consultant = new ClassOnServer(); } catch (IOException e) {System.out.println(e);} } public ClassOnServer() throws IOException { // Listen for client connections on portNumber ServerSocket serverlistener = new ServerSocket(portNumber); System.out.println("Ready for client requests"); int i=1; //Infinite loop while(true) { //Waiting for connection to come in Socket sSock = serverlistener.accept(); System.out.println("Client " + i + " accepted"); //Create a Socket object that represents a new client server session //Thread session = new ClientServerSession(i, sSock.getInputStream(), sSock.getOutputStream()); Thread session = new ClientServerSession(i, aConsultant, aSock); //Start session session.start(); i++; } } public void removeConnection(Socket s) { for (int i = 0; i < clientList.size(); i++) { if (s.equals(clientList.get(i))) { clientList.remove(s); } } } public synchronized void sentToAll(String message, String name) { for (int i=0; i < clientList.size(); i++) { try { Socket s = (Socket)clientList.get(i); out = new OutputStreamWriter(s.getOutputStream()); out.write("Sent by client " + name + ":" + message + "\n"); out.flush(); } catch (IOException ex) { } } } }
ClassOnClient:
import java.io.*; import java.net.*; public class ClassOnClient { public static final int portNumber = 5000; BufferedReader input, userTerminal; Writer output; ObjectOutputStream out; public static void main(String [] args) { try { ClassOnClient clientObject = new ClassOnClient(); } catch (IOException e) {System.out.println(e);} } public ClassOnClient() throws IOException { Socket cSock = new Socket("LocalHost", portNumber); Reader iRead = new InputStreamReader(cSock.getInputStream()); input = new BufferedReader(iRead); userTerminal = new BufferedReader(new InputStreamReader(System.in)); output = new OutputStreamWriter(cSock.getOutputStream()); while(true) { String line = input.readLine(); System.out.println (line); line = userTerminal.readLine(); if (line.equals("q")) break; output.write(line + "\n"); output.flush(); } System.out.println("Disconnected with Server"); } }
ClientServerSession:
import java.io.*; import java.net.*; public class ClientServerSession extends Thread { private String username; private String message; BufferedReader in, userTerminal; Writer out, output; int id; ClassOnServer server; public ClientServerSession (int id, ClassOnServer server, Socket sSock) throws IOException { this.id = id; this.server = server; //sSock = new Socket("LocalHost", 5000); Reader iRead = new InputStreamReader(sSock.getInputStream()); in = new BufferedReader(iRead); userTerminal = new BufferedReader(new InputStreamReader(System.in)); output = new OutputStreamWriter(sSock.getOutputStream()); //InputStream inS; //OutputStream outS; //Reader iRead = new InputStreamReader(inS); //out = new OutputStreamWriter(outS); } public void run() { try { output.write("Enter your name: \n"); output.flush(); while (true) { //--Displays user name and prompts for an initial message username = userTerminal.readLine(); String name = username; output.write(name + ", please enter a message:(q to end) \n"); output.flush(); while(true) { //--Displays user name and message message = userTerminal.readLine(); String msg = message; output.write(name + ": " + message); output.flush(); } } } catch (IOException e) { System.out.println(e); } } }
ClassOnClient is 100% accurate, might need some tweaking in ClassOnServer, however im sure the big chunk of why this isn't working lies with the ClientServerSession.