0
votes

I have developed TCP/IP Server in java listening on port and a GPS device is sending the data to server.

Initially, the device sends the IMEI number to server and server acknowledges with 01. After receiving the acknowledgement by device, a new packet of data is sent to server.

I am able to get IMEI number by TCP Server and after sending acknowledgement, i am not able to receive new packet data from client. I am including my code below also. Please let me know where i am wrong.

This is my code

while(true) 
{
socket = serverSocket.accept();
if(!socket.isClosed())
{
System.out.println("Server accepted");

BufferedInputStream is = new  
BufferedInputStream(socket.getInputStream(),1500);
System.out.println("Server accepted"+is.available());

InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println("Server");
Object response="";
 int flag=0;

 Thread.sleep(2000);

response = br.readLine();
 DataOutputStream outToClient = new 
DataOutputStream(socket.getOutputStream());
try
{
        System.out.println("break::"+response);
             String hexString = "01";

             byte[] b = hexString.getBytes(StandardCharsets.US_ASCII);
             int len = b.length;
             outToClient.write(b,0,len);
             outToClient.write('\n');
            outToClient.flush();

            Thread.sleep(2000);

             if(is.available()==0)
             {
                 response = br.readLine();
                 System.out.println("break::"+response);
                 String s = ((String) response).substring(2,4);

                 int count = Integer.parseInt(s);
                 System.out.println("String len : "+count);
                 outToClient.write(count);
             }

             br.close();

}
catch (NullPointerException e) 
{
    e.printStackTrace();
}


socket.close();

}

}
1
I'm sending response in binary packet format, is it correct? - Vinod Poorma
Don't rely on is.available()==0. It most probably does not what you expect. This code has further design issues, but that would lead too far. - Fildor

1 Answers

0
votes

from your question it looks like you are configuring Teltonika device. The mistake that i see is in sending the acknowledgment. Instead of 01 you should send hex data attached like \x01.

Also once the data is received - it has to re-acknowledge the client with another dynamic value from the data received.