简体   繁体   English

通过单击按钮将文本消息从 java 服务器发送到 android 客户端使用套接字

[英]send text message from java server to android client usig sockets by clicking on a button

I need to send a text message from core java server to android client by clicking on a button.我需要通过单击按钮从核心 java 服务器向 android 客户端发送一条短信。 However, it does not show an error, and the message is not sent to client side.但是,它不会显示错误,并且消息不会发送到客户端。

Server side button code:服务器端按钮代码:

private void msg_sendActionPerformed(java.awt.event.ActionEvent evt)   {                                         
// TODO add your handling code here:
    String msgout="";
    msgout=msg_text.getText().toString();
    try{
        dout.writeUTF(msgout);
        //printWriter.write(msgout + "\n");
        System.out.printf(msgout);
        //printWriter.flush();
    }catch(Exception e){

    }
} 

Client side code:客户端代码:

Thread r = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            s = new Socket("192.168.1.5", 1201);
            // din=new DataInputStream(s.getInputStream());
            inputStreamReader = new InputStreamReader(s.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader);

            dataout = new DataOutputStream(s.getOutputStream());
            String message = "";
            while (!message.equals("exit")) {
                //msgin=din.readUTF();
                message = bufferedReader.readLine();

                msg_area.setText(msg_area.getText().toString() + "\n       Server:\t" + message);
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Exception-->" + e.getMessage(), Toast.LENGTH_SHORT).show();

        }
    }
});
r.start();

readLine() reads lines. readLine()读取行。 You aren't sending a line.你不是在发送一条线。 You're sending with writeUTF() , which uses a special format, documented in the Javadoc.您使用writeUTF()发送,它使用 Javadoc 中记录的特殊格式。 Either send lines or use readUTF) .发送行或使用readUTF)

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

相关问题 有没有办法只在 Java 套接字中从服务器向一个客户端发送消息? - is there a way to send a message from server to one client only in Java sockets? 通过TCP套接字从Java Server返回消息到Android客户端 - Return message to Android client from java Server via tcp sockets Java套接字 - 将文件从客户端发送到服务器 - Java Sockets - Send a file from the Client to the Server Java套接字 - 将数据从服务器发送到客户端 - Java Sockets - send data from server to client 在Java套接字中将对象从服务器发送到客户端 - send objects from server to client in java sockets 服务器向客户端套接字发送消息 - Server send message to client Sockets 如何使用Java中的套接字从服务器向特定客户端发送字符串消息? - How could I send a string message to a specific client from a server using sockets in java? 使用 sockets 将消息从 Java 客户端发送到 Python 服务器,反之亦然 - Send message from Java client to Python server using sockets and vice versa 无法通过文本字段将消息从服​​务器发送到android中的客户端 - Unable to send message from server to the client in android through text field 是否可以使用TCP套接字将JSON数据从Java服务器发送到Android客户端 - Is it possible to send json data from java server to android client using TCP sockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM