繁体   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