I just started programming in java about 2 months ago and only know the old school methods of getting things done. I am making a chat client, server. The client and server work fine but whenever a client connects to the server the server creates a thread and that thread makes a new protocol. I need some way for the server to remmeber all the clients so it can relay the messages to them.
The main server object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import java.net.*; import java.io.*;
public class ChatServer2 { public static void main(String[] args) throws IOException { boolean listening = true; ServerSocket serverSocket = null;
try { serverSocket = new ServerSocket(1337); } catch (IOException e) { System.err.println("Could not listen on port: 1337."); System.exit(-1); } System.out.print("Server ready.\n"); while (listening) { new ChatThread(serverSocket.accept()).start(); } serverSocket.close(); } } |
The thread object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.net.*; import java.io.*;
public class ChatThread extends Thread { private Socket ChatSocket = null; int place = 0; ChatProtocol cp = new ChatProtocol(); public ChatThread(Socket ChatSocket) { super("ChatThread"); this.ChatSocket = ChatSocket; } |
theres more to the thread but i just included the important code.
what i want is for the server to recognize all the clients instead of just 1. Thank you.