This is basically a simple chat application that I have built after learning socket programming. The Gui of this chat application need to be improved yet i would do later task is to make it cpable of creating unlimited users's objects rather than client1, 2 etc
-
mainMethod:- The
mainmethod starts the application by calling thegomethod on an instance of theserverclass.
public static void main(String[] args) { new server().go(); }
- The
-
goMethod:-
Initialize
clientOutputStreams: AnArrayListto storePrintWriterobjects for each client. -
Create
ServerSocket: Listens for incoming client connections on port 5000. -
Infinite Loop: Continuously waits for and accepts new client connections.
while (true) { Socket clientSocket = serverSock.accept(); PrintWriter writer = new PrintWriter(clientSocket.getOutputStream()); clientOutputStreams.add(writer); Thread t = new Thread(new ClientHandler(clientSocket)); t.start(); System.out.println("some client got connected with server"); }
-
Accept Client Connection: Blocks until a new client connects, then accepts the connection.
-
Create
PrintWriter: For the connected client and adds it toclientOutputStreams. -
Start Client Handler Thread: Creates and starts a new
ClientHandlerthread for the client.
-
-
ClientHandlerClass:- Each
ClientHandlerhandles communication with a single client. It reads messages from the client and broadcasts them to all clients.
Constructor:
- Initializes the
BufferedReaderto read messages from the client socket.public ClientHandler(Socket clientSocket) { try { sock = clientSocket; InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(isReader); } catch (Exception ex) { ex.printStackTrace(); } }
run Method:
-
Runs in a separate thread for each client. It reads messages from the client in a loop and broadcasts them to all clients.
public void run() { String message; try { while ((message = reader.readLine()) != null) { System.out.println(message); tellEveryone(message); } } catch (Exception ex) { ex.printStackTrace(); } }
-
Read Messages: Continuously reads messages from the client.
-
Broadcast Messages: Calls
tellEveryoneto broadcast the received message to all clients.
- Each
-
tellEveryoneMethod:- Iterates over all
PrintWriterobjects inclientOutputStreamsand sends the message to each client.
public void tellEveryone(String message) { Iterator<PrintWriter> it = clientOutputStreams.iterator(); while (it.hasNext()) { try { PrintWriter writer = (PrintWriter) it.next(); writer.println(message); writer.flush(); } catch (Exception ex) { ex.printStackTrace(); } } }
- Iterate and Send Messages: For each client, sends the message and flushes the stream to ensure it's sent immediately.
- Iterates over all
- main: Calls
go. - go: Sets up the server, waits for clients to connect, and starts a new
ClientHandlerthread for each client. - ClientHandler: Reads messages from the client and calls
tellEveryoneto broadcast the message. - tellEveryone: Sends the message to all connected clients using their respective
PrintWriterobjects.
Each client's communication is handled in a separate thread, allowing multiple clients to interact with the server concurrently.
