通过wifi连接到套接字 - android

时间:2015-10-12 08:37:49

标签: java android sockets wifi

我正在尝试使用Android中的套接字通过wifi连接到本地网络中UDP为{{{{}}的机器上的某个some_port端口(ip) 1}}。

当我跑步时

some_ip

我没有收到任何消息错误,但程序似乎没有读取此行,并且在socket = new Socket(some_ip, some_port); 周围时我无法记录错误。

我该如何调试?

编辑1:这是我的try/catch

try/catch

编辑2:这里是整个代码

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

2 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

您可以对此进行配置,并在连接之间使用transData()。

declare @PTSID = '7VD6GSZG'
Select top (1)
p_id,   
insp_status 
from Insp_Results   
where RESV_PTSID = @PTSID
order by RESD_DATE desc