📄 client.java
字号:
import java.net.*;
import java.awt.*;
import java.io.*;
import javax.swing.JOptionPane;
/*
* 该类实现创建一个客户端
*/
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(),true);
//建立对服务器发送来的信息进行监听的线程
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)
{
JOptionPane.showMessageDialog(null,"对不起,有错误,请重启程序。","错误",JOptionPane.INFORMATION_MESSAGE);
}
}
//提供给可视界面一个发消息的入口
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 + -