0

I am trying to use sockets in Android to connect over wifi to some UDP port (some_port) on a machine in my local network whose ip is some_ip.

When I run

socket = new Socket(some_ip, some_port);

I get no message error but the program does not seem to read this line and I can't log the error when surrounding with try/catch.

How can I debug that ?

Edit 1 : here's my try/catch

try{
    socket = new Socket(some_ip, some_port);
}
catch(ConnectException e) {
  e.printStackTrace();
}

Edit 2 : here's the entire code

private void getUDPData() throws IOException {

        class ProcessUPDTask extends AsyncTask<String, Void, Socket> {

            private Exception exception;

            private Socket socket;

            public ProcessUPDTask() throws IOException {

            }

            private void runThread(){
                new Thread() {
                    public void run() {
                        Toast.makeText(activity, "Own Message",       Toast.LENGTH_LONG).show();
                           }
                }.start();
             }


            protected Socket doInBackground(String... urls) {

                try {
                    try{
                        socket = new Socket(some_ip, some_port); 
                        socket.setSoTimeout(1500);
                    }
                    catch(IOException e) {
                       e.printStackTrace();
                    }

                    Log.d("TAG","this line is reached");
                    while(true){
                        try {
                            dataInputStream = new DataInputStream(socket.getInputStream());
                            dataOutputStream = new DataOutputStream(socket.getOutputStream());
                            System.out.println("ip: " + socket.getInetAddress());
                            System.out.println("message: " + dataInputStream.readUTF());
                            dataOutputStream.writeUTF("Hello!");
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        finally{
                            if( socket!= null){
                                try {
                                    socket.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                            if( dataInputStream!= null){
                                try {
                                    dataInputStream.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                            if( dataOutputStream!= null){
                                try {
                                    dataOutputStream.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    this.exception = e;
                    e.printStackTrace();
                    return null;
                }
            }
            protected void onPostExecute(Socket socket) {
                // TODO: check this.exception
                // TODO: do something with the feed
            }
        }
        new ProcessUPDTask().execute();
    }
2
  • If the socket can't establish the connection to the given address..it will throw a plain socket error..you can get it within try catch.. Oct 12, 2015 at 8:41
  • @AnoopM I edited my post. I put there my try/catch block but nothing appears as an execption...
    – epsilones
    Oct 12, 2015 at 8:43

2 Answers 2

0

Try this

     try {
        Socket socket = new Socket(IP_ADDRESS, PORT);
        socket.setSoTimeout(1500);   
      } catch (IOException ex) {
         Log.e("Connection Error",String.valueOf(ex));
      }

Replace your code with this and wait some seconds(60 sec for now) you can see the error toast..

private void getUDPData() throws IOException {
        class ProcessUPDTask extends AsyncTask<String, Void, Socket> {

            private Exception exception;

            private Socket socket;

            public ProcessUPDTask() throws IOException {

            }
            protected Socket doInBackground(String... urls) {
                try {
                    try {
                        socket = new Socket("192.168.1.101", 1234);
                        socket.setSoTimeout(1500);
                    } catch (IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
                            }
                        });
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "Reached", Toast.LENGTH_LONG).show();
                        }
                    });
                    while (true) {
                        try {
                            dataInputStream = new DataInputStream(socket.getInputStream());
                            dataOutputStream = new DataOutputStream(socket.getOutputStream());
                            System.out.println("ip: " + socket.getInetAddress());
                            System.out.println("message: " + dataInputStream.readUTF());
                            dataOutputStream.writeUTF("Hello!");
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } finally {
                            if (socket != null) {
                                try {
                                    socket.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                            if (dataInputStream != null) {
                                try {
                                    dataInputStream.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                            if (dataOutputStream != null) {
                                try {
                                    dataOutputStream.close();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    this.exception = e;
                    e.printStackTrace();
                    return null;
                }
            }

            protected void onPostExecute(Socket socket) {
                // TODO: check this.exception
                // TODO: do something with the feed
            }
        }
        new ProcessUPDTask().execute();
    }
8
  • sorry but it appears that the program can not reach the line after ` Socket socket = new Socket(IP_ADDRESS, PORT);`
    – epsilones
    Oct 12, 2015 at 8:50
  • Your trying to say that the next lines of code couldn't executes after Socket socket = new Socket(IP_ADDRESS, PORT) ? right ? Oct 12, 2015 at 8:53
  • that's what I guess since the catch block is not read and that if I log something just after try/catch I get nothing...
    – epsilones
    Oct 12, 2015 at 8:54
  • I can't figure out any thing weird in your code it will probably print an exception within 1.5 second as you set timeout inside the try catch.and execute Log.d("TAG","this line is reached") as finally and won't execute System.out.println("ip: " + socket.getInetAddress());..go through your debug console of logcat carefully.. System.out.println("message: " + dataInputStream.readUTF()); dataOutputStream.writeUTF("Hello!"); if you aren't establish the connection.go trough your debug console of logcat carefully.. Oct 12, 2015 at 9:13
  • do you think it can be related to Android Studio ?
    – epsilones
    Oct 12, 2015 at 9:16
0

You can configure this and use transData() between connectivity.

private void transData(int sending_msg_int) throws IOException {

        String sending_msg = Integer.toString(sending_msg_int);
        SocketAddress socketAddress = new InetSocketAddress(ip, Data.Port);
        DatagramSocket ds = new DatagramSocket();

        byte[] buffer = sending_msg.getBytes();

        DatagramPacket dp = new DatagramPacket(buffer, buffer.length,
                socketAddress);
        ds.send(dp);
        ds.close();

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.