繁体   English   中英

服务器无法从客户端Java接收文本

[英]Server is not able to receive the text from client java

我正在创建一个应用程序,客户端可以在单击按钮时向服务器发送一些文本。我想在控制台服务器端打印该文本,但是每当按下客户端按钮时,都不会发生任何事情。

有时我在控制台android.support.v7.widget.AppCompatEditText@b2253390上得到了这一行。 我不明白为什么会这样吗?

服务器代码:

public static ServerSocket server = null;
public static Socket client = null;

public static void main(String []arg)throws IOException{
    try {
        server = new ServerSocket(8002); 
        System.out.println("Server Started..............."); 
        while(true){
        client = server.accept();
        DataInputStream in = new DataInputStream(client.getInputStream());
        String msg = in.readLine(); 
        System.out.println("and: "+msg); 
        }
    }catch(IOException r){
        System.out.println("error :"+r.getMessage()); 
    }
}

客户代码:

public void send(){
    send.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Socket cs = null;
                    try {
                        cs = new Socket("192.168.1.100", 8002);
                        DataOutputStream out = new DataOutputStream(cs.getOutputStream());
                        out.writeBytes(text.toString());
                    } catch (IOException e) {
                        Toast.makeText(KeyboardActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
    );
}

这是因为textEditText

我想你的意思是

out.writeBytes(text.getText().toString());

代替,

out.writeBytes(text.toString());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM