Java桌面客户端不向android模拟器发送消息

时间:2013-10-24 16:42:01

标签: android

我正在开发一个客户端服务器应用程序。我的客户端是一个java应用程序,我的服务器是一个Android模拟器。我想从客户端发送消息到服务器但在android模拟器上没有收到消息。 Java桌面客户端代码

Socket s = null;
    BufferedReader get = null;
    PrintWriter put = null;
    try {
         s = new Socket("127.0.0.1", 6000);




        put = new PrintWriter(s.getOutputStream(), true);



        put.println("hi");

    }
    catch(Exception ex)
    {

    }

Android服务器代码

private ServerSocket serverSocket;

Handler updateConversationHandler;

Thread serverThread = null;

private TextView text;

私人EditText文本框;

public static final int SERVERPORT = 6000;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView) findViewById(R.id.text2);
    updateConversationHandler = new Handler();
    this.serverThread = new Thread(new ServerThread());
    this.serverThread.start();
}
@Override
protected void onStop() {
    super.onStop();
    try {
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class ServerThread implements Runnable {
    public void run() {
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                socket = serverSocket.accept();
                CommunicationThread commThread = new CommunicationThread(socket);
                new Thread(commThread).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class CommunicationThread implements Runnable {

    private Socket clientSocket;

    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {

        this.clientSocket = clientSocket;

        try {

            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {


        while (!Thread.currentThread().isInterrupted()) {

            try {

                String read = input.readLine();

                updateConversationHandler.post(new updateUIThread(read));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

class updateUIThread implements Runnable {
    private String msg;

    public updateUIThread(String str) {
        this.msg = str;
    }

    @Override
    public void run() {
        text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
    }

}

1 个答案:

答案 0 :(得分:1)

documentation的相关引用(向下滚动到网络地址空间部分:

Each instance of the emulator runs behind a virtual router/firewall service 
that isolates it from your development machine's network interfaces and settings
and from the internet. An emulated device can not see your development machine
or other emulator instances on the network. Instead, it sees only that it is
connected through Ethernet to a router/firewall.

The virtual router for each instance manages the 10.0.2/24 network address
space — all addresses managed by the router are in the form of 10.0.2.<xx>,
where <xx> is a number. Addresses within this space are pre-allocated by the
emulator/router as follows:

然后他们继续在表格中显示预先分配的地址。