本地PC和android模拟器服务器套接字连接

时间:2019-05-04 23:09:54

标签: java android sockets serversocket localserversocket

服务器套接字不接受连接。

我尝试将我创建的服务器套接字连接到开发计算机上的127.0.0.1

OnCreate类

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    messaging_icon = (ImageButton) findViewById(R.id.textButton);
    messaging_icon.setOnClickListener(hostServer.this);
    new_textmessage = (EditText) findViewById(R.id.new_text2);

    try{

        newSocket = new ServerSocket();
        newSocket.setReuseAddress(true);
        newSocket.bind(new InetSocketAddress(6000));//8080
    }
        catch(IOException e){
        e.printStackTrace();
        }
}

线程

    Thread newThread = new Thread(new Runnable() {

        @Override
        public void run() {

            Boolean endofLoop = true;
            while (endofLoop == true) {





                try {
                    Log.d("tag","Accepted");



                    connectionSocket= newSocket.accept();


                    Log.d("tag","Accepted");
                    inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                    ClientInput = inFromClient.readLine();
                    Log.d("tag", ClientInput+"");

                    try{
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e){
                        e.printStackTrace();
                    }

                    System.out.println("Clients Words: "+ClientInput);
                    ClientInput = "";
                    //new2.close();
                   // newSocket.close();
                    endofLoop = false;
                    new_textmessage.getText().clear();
                }
                catch (IOException e) {
                    e.printStackTrace();
                    System.out.println((SERVERIP));;
                }

            }}});

....

另一端的客户端能够将TCP消息发送到端口5000,然后假定使用redir add TCP:5000:6000将来自端口5000的消息从主机重定向到设备,但是从未发生过accept方法

0 个答案:

没有答案