📄 client.java
字号:
package chatnew;
import java.net.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Client extends Thread {
Listener listener=null;
Socket client=null;
List onLineUsersList=null,chatContentList=null;
String word="";
boolean isClosed=false;
public Client(String ip,int port,List onLineUsersList,List chatContentList) throws UnknownHostException,
IOException {
this.client=new Socket(ip,port);
this.onLineUsersList=onLineUsersList;
this.chatContentList=chatContentList;
this.start();
}
public void run(){
BufferedReader br=null;
PrintStream ps=null;
try {
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
ps=new PrintStream(client.getOutputStream());
//建立对服务器发送来的信息进行监听的线程
listener=new Listener(br,this.onLineUsersList,this.chatContentList);
//循环发送消息给服务器
while(!isClosed){
if(this.word.length()>0){
ps.println(this.word);
this.word="";
}else{
sleep(100);
}
}
//关闭回收操作
listener.destroy();
client.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//提供给可视界面一个发消息的入口
public void send(String s){
this.word=s;
}
public void destroy() {
this.onLineUsersList.removeAll();
this.chatContentList.removeAll();
this.isClosed=true;
}
public Listener getListener(){
return this.listener;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -