本文实例为大家分享了udp实现简单的多人聊天功能,供大家参考,具体内容如下
多个客户端向服务器发送信息,服务端再将信息返回到各个客户端。
这是接收udp的实现类:
public class Rec implements Runnable{
private DatagramSocket ds;
public Rec(DatagramSocket ds){
this.ds = ds;
}
@Override
public void run() {
while(true){
//接受数据和打印数据
byte[] buf= new byte[1024];
DatagramPacket pac = null;
try {
pac = new DatagramPacket(buf,buf.length);
ds.receive(pac);//接收数据
} catch (IOException e) {
e.printStackTrace();
}
//获得消息
String info = new String(pac.getData(),0,pac.getLength());
//获得ip地址
String ip = pac.getAddress().getHostAddress();
String port = pac.getPort()+"";
String name = info.substring(0, info.lastIndexOf(":"));
if(!name.equals(Thread.currentThread().getName())){
System.out.println(info);
}
}
}
}
这是发送udp的实现类:
public class Send implements Runnable {
private DatagramSocket ds;//负责发送和接受数据
private int receport;//准备发送的端口号
private String ipAddress; //准备发送的ip地址
public Send(DatagramSocket ds,int receport,String ipAddress){
this.ds = ds;
this.ipAddress = ipAddress;
this.receport = receport;
}
@Override
public void run() {
// 多线程发送消息
Scanner sc = new Scanner(System.in);
InetAddress ip = null;
try {
// String ipad = ipAddress.substring(0, ipAddress.lastIndexOf(":"));//ip地址
ip = InetAddress.getByName(ipAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}
while(true){
String info = sc.nextLine();
info=Thread.currentThread().getName()+":"+info;
byte[] bs = info.getBytes();
//把数据封装为数据包
//数据包有四个部分组成
DatagramPacket pack = new DatagramPacket(bs,bs.length,ip,receport);
try {
ds.send(pack);
String content=info.substring(info.lastIndexOf(":")+1);
if(content.equals("886")){
System.out.println("已退出聊天室");
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端:
我是模拟的本地多用户,端口号不能重复所以设置了6001-6005,线上只需同一接收udp端口号即可。
public class ServerCilent {
public static void main(String[] args) throws IOException {
//接受数据包
DatagramSocket rec = new DatagramSocket(6262);//接收端口号
//建立发送类
DatagramSocket send = new DatagramSocket(); //发送数据
List<String> addressList=new ArrayList<>();//存放所有发送数据用户的ip
while (true) {
//定义一个数据包接受数据
byte[] bs = new byte[1024];
DatagramPacket dp = new DatagramPacket(bs, bs.length);
//接受数据
rec.receive(dp);
InetAddress address = dp.getAddress();
String s = address.toString();
String ip = s.substring(s.lastIndexOf("/") + 1);//ip
//判断ip是否已存在List中
int index=1;
for (String s1 : addressList) {
if(s1.equals(ip)){
index=-1;
}
}
if(index>0){
addressList.add(ip);
}
//取出数据
//dp.getData()获取发送数据的字节数组,dp.getLength()获取发送内容的长度
String info = new String(dp.getData(), 0, dp.getLength());
System.out.println("客户端发送来的信息:" + info);
//--------------------服务器转发--------------------------//
String content=info.substring(info.lastIndexOf(":")+1);
if (content.equals("886")) {
String name=info.substring(0,info.lastIndexOf(":"));
info=name+":退出了聊天室";
}
//建立数据包 并转发信息
byte[] bs1 = info.getBytes();
for (String s1 : addressList) {
for (int i=6001;i<=6005;i++){
DatagramPacket dp1 = new DatagramPacket(bs1, bs1.length, InetAddress.getByName(s1), i);
send.send(dp1);
}
}
}
}
}
客户端测试:
public class Test1 {
public static void main(String[] args) throws SocketException {
Send s=new Send(new DatagramSocket(),6262,"127.0.0.1");
Rec rec=new Rec(new DatagramSocket(6001));
new Thread(s,"宸").start();
new Thread(rec,"宸").start();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。