簡體   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