我正在开发一个客户端/服务器应用程序,其中Android客户端向Java服务器发送请求,然后向用户显示服务器respose的通知。 请求基于LocationManager更新,因此它们非常频繁。 我看到RTT非常高(约6秒),我想降低。 提出请求的时间是可以追溯的。
现在我正在使用TCP协议
这是客户
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);
}}
......这是服务器
public class Server扩展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(); }
}
}
}
公共类ComunicazioneClient扩展了线程{
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(); }
}