1

I'm getting mad trying to read data from a socket. I tried all and all, I think that my code should work but didn't.

My intention is to only run the following method on the onCreate. First I create a Thread to run all the things that are related to the network. Then, I create the scoket object and I read the inputstream of the socket. In this moment, in an infinit loop, I read the inputstream using readLine as explained saw on this answer. Finally, I do what I want with the data that came by the socket.

I don't know how much data will be sended by the server. And it would be in json, but this doesn't matter.

Here my code

public void receiveMsgs(){      
    new Thread(new Runnable(){
        @Override
        public void run() {
            BufferedReader in = null;
            try {
                Log.d("NETWORK-RECEIVE", "Trying to connect to socket...");
                Socket socket;
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                if(socket.isConnected()){
                    Log.d("NETWORK-RECEIVE", "The connection have been stablished");
                }
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("NETWORK-RECEIVE", "Something goes wrong: IOException");
            }
            while(true){
                String msg = null;
                try {
                    StringBuilder total = new StringBuilder();
                    String line;
                    while ((line = in.readLine()) != null) {
                        total.append(line);
                    }                   
                    msg = total.toString();
                    Log.d("NETWORK-RECEIVE","Message readed!:"+msg);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.d("NETWORK-RECEIVE", "Something goes wrong: IOException");
                }
                if(msg == null){
                    Log.d("NETWORK-RECEIVE", "Message is null");
                    break;
                }
                else{
                    //Do what I want
                    Log.d("NETWORK-RECEIVE", "something");
                }
            }           
        }
    }).start();
}
Community
  • 1
  • 1
Gabriel Esteban
  • 752
  • 2
  • 9
  • 34
  • are you trying it on emulator or device..? If you want data from server why aren't you using httpclient ? – Piyush Dec 06 '13 at 20:27
  • Both, because we work with our own protocol using streams, and I have to post things via streams – Gabriel Esteban Dec 06 '13 at 20:29
  • Well, what happens what you run it? What log messages do you generate? Do you have any basis to believe the connection is even being established with the server? – Chris Stratton Dec 06 '13 at 20:33
  • No error showned on the logcat, so, I have to thing that the connections with the socket works, also, in another method, to sendmessages, I connected to the socket using a similar code. – Gabriel Esteban Dec 06 '13 at 20:41
  • 1
    Don't guess - add some step by step logging of successful results to your code too. And tell us what the actual observed behavior is. How do you know it's not working? – Chris Stratton Dec 06 '13 at 21:12
  • I added some code that is runned when the socket is connected, it's showed on the logcat, so, the socket works. – Gabriel Esteban Dec 06 '13 at 21:29
  • 1
    So what doesn't work? You really are not being very proactive, either about investigating the problem or about reporting the information which would be needed for anyone to help you. – Chris Stratton Dec 06 '13 at 22:42
  • Your `isConnected()` test is pointless. If the socket didn't connect, an exception would have been thrown, so the test would be unreachable. And if the exception is thrown, you shouldn't then fall into I/O code that assumes it wasn't. All your exception handling is poorly structured: for example, `msg == null` can mean either that no data was sent or an exception occurred. You're conflating the two conditions. – user207421 Dec 09 '13 at 03:41

1 Answers1

1

Ever look at square's OKHttp or Retrofit?

http://square.github.io/okhttp/#examples

&

http://square.github.io/retrofit/

Seems easier than sockets. Do you really need sockets?

McNinja
  • 798
  • 7
  • 19