Response to Client after get info

davanzi8 years ago

Hello! I am a beginner and I'm trying to create a server that receives the Client Application information Traccar. The first step I got through socket can get the information. The problem is that the application always sends the same data, possibly due to not having an answer from my server. How should I responser? I am using Java. Thank you,

davanzi8 years ago

This is my code:

System.out.println("Cliente conectado: " + cliente.getInetAddress().getHostAddress());
System.out.println(Thread.currentThread().getId());
BufferedReader br = null;
try {
    br = new BufferedReader(new InputStreamReader(cliente.getInputStream()));
    OutputStream saida = cliente.getOutputStream();
    StringBuilder result = new StringBuilder();
    result.append(br.readLine());
    System.out.println(result.toString());

} catch (IOException e) {
}
cliente.close();

And result this in console:

Servidor ouvindo a porta 5055
Servidor ouvindo a porta 5055*
Cliente conectado: 172.16.10.241
8
GET /?id=749755&timestamp=1462292311&lat=-25.414046&lon=-49.2507456&speed=0.0&bearing=0.0&altitude=0.0&batt=59.0 HTTP/1.1
Servidor ouvindo a porta 5055*
Cliente conectado: 172.16.10.241
9
GET /?id=749755&timestamp=1462292311&lat=-25.414046&lon=-49.2507456&speed=0.0&bearing=0.0&altitude=0.0&batt=59.0 HTTP/1.1
Servidor ouvindo a porta 5055*
Cliente conectado: 172.16.10.241

The result is the same, probably because don't have response to client.
How can I response?
Thanks

Anton Tananaev8 years ago

Traccar Client sends standard HTTP request, so it expect standard HTTP 200 OK response.

davanzi8 years ago

Hi Anton Tananaev. Your help was essential for my solution. I am very grateful. Now I can continue my studies on your platform. And congratulations created solution, will be useful for many people. Here is how was my code in Java.

public class ServerSocketTracker {

    public static void main(String[] args) {
        try {
            // Instancia o ServerSocket ouvindo a porta 5055
            ServerSocket servidor = new ServerSocket(5055);
            System.out.println("Servidor ouvindo a porta 5055");
            while (true) {
                System.out.println("Servidor ouvindo a porta 5055*");
                Socket cliente = servidor.accept();
                ViewTracker vt = new ViewTracker(cliente);
            }
        } catch (Exception e) {
            System.out.println("Erro: " + e.getMessage());
        } finally {
        }
    }
}

Thanks again! I hope to contribute something in the coming months.
Regards Daniel

Anton Tananaev8 years ago

Not sure why you are using raw sockets instead of some HTTP server implementation, but thanks for sharing anyway. It might be useful to someone.