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
-
main
Method:- The
main
method starts the application by calling thego
method on an instance of theserver
class.
public static void main(String[] args) { new server().go(); }
- The
-
go
Method:-
Initialize
clientOutputStreams
: AnArrayList
to storePrintWriter
objects 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
ClientHandler
thread for the client.
-
-
ClientHandler
Class:- Each
ClientHandler
handles communication with a single client. It reads messages from the client and broadcasts them to all clients.
Constructor:
- Initializes the
BufferedReader
to 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
tellEveryone
to broadcast the received message to all clients.
- Each
-
tellEveryone
Method:- Iterates over all
PrintWriter
objects inclientOutputStreams
and 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
ClientHandler
thread for each client. - ClientHandler: Reads messages from the client and calls
tellEveryone
to broadcast the message. - tellEveryone: Sends the message to all connected clients using their respective
PrintWriter
objects.
Each client's communication is handled in a separate thread, allowing multiple clients to interact with the server concurrently.