I have this question long time ago, how to transfer data from one client to another through a server? After some Google search, I think the keyword that I am looking for should be "Asynchronous Socket Programming".
I manage to find the keyword, but I fail to find a simple example in Java. So, I decide to write one myself based on Socket Programming tutorial by S.Prasanna.
First of all, I am going to create two projects, TCPServer & TCPClient.- TCPServer having three threads for captures input (Main.java), read clients output (ReadThread.java) & read client new connection (ConnectThread.java).
- TCPClient having two threads for captures input (Main.java) & read server output (ReadThread.java).
Here are some demo scenes:
First of all, open the TCPServer program. Open two TCPClient program, connect to TCPServer. Send message from TCPClient A program to TCPServer. Send message from TCPClient B program to TCPServer. Send message from TCPServer to both TCPClient programs. Lastly, I try to disconnect one of the TCPClient program.This is my first socket connection program, correct me if I make any mistake.