6
In the following class SocketServidor
send a single thread with the out
and the in
, but this class is supposed to be able to send two threads when running.
You’re supposed to do this:
The server should work in multi-threading, having for each active connection two threads, which we will designate in and out:
- in to receive messages from customers (input stream) This thread will be locked waiting for messages from the client. Messages that the server receives should be stored in a structure in memory. When a given user has new messages, if they are online, the thread out associated with their connection should be notified in order to process the delivery of the messages.
- out to deliver messages and notifications to customers (output stream) This thread will be on hold, becoming active when there are new messages or message delivery notifications relating to the link user.
SocketServidor
public class SocketServidor extends Thread{
private ServerSocket servidorSocket;
private Socket socket;
private static final int PORTA = 8080;
//HashMap recurso partilhado! (sempre que invocarmos este recurso temos de fazer synchronized)
private Map<String, ObjectOutputStream> utilizadores = new HashMap<String, ObjectOutputStream>(); //utilizadores da aplicacao (online!)
public SocketServidor(){
try {
servidorSocket = new ServerSocket(PORTA);
System.out.println("Estabelecer ligação TCP/IP no porto: " + PORTA);
System.out.println("A aguardar conexão do cliente...");
while(true){
socket = servidorSocket.accept();
new Thread(new ListennerSocket(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
And what is your doubt?
– NilsonUehara
@Nilsonuehara is to do the server with multithreading, server with two threads one in and one out, is that what I do above is to send only one thread with the in and the out.
– rrr
@Nilsonuehara and the in and out are supposed to give as described above, can help me?
– rrr