下面要做的是將文本從閒聊服務器傳輸到顯示屏上
最好的辦法是應用thread
和關閉
數據和更新TextArea方面效率都很高
Chat
import java
import java
import
import java
public class Chat extends Applet implements Runnable {
TextArea ta;
TextField tf;
Socket s;
Thread t;
public void init() {
ta = new TextArea(
ta
add(ta);
tf = new TextField(
add(tf);
}
public void start() {
try {
String host = getParameter(
int port = Integer
s = new Socket(host
t = new Thread(this);
t
} catch(Exception e) {
ta
}
}
public void stop() {
try {
t
s
} catch(Exception e) {
ta
}
}
public void run() {
try {
InputStream is = s
byte[] buf = new byte[
while(true) {
int avail = is
if(avail<1) avail=1;
if(avail>buf
int bytes_read = is
ta
}
} catch(Exception e) {
System
}
}
public boolean handleEvent(Event e) {
if(e
ta
tf
return true;
}
return false;
}
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19276.html