Messages sent from Android Server Socket is not received by applications like Packet Sender on Ubuntu or Windows

269 views Asked by At

I have implemented Android ServerSocket to work with a payment device in semi integrated mode. Payment device is Android based so it will open a server socket and listens for incoming requests on a fixed port. I have also created a dummy clientApp where I send data/request to payment app and it sends the response back to cleint app. This part works. The problem arises when I send the same request via an app like Paket Sender. In this case the request is received on server side but when I send the response back to client (Packet Sender) it does not receive anything when connection is active. Below is my code snippet where I read the data and then send the data.

the part where I create a server socket

ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
Socket socket = serverSocket.accept();
CommunicationThread commThread = new CommunicationThread(socket);
new Thread(commThread).start();

Then the communication thread

 class CommunicationThread implements Runnable {

    private Socket clientSocket;

    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {
        //here the clientSocket is used to listen to incoming messages from client
        this.clientSocket = clientSocket;

        //here this tempClientSocket is used to send messages back to client  through sendMessage() methos which is defined below.
        tempClientSocket = clientSocket;

        try {
            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
            showMessage("Error Connecting to Client!!");
        }
    }

    public void run() {

        while (!Thread.currentThread().isInterrupted()) {
            try {

                //we receive the incoming message in this read variable and from here we parse it and send the request to PayWorks SDK.
                String read = input.readLine();
                Log.e("READ"," Read->"+read);

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

        }
    }
}

* Using below method we send the message back to the client. We are having problems in this part when client is an application * like Packet Sender. * Because we do not know how the underlying parsing of these apps works hence we cant debug it. * What we have made sure is that result 'message' variable is * correct here and that tempClientSocket is not null. * But after that we just send the message over the socket and we do not have further control over it. * This method works if client is implemented in mobile and have similar implemementation of client code as mentioned on * android developer Socket implementation guide. *

private void sendMessage(final String message) {
    try {
        Log.e("MESSAGES","Message is->"+message);
        if (null != tempClientSocket) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    PrintWriter out = null;
                    try {
                        out = new PrintWriter(new BufferedWriter(
                                new OutputStreamWriter(tempClientSocket.getOutputStream())),
                                true);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    out.println(message);

                }
            }).start();

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

Can someone help me figuring out what I need to change in sendMessage() method above so that even in applications like Packet Sender I can see the data sent back by the server. PS- I have tried most of the solutions available on SO regarding this and hence posting this as a last resort.

0

There are 0 answers