A Java TCP client/server pair in which the server accepts multiple clients at once, handling each connection on its own thread, and echoes every message back in upper case.
TCP_Serverlistens on port 56783 and loops onaccept(); each new connection is given an incrementing client number and logged (Server is now connected to the N Client).- Each connection is handed to a separate
thread(subclass ofjava.lang.Thread), so several clients can be served concurrently. - The handler thread reads one line at a time, converts it to upper case, prints it on the server console and sends it back to the client.
TCP_Clientconnects tolocalhost:56783, reads lines from the keyboard, sends them to the server and prints the reply (FROM SERVER: ...).- Typing
Close Socket(case-insensitive) ends the session: the client closes its socket and exits, and the server's handler thread for that client stops while the server keeps accepting new clients.
- Java (standard library only:
java.net.ServerSocket,java.net.Socket,java.lang.Thread,java.iostreams) - Eclipse project files
src/
TCP_Server.java Server: accepts connections and starts a handler thread per client
thread.java Per-client handler thread (read line -> upper-case -> reply)
TCP_Client.java Console client
.classpath, .project, .settings/ Eclipse project files
From the repository root:
javac -d bin src/*.java
# Terminal 1 – start the server first
java -cp bin TCP_Server
# Terminals 2, 3, ... – start one or more clients
java -cp bin TCP_ClientType a message in a client terminal to get it back in upper case; type Close Socket to disconnect that client.
The classes can also be run from Eclipse (run TCP_Server, then TCP_Client one or more times).
Developed by Khaled Walid