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

时间:2017-02-11 16:02:13

标签: java android sockets

我需要通过单击按钮从核心java服务器向android客户端发送文本消息。但是,它不会显示错误,并且不会将消息发送到客户端。

服务器端按钮代码:

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){

    }
} 

客户端代码:

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();

1 个答案:

答案 0 :(得分:0)

readLine()读取行。你没有发送一条线。您使用writeUTF()发送,它使用Javadoc中记录的特殊格式。发送行或使用readUTF)