Re: serversocket
On Sat, 14 Jul 2012 10:23:52 -0700, Knute Johnson wrote:
On 7/14/2012 7:56 AM, Lucyann Lenon Emerick De Assis wrote:
translating Good morning!
to starting with java but Nevertheless I have some experience with php.
Trying to create a ServerSocket with java to get data from the GPS
model TK102, did the class below That by the way this "working" I
emulei the model of it on my smart and he did what he HAD to read on
screen and printed logs , However when i put the gps to connect it does
not conect. I know he is working as a forum on the net a guy Gave me
the ip of the server and connected it hin. Can someone help me?
Em s??bado, 14 de julho de 2012 09h41min27s UTC-3, Lucyann Lenon Emerick
De Assis escreveu:
Bom dia!
to come??ando com java mas no entanto j?? tenho uma certa experi??ncia
com php. to tentando criar um serversocket com java para receber dados
de um gps modelo tk102 , fiz a classe abaixo que por sinal esta
"funcionando" eu emulei o modelo dele no meu smart e ele fez
o que tinha que fazer leu e imprimiu em tela os logs, entretanto
quando eu coloco o gps pra conectar ele nao conect. sei que ele esta
funcionando pois em um forum na net um cara me forneceu o ip do server
dele e nele conectou. alguem pode me ajudar ?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Track {
public static void main(String[] args) {
//Declaro o ServerSocket ServerSocket serv = null;
//Declaro o Socket de comunica????o Socket s = null;
//Declaro o leitor para a entrada de dados BufferedReader
entrada = null;
while (true) {
try {
//Cria o ServerSocket na porta 11000 se estiver
dispon??vel serv = new ServerSocket(11000);
//Aguarda uma conex??o na porta especificada e cria
retorna o socket que ir?? comunicar com o cliente s =
serv.accept();
//Cria um BufferedReader para o canal da stream de entrada de dados
do socket s
entrada = new BufferedReader(new
InputStreamReader(s.getInputStream()));
//Aguarda por algum dado e imprime a linha recebida
quando recebe System.out.println(entrada.readLine());
//trata poss??veis excess??es de input/output. Note que
as excess??es s??o as mesmas utilizadas para as classes
de java.io
} catch (IOException e) {
//Imprime uma notifica????o na sa??da padr??o caso haja
algo errado.
System.out.println("Algum problema ocorreu para
criar ou receber o socket.");
} finally {
try {
//Encerro o socket de comunica????o s.close();
//Encerro o ServerSocket serv.close();
} catch (IOException e) {
}
}
}
}
}
A ServerSocket is used when you want to be the server. A Socket is used
when you want to connect to a server. Is the GPS running a server?
I've found a set of variations on a sort-of spec for the TK-102 which
don't shed a lot of light. The thing is a vehicle or person tracker:
you're meant to stick a SIM into it and either interrogate it via SMS or
set it up to send you an SMS at configurable intervals to say where it
is. It also seems to have a microphone that can be used to listen in to
what's happening near it.
It appears that you can also talk to it via its USB socket for
configuration, etc. This assumes you'll use something like a Windows box
running Hyperterminal or a Linux system running minicom or Kermit. I
gather from this that the USB connection must be a serial connection,
which Java does not do without JNI, though I suppose you could use a
socket connection to talk to a TCP:serial converter of some sort.
There is some talk about configuring it with an IPV4 IP, but I frankly
don't understand this unless you can piggyback a TCP session onto GPRS
and/or via an SMS stream and so get to it that way.
In any case it looks very much as though the TK-102 wants to be the
server, so if it can be reached over a socket connection, it looks like
the OP should be using a Socket rather than a ServerSocket to talk to it.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |