获取null套接字 - 与本地主机进行android通信

时间:2014-04-20 17:08:33

标签: java android sockets localhost

我在用Java编写的本地主机上运行服务器,我试图让我的设备与它通信。我已经尝试了很多方法,并且我已经阅读了很多关于它的帖子,但仍然没有提出解决方案。这就是我写的:

public class MainActivity extends Activity {
    String hostName = "10.0.2.2";
    int portNumber = 5667;
    private Socket echoSocket;


    private void runSecondThread() {

        new Thread() {

            @Override
            public void run() {
                try {
                    Log.i("try", "enter try");
                    echoSocket = new Socket(hostName, portNumber);
                    Log.i("taggg", "echoSocket");

                } catch (UnknownHostException e) {
                    Log.i("error", "Don't know about host " + hostName);
                    System.exit(1);
                } catch (IOException e) {
                    Log.i("error", "Couldn't get I/O for the connection to " + hostName);
                    System.exit(1);
                }
            }
        }.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        runSecondThread();

    }

我发现我的echoSocket是null。我的设备连接到与我的PC相同的Wi-Fi。

0 个答案:

没有答案