P
P
Pavel2013-08-13 16:16:17
Java
Pavel, 2013-08-13 16:16:17

Android socket is a problem when sending data. Why is data only sent after the socket's outgoing stream is closed?

Good afternoon!
I am writing a client application for android. Sending requests to the server and receiving responses from the server. Connecting to the server via sockets.
Faced such problem: the data is sent only after closing of an output stream of a socket. I will describe the situation: there is an Asynctask in which ONLY ONE connection to the server should be open. But I am unable to implement it. It turns out to be done only according to this scheme: opened the connection, sent the data, received the data, closed the connection. Because without closing the connection no data is sent to the server.
Here is the AsyncTask class:

class InternetRequestAsyncTask extends AsyncTask<String, Integer, String>
    {
        private Socket socket = null;
        private DataOutputStream outStream = null;
        private BufferedReader in;
        private String ip, port;
        final String GPS_MESSAGE = "GPS_MESSAGE";
        final String LOGIN_REQUEST = "LOGIN_REQUEST";
        final String LIST_REQUEST = "LIST_REQUEST";
        private String command = "";

        public InternetRequestAsyncTask(String IP, String PORT) 
        {
            ip = IP;
            port = PORT;
        }

        private void setCommandType(String commandType)
        {
            command = commandType;
        }

        private void connect()
        {
            try {
                socket = new Socket(ip, Integer.parseInt(port));
                socket.setKeepAlive(true);

                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                outStream = new DataOutputStream(socket.getOutputStream());

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

            Log.d("MyTag", "Connected");
        }

        private void disconnect()
        {
            if (socket != null)
            {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outStream != null)
            {
                try {
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            Log.d("MyTag", "Disconnected");
        }

        private void writeMessage(String message) throws IOException
        {            
            outStream.writeBytes(message + "\r\n");
        }

        @Override
        protected String doInBackground(String... params) 
        {        
            this.connect();
            
            while (true)
            {
                if (command.equals(GPS_MESSAGE))
                {
                                        
                    try {
                        this.writeMessage(GPS_MESSAGE);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    command = "";
                    
                } else
                {
                    if (command.equals(LOGIN_REQUEST))
                    {
                                                
                        try {
                            this.writeMessage(LOGIN_REQUEST);
                            
                            StringBuilder sb = new StringBuilder();
                            String str;
                            
                            while ((str = in.readLine()) != null)
                            {
                                sb.append(str);
                            }
                            Log.d("MyTag", sb.toString());

                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                        command = "";
                        
                    } else
                    {
                        if (command.equals(LIST_REQUEST))
                        {
                                                    
                            try {
                                this.writeMessage(LIST_REQUEST);
                                
                                StringBuilder sb = new StringBuilder();
                                String str;
                                
                                while ((str = in.readLine()) != null)
                                {
                                    sb.append(str);
                                }
                                Log.d("MyTag", sb.toString());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            
                            command = "";
                            
                        } else
                        {
                            if (command.equals("EXIT"))
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) 
        {
            this.disconnect();

            super.onPostExecute(result);
        }

        @Override
        protected void onCancelled() 
        {
            this.disconnect();

            super.onCancelled();
        }
    }

I use like this:
InternetRequestAsyncTask irat = new InternetRequestAsyncTask(IP, PORT);
irat.execute();

and by pressing the buttons I send the necessary commands:
irat.setCommandType(commandType);
in this form, the code (when connect() is called only once, and disconnect() at the end of the thread) the message is sent to the server only once.
Help me to understand. You need to open ONE connection in AsyncTask and reuse it for sending/receiving messages.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Toseter, 2013-08-13
@Toseter

stackoverflow.com/questions/7166328/when-why-to-call-system-out-flush-in-java

B
Boris Vanin, 2013-11-21
@fogone

Try using the flush() method when you want to "push" data from the stream buffer to the network.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question