熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

傳輸文本

2013-11-15 11:36:05  來源: JSP教程 

  下面要做的是將文本從閒聊服務器傳輸到顯示屏上作這個工作
  最好的辦法是應用threadthread隨套接口的打開和關閉而打開
  和關閉thread能夠同時讀取相當大量的數據在從套接口讀取
  數據和更新TextArea方面效率都很高
  Chatjava:
  
  
  import javaappletApplet;
  import javaawt*;
  import Socket;
  import javaioInputStream;
  
  public class Chat extends Applet implements Runnable {
  TextArea ta;
  TextField tf;
  Socket s;
  Thread t;
  
  public void init() {
  ta = new TextArea();
  tasetEditable(false);
  add(ta);
  
  tf = new TextField();
  add(tf);
  }
  
  public void start() {
  try {
  String host = getParameter(host);
  int port = IntegerparseInt(getParameter(port));
  s = new Socket(hostport);
  t = new Thread(this);
  tstart();
  } catch(Exception e) {
  taappendText(applet error: +e+\n);
  }
  }
  
  public void stop() {
  try {
  tstop();
  sclose();
  } catch(Exception e) {
  taappendText(applet error: +e+\n);
  }
  }
  
  public void run() {
  try {
  InputStream is = sgetInputStream();
  byte[] buf = new byte[];
  
  while(true) {
  int avail = isavailable();
  if(avail<1) avail=1;
  if(avail>buflength) avail=buflength;
  int bytes_read = isread(bufavail);
  taappendText(new String(buf bytes_read));
  }
  } catch(Exception e) {
  Systemerrprint(e);
  }
  }
  
  public boolean handleEvent(Event e) {
  if(eid==EventACTION_EVENT && etarget==tf) {
  taappendText(tfgetText()+\n);
  tfsetText();
  return true;
  }
  return false;
  }
  
  }
  

From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19276.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.