簡體   English   中英

通過wifi連接到套接字-android

[英]connecting to socket over wifi - android

我正在嘗試使用Android套接字通過wifi連接到我的本地網絡中ipsome_ip的計算機上的某些UDP端口( some_port )。

當我跑步

socket = new Socket(some_ip, some_port);

我沒有收到任何消息錯誤,但該程序似乎無法讀取此行,而在try/catch周圍時,我無法記錄該錯誤。

我該如何調試?

編輯1:這是我的try/catch

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

編輯2:這是完整的代碼

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();
    }

嘗試這個

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

以此替換代碼,等待幾秒鍾(現在為60秒),您會看到錯誤提示。

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();
    }

您可以配置它並在連接之間使用transData()。

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();

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM