Capture GPS data with JAVA [closed]

0

I'm doing an application to show data from some gps on the web, but I'm not clear about the topic of capturing the frames

I would like to make a socket in java but I'm not sure how to do it, so that it runs in the background and I capture the data of the gps.

    
asked by Mario Davila 24.08.2017 в 09:29
source

1 answer

1

I would do it this way:

package ejemplo.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServidorEscucha {


    public ServidorEscucha() throws Exception{


        InetAddress hostIp;
        hostIp = InetAddress.getByName("127.0.0.1"); // O la que necesites

        ServerSocket escuchar = new ServerSocket(1234, 1000, hostIp); // Puerto 1234 o el que requieras abrir
        try {
            while (true) {
                new AtenderCliente(escuchar.accept()).start();
            }
        } finally {
            escuchar.close();
        }
    }

    private static class AtenderCliente extends Thread {
        private Socket socket;

        public AtenderCliente(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            try {

                // Preparamos el buffer para leer la data de los GPS
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                // Leeemos linea a linea lo que envíe el GPS
                while (true) {
                    String TramaEntrante = in.readLine();
                    if (TramaEntrante == null) {
                        break;
                    }
                    // Procesar la trama entrada como se requiera
                    // *** PARSEAR / ALMACENAR EN DB  ***********
                }
            } catch (IOException e) {
                System.out.println("Error en cliente: " + e.getMessage());
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("No se pudo cerrar el puerto: " + e.getMessage());
                }
                System.out.println("Conexión con cliente cerrada.");
            }
        }
    }

}
    
answered by 28.11.2017 в 16:28