Question

Following code works fine with java client, the server can able to receive the data correctly, but it is not works with GPRS client. GPRS Client using AT command in Serial Terminal(Docklight) to send the data to java server.

public class Tcpserver {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    String clientSentence;
    String capitalizedSentence;

    ServerSocket welcomeSocket = new ServerSocket(90);
    while(true)
    {             
        Socket connectionSocket = welcomeSocket.accept();
        System.out.println("connected:" );
        System.out.println("message length: "+ connectionSocket.getInputStream().available());
        BufferedReader inFromClient =new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        clientSentence = inFromClient.readLine();
        System.out.println("Received: " + clientSentence);
        capitalizedSentence = clientSentence.toUpperCase() + '\n';
        DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
        outToClient.writeBytes(capitalizedSentence);
    }
}

}

Can anybody point the problem with this code or else in what way we have to get the data from GPRS Modem.

Was it helpful?

Solution

This code can only read one line per connection. You need to start a new thread per connection and have it handle all the I/O, in a loop of its own.

I'm also wondering whether the GPRS client is sending line terminators. You might be better off just reading and writing bytes:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

operating directly on input and output streams, with any buffer size > 1, say 1024 for a start. count is an int.

I don't see why you should need to capitalize anything either. If the server doesn't understand what the GPRS modem actually sends, someone needs to fix it so it does.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top