我想實現一個功能,可以實時地在頁面上反映出客戶端傳來的消息。我想實現的效果是:當該頁面被打開的時候,此時端口接收到的信息就會顯示到頁面上。我已經用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
提交
取消