[英]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.