Reputation: 37
I have a server socket A in a thread, listening to petitions. I'll have six clients using the same server (maybe at the same time). The server A reads the data, does something with it and sends the data to another server socket B.
Code: Server A
public class ServerA implements Runnable{
@Override
public void run() {
this.Hilo();
}
public void Hilo(){
String dataIn;
ServerSocket socket;
try {
socket = new ServerSocket(6000);
System.out.println("Server on...");
while(true) {
Socket connectionSocket = socket.accept();
BufferedReader client = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
dataIn = client.readLine();
if (dataIn!= null){
// dataInPlus = SomeMethod(dataIn)
// Send dataInPlus to server B
}
}
}catch (Exception ex) {
System.out.println("Error");
}
}
1) ¿I should create a thread every time I read a client in the server A, to work with data?
2) ¿What will happen if the six clients send data at the same time to server A?
3)¿I should create a thread for every petition in Server B?
Upvotes: 0
Views: 194
Reputation: 37023
1) I should create a thread every time I read a client in the server A, to work with data?
Yes. These worker threads are known as worker thread. If you don't then you would block next request while reading and processing the request. So wait time for other request will increase.
2) What will happen if the six clients send data at the same time to server A?
Same as above. Only one of them will be active, others would end up waiting.
3)I should create a thread for every petition in Server B?
Let your worker thread handle communication with serverB.
Upvotes: 1
Reputation: 3061
1) - Yes, or each of the clients will have to wait for the server to deal with the others data;
2 - Nothing happens, 6 threads will just be created and the data will be taken care of. If you have a server with 6 cores, most likely each of the requests will be processed at the same time, if not the CPU will do its necessary scheduling;
3 - If for each of the client requests, the server B will also receive a request, then sure.
Upvotes: 0