I have three entities: A,B and C. A is a server and B and C are clients.
A is a multithread server implemented using the class in java.net
I would realize a communication in which B sends a message to A, A send a message to C and C send a message B.my question is: the clients C must be even a server or is must be only a client that forward the message?
public class ServerA{
public static void main(String[] args){
boolean listening = true;
int port_number = 8888;
try{
ServerSocket ss = new ServerSocket(port_number);
System.out.println("Waiting for incoming connection...");
while(listening){
new Thread(new ServerThread(ss.accept())).start();
}
}
catch(IOException ioe){ioe.printStackTrace();}
}
}
public class ServerThread implements Runnable{
@Override
public void run(){
}
}
Is there any particular reason why you would like design your architecture like that?
To answer your question: lets think a bit about it. What do you need to send a message from client C to client B? How would you want to implement it? You mentioned a channel in your comment – but what do you exactly need to open a “channel” between two clients? You need to know somehow its (client B’s) location and a port client B is listening for messages on (worst case scenario each client knows about all/some other clients, better way would be to query the main server for a list of other clients/have some kind of a registry with all clients). Also, obviously, client B needs to have a port opened and be waiting for messages.
So basically client C queries server A for something, gets a response and then sends (through a socket) a message to client B, which has to be listening (just like client A was) for it. This is basically what server-client does.
If you know that it will be in a LAN or something similar then you can consider using UDP instead of TCP, but then you need to manage way more things by yourself since it is not as reliable.
This architecture reminds me more of a peer-to-peer network (where all “clients” are more like a client+server combination) with a single server which can fetch data from somewhere/do some computation.