c/s客户端等待服务器信息时无响应
影竹 2011-09-01 03:16:01 我用swt designer做了一个Application window(main.java)作为客户端,与服务器端双向通信,客户端作为main.java的一个内部类。在窗口输入ip,端口号等信息后按下按钮启动客户端线程。但线程启动后非常卡,没几秒就没响应了,只有开始那几秒能收到服务器发送的消息并显示出来
客户端代码如下
class server implements Runnable{
ServerSocket server;
Socket client;
InputStream in;
OutputStream out;
int port;
public void setPort(int port) {
this.port = port;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
server=new ServerSocket(port);
client=server.accept();
text_4.append("客户机是:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
out=client.getOutputStream();
}catch(IOException ioe){}
while(true){
try{
byte[]buf=new byte[256];
in.read(buf);
String str=new String(buf);
text_4.append("\n客户机说:"+str+"\n");
Thread.sleep(1000);
}catch (IOException e){} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void close(){
System.exit(0);
}
public void send(String s){
try{
byte[]buf=s.getBytes();
out.write(buf);
}catch(IOException ioe){}
}