📄 chatclient.java
字号:
//ChatClient.java
import protocols.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
//此类用来处理和服务器的连接
public class ChatClient
{
private boolean connected;//是否连接标志
int serverSocketNumber;//连接数目
String serverAddress;//服务器地址
private Socket socket;//端口号
String Name;//机器名
private int clientID;
ObjectOutputStream out;//输出流
ObjectInputStream in;//输入流
DefaultListModel clientList;
private InputListener listener;//监听端口
ClientInterface window;
//对进来的信息进行回应
class InputListener extends Thread
{
//提供中断循环的方式
boolean running=false;
public void run()
{
//一直循环直到Listener停止
while(true)
{
if(running)
{
// 测试信息类型并作出相映的反映
try
{
Object serverMsg=in.readObject();
if(serverMsg instanceof Message)
{
window.showMessage((Message)serverMsg);
}
else if(serverMsg instanceof ChatRequest)
{
window.openNewTab(((ChatRequest)serverMsg).senderId);
}
else if(serverMsg instanceof UpdateList)
{
if(((UpdateList)serverMsg).requestType==true)
{
clientList.addElement(((UpdateList)serverMsg).newClient);
}
else
{
window.notifyUserLeft(((UpdateList)serverMsg).newClient);
clientList.removeElement(((UpdateList)serverrMsg).newClient);
}
}
else if(serverMsg instanceof ServerShutDown)
{
disconnectFromServer(false);
window.notifyDisconnect();
JOptionPane.showMessageDialog(window,"Server Has Been Shut Down","Connection Error",JOptionPane,ERROR_MESSAGE);
}
else if(serverMsg instanceof KickedOutNotice)
{
disconnectFromServer(false);
window.notifyDisconnect();
JOptionPane.showMessageDialg(window,"Server Kickde You Out","Connection Error",JOptionPane.ERROR_MESSAGE);
}
}
catch(ClassNotFoundException cnfe)
{
JOptionPane.showMessageDialog(windw,"Class of a serialized object cannot be found.","Termination Error",JOptionPane.ERROR_MESSAGE);
shutDown();
}
catch(InvalidClassException ice)
{
JOptionPane.showMessageDialog(windw,"Something is wrng with a class used by serialization.","Termination ERROR",JOptionPane.ERROR_MESSAGE);
shutDowwn();
}
catch(StremCorruptedException sce)
{
JOptionPane.showMessageDialog(window,"Control infrmation in the stream is inconsistenr.","Termination Error",JOptionPane.ERROR_MESSAGE);
shutDown();
}
catch(OptionalDataException ode)
{
JOptionPane.showMessageDialog(window,"Primitive data was found in the stream instead of objects.","Termination Error",JOptionPane.ERROR_MESSAGE);
shutDown();
}
catch(IOException ioe)
{
JOptionPane.ERROR JOptionPane.showMessageDialog(null,"Any of the usual_MESSAGE");
//shutDown();
}
}
}
}
}
//将数据从用户端发送到服务器端
void sendPublicMessage(String userMsg)throws IOException
{
Message msg=new Message();//产生新的信息
msg.audience=true;
msg.roomNumber=0;//聊天室号码
msg.senderId=clientID;
msg.message=Name+"says>"+userMsg;
out.writeObject(msg);//发送信息
out.flush();//清空信息
}
//只是对于一个用户发送信息
void sendPrivateMesssge(int recipient, String userMsg)
throws IOException
{
Message msg = new Message();
msg.audience = false;
msg.recieverId = recipient;
msg.senderId = clientID;//设定用户号
msg.message = Name + "says>" + userMsg;
out.writeObject(msg);
out.flush();
}//某个用户想与另一个用户单独交谈
void sendChatRequest(int recieverId)
{
ChatRequest request = new ChatRequest();//获得聊天请求
request.recieverId = recieverId;
request.senderId=clientID;
try
{
out.writeObject(request);
}
catch(IOEException io_e)
{}
}
//发送新的配置消息
void setConfiguration(String newServer,int newport)
{
try
{
FileWrite.configFile=new FileWriter("clientConfig.cfg");
//设定配置文件
configFile.Write(newServer.trim()+";"+newPort+";");
configFile.close();//关闭配置文件
}
catch(IOExcept io_e)
{
JOptionPane.showMessageDialog(windw,"Cannt Save ConfigurationFile","File Error",JOption.ERROR_MESSAGE);
}
}//从文件获取配置信息,并保存在变量中
void getConfiguration()
{
try
{
char[]buffer=new char[255];
FileReader configFile=new FileReader("clientConfig.cfg");
configFile.read(buffer);//从缓冲区中读如信息
serverAddress=String.copyValueOf(buffer);//获得服务器地址
String[] temp=serverAddress.split(";");
serverAdress=temp[0];
serverSocketName=Integer.parseInt(tamp[1]);
}
catch(FileNotFoundException fnf_e)
{
JOptionPane.showMessageDialog(window,"Configuration File Not Found,Using Defaults","Configuration File Missing","JOptionPane.ERROR_MESSAGE");
serverSocketName=1665;//设置服务器端口号
serverAdress="localhost";
}
catch(IOException io_e)
{
JOptionPane.showMessageDialog(window,"Error Reading Configuration File,Using Defaults","Configuration Error","JOptionPane.ERROR_MESSAGE");
serverSocketName=1665;//设置服务器端口号
serverAdress="localhost";
}
}
//从服务器断开,reason=true 为用户选择,reason=false 为服务器选择
synchronized void disconnectFromServer(boolean reason)
{
try
{
if(connected)
{
//停止listener线程
listener.running=false;
if(reason=true)
{
out.writeObject(new LogOut());
out.flush();
}
out.close();//关闭输出流
socket.close();//关闭套接字
clientList.clear();
connected=false;
}
}
catch(IOExceptin ex)
{}
}
//开始连接服务器
synchronized boolean connectToServer()
{
getCnfiguration();
try
{
InetAddress addr=InetAddress.getByName(serverAddress);
//获得服务器地址
socket=new Scket(addr,serverSocketNumber);
}
catch(UnknownHostExceptin e)
{
JOptinPaneshwMessageDialg(window,"Host Not Found,Reconfigure...","Host Lookup Error",JOptinPane.ERROR_MESSAGE);
return false;
}
catch(IOExcption e)
{
JOptionPane.showMessageDialog(window,"Server Nt Fund,Check If Server Exists..","Scket Error",JOptinPane.ERROR_MESSAGE);
return false;//返回错误信息
}
try
{
in=new ObjectInputStream(socket.getInputStream());//输入流
out=new ObjectOutputStream(socket.getOutputStream());//输出流
}
catch(IOException e)
{
JOptinPane.showMessageDialg(windw,"Cannot Create Data Stream,Clsing Client...","Data Stream Error",JOptinPane.ERROR_MESSAGE);
try
{
scket.clse();//关闭套接字
}
catch(IOException iO_e)
{}
return false;
}
if(!handShake())
return false;
listener.running=true;
//是第一次连接则开启listener对象
if(Listener.isAlive()==false)
{
listener.start();
}
connected=true;
return true;
}
//设置参数内容
private boolean handshake()
{
try
{
if(((ConnectionNotice)in.readObject()).status)
{
out.writeObject(Name);//通过对象序列化发送信息
if(((ConnectionNtice)in.readObject()).status==false)
{
JOptionPane.showMessageDialog(window,"Name Already In Use.Change Login Name","Nick Error",JptionPane.ERROR_MESSAGE);
return false;
}
clientList=(defaultListModel)in.readObject();//获得用户信息列表
clientID=clientList.getSize()-1;//获得客户端ID号
return true;
}
else
{
JOptinPane.showMessageDialog(windw,"Maximum User Limit Reached.Server Rejected Connection","Cnnection Rejected",JOptinPane.ERROR_MESSAGE);
}
}
catch(Exceptio e)
{
}
return false;
}
//关闭应用连接
void shutDown()
{
disconnectFromServer(ture);//断开连接
listener=null;
System.exit(0);//退出系统
}
ChatClient()
{
connected=false;
listener=new InputListener();
window=new ClientInterface(this);
}
public static void main(String args[])throws IOException
{
new ChatClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -