📄 client.java
字号:
package dssserver;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.StringTokenizer;
//实现 Client线程类
class Client implements Runnable{
Socket socket; //用来存储一个连接客户的socket信息
String name; //用来存储客户的连接姓名
String pswd;
String ip;
String info ; //用来存储客户的ip信息
BufferedInputStream in; //用来实现接受从客户端发来的数据流
PrintStream out;
byte[] inBytes=new byte[500];
static String ENC="GB2312";
Thread thread;
ServerMain smparent;
DataBase db;
void thstop(){//线程停止
thread=null;
}
void start(){//线程启动
thread = new Thread(this);
thread.start();
}
public Client(Socket s,ServerMain sa){ //Client线程类的构造器
smparent = sa;
socket = s;
try{
in = new BufferedInputStream(s.getInputStream()); //存储特定客户socket的输入流接受s这个客户发送到服务器端的信息
out = new PrintStream(s.getOutputStream()); //存储特定客户socket的输出流发送服务器给s这个客户的信息
in.read(inBytes); //从(ip 端口)得到数据
info = new String(inBytes,ENC);
StringTokenizer stinfo = new StringTokenizer(info,":"); //用StringTokenizer类来读取用":"分段字符
String head = stinfo.nextToken(); //head用来存储类似于关键字的头信息
db = new DataBase();
if(head.equals("INFO")){
if(stinfo.hasMoreTokens())
name = stinfo.nextToken(); //关键字后的第二段数据是客户名信息
if(stinfo.hasMoreTokens())
pswd = stinfo.nextToken();
if(stinfo.hasMoreTokens())
ip = stinfo.nextToken(); //关键字后的第四段数据是客户ip信息
if(db.checkuser(this))//把this就是当前类的变量值, 他里面有变量的值
send("SUC:"); //向(ip 端口)写数据
else
send("REFUSE:");
}
else if(head.equals("REG")){
String username=stinfo.nextToken();
String userpswd=stinfo.nextToken();
db.reguser(username,userpswd);
s.close();
}
}
catch(IOException e)
{
System.out.println("网络I/O错误:"+e);
}
}
public void send(String msg){ //实现向客户端发送信息的方法
out.print(msg);
out.flush();
}
public void run(){
Thread thisThread = Thread.currentThread();
while(thread == thisThread){
String line = null; //用来的到客户端发来的信息
try{
in.read(inBytes);
line = new String(inBytes,ENC);
}
catch(IOException e){
System.out.println("网络I/O错误"+e);
smparent.notifyOthers(this,"QUIT");
smparent.disconnect(this);
return;
}
if(line == null){ //客户已离开
smparent.notifyOthers(this,"QUIT");
smparent.disconnect(this);
return;
}
StringTokenizer st=new StringTokenizer(line,":"); //的到客户端发来的信息, 用StringTokenizer分开
String keyword=st.nextToken();
if(keyword.equals("MSG")){ //如果关键字是MSG则是客户端发来的聊天信息
String toname=st.nextToken();
if(smparent.clientName.contains(toname)){
int index=smparent.clientName.indexOf(toname);
Client c=((Client)smparent.clients.elementAt(index));
c.send(line);
}
else{
String errString="ERROR:"+st.nextToken()+":"+toname+":"+"我下线了"+":";
send(errString);
}
}
else if(keyword.equals("QUIT")){
smparent.notifyOthers(this, "QUIT");
smparent.disconnect(this);
}
else if(keyword.equals("ASKFOR")){ //选择和谁对话
String peo= "EXIST:"+smparent.clientName.size();
for(int i= 0;i<smparent.clientName.size();i++){
peo=peo+":"+smparent.clientName.elementAt(i);
}
peo=peo+":";
send(peo);
smparent.notifyOthers(this,"NEW");
try{
smparent.clients.addElement(this);
smparent.clientName.addElement(this.name);
}
catch(NullPointerException e){
}
}
else if(keyword.equals("FILE_TO")){ //文件发送
String toname=st.nextToken();
String me=st.nextToken();
if(smparent.clientName.contains(toname)){
int index=smparent.clientName.indexOf(toname);
Client c=((Client)smparent.clients.elementAt(index));
c.send(line);
}
}
else if(keyword.equals("FILE_FROM")){ //接收文件
String toname=st.nextToken();
String me=st.nextToken();
if(smparent.clientName.contains(toname)){
int index=smparent.clientName.indexOf(toname);
Client c=((Client)smparent.clients.elementAt(index));
c.send(line);
}
}
}
}
public boolean checkName(){ //检查是否有相同的用户连接
if(smparent.clientName.contains(name)||(name == "SERVERSHUTDOWN"))
return false;
else return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -