📄 client.java
字号:
//Client.java
/**
@author Joedan
*/
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class Client
{
String msg;
Client(String ip, int port)
{
try
{
s = new DatagramSocket();
ps = new DatagramPacket("".getBytes(), 0, InetAddress.getByName(ip), port);
// ps = new DatagramPacket("".getBytes(), 0, InetAddress.getLocalHost(), port);
ps2 = new DatagramPacket("".getBytes(), 0);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
public String 登录(String id, String password)//处理用户登录事务,返回登录结果
{
this.id = id;
this.password = password;
try
{
s.setSoTimeout(MyConstants.TIMEOUT);//设置超时
for (int i = 0; i < MyConstants.MAXRETRY; i++)
{
发送上线数据包();
try
{
pr = 读取数据包WithTimeout();
}
catch(SocketTimeoutException e)
{
continue;//超时,试下一次
}
tmp = new String(pr.getData()).trim();//收到数据包,将数据包内容放入tmp
if (tmp.startsWith(MyConstants.登录结果))//if 收到登录结果
{
s.setSoTimeout(0);//取消超时的设置
return tmp.substring(MyConstants.LENGTH_OF_RULE);//返回 登录结果值
}
}
s.setSoTimeout(0);//取消超时的设置
return MyConstants.登录结果_超时;//超过重试次数仍未收到登录结果
}
catch(Exception e)
{
return MyConstants.登录结果_失败;
}
}
public void 下线()//处理用户下线事务
{
发送下线数据包();
}
public void 刷新好友列表()//请求服务器发送好友列表与在线好友列表
{
发送请求好友列表数据包();
发送请求在线好友列表数据包();
}
public boolean 是否在线()//是否在线
{
return false;
}
public int 发送数据包(DatagramPacket p)//发送数据包
{
try
{
s.send(p);
}
catch(Exception e)
{
return -1;
}
return 0;
}
public DatagramPacket 读取数据包()//读取数据包
{
try
{
return 读取数据包WithTimeout();
}
catch(SocketTimeoutException e1)//设置超时时,会抛出此类例外
{
return null;
}
}
public DatagramPacket 读取数据包WithTimeout() throws SocketTimeoutException//读取数据包
{
try
{
for (int i = 0; i < MyConstants.MAXSIZE; i++)//将缓冲区清空
buffer[i] = 0;
s.receive(pr);
}
catch(SocketTimeoutException e1)//设置超时时,会抛出此类例外
{
throw(e1);
}
catch(IOException e)
{
return null;
}
return pr;
}
public String 发送注册数据包(String id, String name, String password)// 用户id,用户名,密码
{
tmp = MyConstants.注册 + id + "," + name + "," + password;
try
{
s.setSoTimeout(MyConstants.TIMEOUT);//设置超时
for (int i = 0; i < MyConstants.MAXRETRY; i++)
{
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
try
{
pr = 读取数据包WithTimeout();
}
catch(SocketTimeoutException e)
{
continue;//超时,试下一次
}
tmp = new String(pr.getData()).trim();//收到数据包,将数据包内容放入tmp
if (tmp.startsWith(MyConstants.注册结果))//if 收到结果
{
s.setSoTimeout(0);//取消超时的设置
return tmp.substring(MyConstants.LENGTH_OF_RULE);//返回 注册结果值
}
}
s.setSoTimeout(0);//取消超时的设置
return MyConstants.注册结果_超时;//超过重试次数仍未收到登录结果
}
catch(Exception e)
{
return MyConstants.注册结果_其它错误;
}
}
public void 发送上线数据包()//
{
tmp = MyConstants.上线 + id + "," + password;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public int 发送下线数据包()//
{
tmp = MyConstants.下线 + id;
ps.setData(tmp.getBytes(), 0, tmp.length());
return 发送数据包(ps);
}
public void 发送增加好友数据包(String id)//传入预增加好友的id
{
tmp = MyConstants.增加好友 + this.id + "," + id;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public void 发送删除好友数据包(String id)// 传入预删除好友的id
{
tmp = MyConstants.删除好友 + this.id + "," + id;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public void 发送请求在线好友列表数据包()//
{
tmp = MyConstants.请求在线好友列表 + id;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public void 发送请求好友列表数据包()//
{
tmp = MyConstants.请求好友列表 + id;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public void 发送修改密码数据包(String newPassword)//
{
tmp = MyConstants.修改密码 + id + "," + newPassword;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public void 发送查询在线用户数据包()
{
tmp = MyConstants.查询在线用户;
ps.setData(tmp.getBytes(), 0, tmp.length());
发送数据包(ps);
}
public void 发送好友消息数据包(SocketAddress add, String msg)//传入对方地址和想发送的消息
{
ps2.setSocketAddress(add);
ps2.setData((MyConstants.好友消息 + id + MyConstants.SPLIT + msg).getBytes());
发送数据包(ps2);
}
//private:
private String id = "";//本客户端id
private String password = "";//本客户端password
private DatagramSocket s;
private DatagramPacket ps;//用于发送到服务器消息
private DatagramPacket ps2;//用于发送好友消息
private byte [] buffer = new byte[MyConstants.MAXSIZE];
private DatagramPacket pr = new DatagramPacket(buffer, 0, buffer.length);
private String tmp = "";
public static void main(String args[])
{
Client c = new Client("127.0.0.1", 5959);
c.发送注册数据包("4137759", "Joedan", "test");// 用户id,用户名,密码
c.登录("4137759", "test");//
c.发送增加好友数据包("10000");//
c.发送删除好友数据包("10000");//
c.发送请求在线好友列表数据包();//
c.发送请求好友列表数据包();//
c.发送修改密码数据包("newPassword");// 传入新密码
c.发送查询在线用户数据包();
c.下线();//
}
static void 提示(String s)
{
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -