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
import java.net.*;
import java.io.*;
public class ChatServer2
// Listens for a client, creates a thread for them, then continues to listen.
{
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
import java.net.*;
import java.io.*;
public class ChatThread extends Thread
// Made just for the client.
{
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.