Salve a tutti!
Sto sviluppando un'applicazione client/server basata su protocollo TCP in cui il client invia una richiesta al server e visualizza i risultati dell'eleborazione tramite una notifica su Android.
Le richieste sono basate sulle coordinate fornite dal LocationManager quindi esse sono abbastanza frequenti.
Ho potuto notare tuttavia che mentre il tempo di elaborazione lato server è trascurabile, il RTT è di circa 6 secondi il è un po' troppo alto.
Come posso ridurlo?
Codice del client (la richiesta è spedita ad un indirizzo ip evitando il ricorso ad un dns)
codice:
public abstract class Client extends AsyncTask<Object, Object, Object>{
private static final String DEFAULT_IP_ADDRESS = "x.x.x.x";
private static final int PORT = 12345;
private String indirizzo;
private int porta;
public Client(String indirizzo, int porta){
this.indirizzo = indirizzo;
this.porta = porta;
}
public Client(){
indirizzo = DEFAULT_IP_ADDRESS;
porta = PORT;
}
public abstract void receiveData(Object object);
@Override
protected Object doInBackground(Object...parametri)
{
Object esito = null;
try
{
InetAddress inetAddress = InetAddress.getByName(indirizzo);
Socket socketClient = new Socket(inetAddress,porta);
//OTTIENE GLI STREAM DI INPUT E OUTPUT
BufferedInputStream inputStream = new BufferedInputStream(socketClient.getInputStream());
BufferedOutputStream outputStream = new BufferedOutputStream(socketClient.getOutputStream());
//CREA DEGLI OBJECTSTREAM PER L'I/O BASANDOSI SUGLI STREAM OTTENUTI DAL SOCKET
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.flush();
oos.writeObject(parametri);
oos.flush();
ObjectInputStream ois = new ObjectInputStream(inputStream);
esito = ois.readObject();
oos.close();
ois.close();
}
catch(UnknownHostException e){ e.printStackTrace(); }
catch(IOException e){ e.printStackTrace(); }
catch(ClassNotFoundException e){ e.printStackTrace(); }
return esito;
}
@Override
protected void onPostExecute(Object receivedData)
{
if(receivedData != null)
receiveData(receivedData);
}
}
Codice del server
codice:
public class Server extends Thread{
private ServerSocket serverSocket = null;
private boolean threadAttivo = true;
public Server()
{
super("threadServerPrincipale");
}
public void startServer(int porta) throws BindException, IOException
{
serverSocket = new ServerSocket(porta);
System.out.println("Server in ascolto sulla porta " + porta);
//AVVIA IL THREAD DI ASCOLTO
this.start();
}
public void chiudiServer() throws IOException
{
if(serverSocket != null)
{
threadAttivo = false;
serverSocket.close();
}
}
public void run()
{
Socket clientSocket;
while(threadAttivo)
{
try
{
//RESTA IN ATTESA DI UNA CONNESSIONE DA PARTE DI UN CLIENT.
//ALL'ARRIVO DI QUESTA CREA UN SOCKET DI COMUNICAZIONE
clientSocket = serverSocket.accept();
//PER PERMETTERE LA CONNESSIONE DI PIU' CLIENT CONTEMPORANEAMENTE, IL SOCKET VIENE PASSATO
//AD UN'ALTRA CLASSE LA QUALE SI OCCUPERA' DI FORNIRE I SERVIZI RICHIESTI
new ComunicazioneClient(clientSocket);
}
catch(SocketException e){}
catch(IOException e){ e.printStackTrace(); }
}
}
}
codice:
public class ComunicazioneClient extends Thread{
private InputStream inputStream = null;
private OutputStream outputStream = null;
public ComunicazioneClient(Socket clientSocket) throws IOException
{
super("thread client" + clientSocket.getLocalSocketAddress().toString());
inputStream = clientSocket.getInputStream();
outputStream = clientSocket.getOutputStream();
System.out.println("Richiesta del client " + clientSocket.getInetAddress().getHostName());
start();
}
public void run()
{
try
{
FrontController frontController = FC.getInstance();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(outputStream));
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(inputStream));
Object[] dati = (Object[])ois.readObject();
Object[] parametri = new Object[dati.length - 1];
System.arraycopy(dati, 1, parametri, 0, parametri.length);
String richiesta = (String)dati[0];
oos.writeObject(frontController.processRequest(richiesta, parametri));
oos.flush();
oos.close();
ois.close();
}
catch (IOException e) { e.printStackTrace(); }
catch (SecurityException e) { e.printStackTrace(); }
catch (NoSuchMethodException e) { e.printStackTrace(); }
catch (ClassNotFoundException e) { e.printStackTrace(); }
catch (InstantiationException e) { e.printStackTrace(); }
catch (IllegalAccessException e) { e.printStackTrace(); }
catch (NullPointerException e) { e.printStackTrace(); }
}
}