JAVA网络编程新手问题

来源:百度知道 编辑:UC知道 时间:2024/05/24 04:21:06
在网络编程中使用readUTF()等操作输入输出流时容易产生阻塞。比如写一个聊天的程序,只能一边说一句,另一边接收完程序才能往下执行。这要如何解决?有没有聊天程序两边可以任意输入的源代码给个?

public class chatClient extends Frame {

/**
* @param args
*/
TextField tfTxT=new TextField();
TextArea taContent=new TextArea();
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected =false;

public static void main(String[] args) {
new chatClient().lunachFrame();
}

private class RecvThread implements Runnable{

public void run() {
try{
while(bConnected){
String str=dis.readUTF();
taContent.setText(taContent.getText()+str+'\n');
}
}catch(IOException e){
e.printStackTrace();
}
}

}

public void lunachFrame(){
this.setLocation(400, 300);
this.setSize(300,300);
//this.setLayout(new FlowLayout());
this.add(tfTxT,"South");
this.add(taCont