Android | can't Send/Receive message to/from Wialon Server using Socket

315 views Asked by At

First of all I know that this is a REALLY COMMON problem and I have surfed all over dozens of publications and posts. They have mostly the same solution. But after hours of trying that still doesn't work for me.

The problem is in sending/receiving text messages - I don't receive any response from server. Its not a HTTP server so I use Socket. I suppose there're could several causes:

  1. Problems with connection initialization;
  2. Possibly process of sending/receiving messages is wrong;
  3. Perhabs I'm building messages by protocol somehow wrong.

Now the code.

Init/open Socket connection:

    private class OpenSocketTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            // Creating InetAddress object from ipNumber passed via constructor from IpGetter class.
            InetAddress serverAddress = InetAddress.getByName(HOST); //also tried instead: socket = new Socket(HOST, PORT);

            // Create a new Socket instance and connect to host
            socket = new Socket(serverAddress, PORT);
            Log.i(TAG, "Socket created: " + HOST + ":" + PORT);

            // Create PrintWriter object for sending messages to server.
            // out = new PrintWriter(socket.getOutputStream(), true); //also tried this
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

            //Create BufferedReader object for receiving messages from server.
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            Log.d(TAG, "In/Out created");

        } catch (UnknownHostException e) {
            Log.e(TAG, "Don't know about host: " + HOST + ":" +PORT);
            connected = false;
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "Couldn't get I/O for the connection to: " + HOST + ":" +PORT);
            connected = false;
            Log.e(TAG, e.getMessage());
        }
        connected = true;
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (connected) {
            Log.d(TAG, "Connected to TCP server...");
        }
        super.onPostExecute(result);
    }
}

Send and Receive messages:

    public class ServerSendMessageTask extends AsyncTask<Void, Void, Void> {
    private String message;

    public ServerSendMessageTask(String message){
        this.message = message;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (connected) {
            Log.d(TAG, "Sending command to server: " + message);
            try {
                out.println(message);
                out.flush();

                /**
                 * Alternative for SEND message
                 * Both didn't work.
                 * */
//                    //Send the message to the server
//                    OutputStream os = socket.getOutputStream();
//                    OutputStreamWriter osw = new OutputStreamWriter(os);
//                    BufferedWriter bw = new BufferedWriter(osw);
//                    bw.write(message);
//                    bw.flush();

                Log.d(TAG, "Message was sent: " + message);

                //Listen for the incoming messages while connected = true
                while (connected) {
                    String incomingMessage = in.readLine();
                    if (incomingMessage != null) {
                        //never reach here :(
                        Log.d(TAG, "Received from server string: " + incomingMessage);
                    }
                }

                /**
                 * This is the alternative way to RECEIVE message
                 * Both didn't work for me
                 * */
//                    //Get the return message from the server
//                    InputStream is = socket.getInputStream();
//                    InputStreamReader isr = new InputStreamReader(is);
//                    BufferedReader br = new BufferedReader(isr);
//                    String response = br.readLine();
//                    Log.d(TAG, "Received from server string: " + response);

            } catch (UnknownHostException e) {
                Log.e(TAG, "Don't know about host: " + HOST + ":" +PORT);
                Log.e(TAG, e.getMessage());
            } catch (IOException e) {
                Log.e(TAG, "Couldn't get I/O for the connection to: " + HOST + ":" +PORT);
                Log.e(TAG, e.getMessage());
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void res) {
        super.onPostExecute(res);
    }
}

I open Socket in Activity's onStart() as new OpenSocketTask().execute(); and send message on button click with code new ServerSendMessageTask(message).execute();. Therefore Threads don't "overlap".

Another thing that could be:

I'm trying to send Ping package (see page 6 here) to server. Just to see its working. It looks like this #P#\r\n. So I'm supposed to receive this string if everything's Ok: #AP#\r\n.

Its not really clear about the format in Java therefore I've tried to send different combinations of messages: #P#\r\n, #P#\\r\\n, #P#. No differences.

Could you please point my mistake here?

0

There are 0 answers