我想实现一个功能,可以实时地在页面上反映出客户端传来的消息。我想实现的效果是:当该页面被打开的时候,此时端口接收到的信息就会显示到页面上。我已经用socket和循环来获取客户端信息了,但是不知道要怎么把这些信息显示到页面上。下面是用于接收的线程代码:public class ReceiveThread extends Thread { static final int SOCKET_PORT_0 = 8800; // 端口号 static ServerSocket mServerTest = null; static Socket mSocket = null; static InputStream mInput = null; byte[] buffer; public void init() { buffer = new byte[65536]; } public void run() { try { mServerTest = new ServerSocket(SOCKET_PORT_0); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int size = -1; while (true) { try { // mOutput = mSocket.getOutputStream(); if (size < 0) { System.out.println("等待客户端的链接...."); mSocket = mServerTest.accept(); System.out.println("服务器测试程序已链接...."); } else { byte[] realBuffer = new byte[size]; System.arraycopy(buffer, 0, realBuffer, 0, size); System.out.print("Message from server: "); } Thread.sleep(100); mInput = mSocket.getInputStream(); size = mInput.read(buffer); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
添加回答
举报
0/150
提交
取消