我想實(shí)現(xiàn)一個(gè)功能,可以實(shí)時(shí)地在頁(yè)面上反映出客戶(hù)端傳來(lái)的消息。我想實(shí)現(xiàn)的效果是:當(dāng)該頁(yè)面被打開(kāi)的時(shí)候,此時(shí)端口接收到的信息就會(huì)顯示到頁(yè)面上。我已經(jīng)用socket和循環(huán)來(lái)獲取客戶(hù)端信息了,但是不知道要怎么把這些信息顯示到頁(yè)面上。下面是用于接收的線(xiàn)程代碼:public class ReceiveThread extends Thread { static final int SOCKET_PORT_0 = 8800; // 端口號(hào) 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("等待客戶(hù)端的鏈接...."); mSocket = mServerTest.accept(); System.out.println("服務(wù)器測(cè)試程序已鏈接...."); } 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(); } } }
添加回答
舉報(bào)
0/150
提交
取消