1.新建一个类"xiaolin",模拟一个账号为"xiaolin"的用户,代码如下: public class xiaolin implements Runnable{ InetAddress inetAddress; DatagramSocket datagramSocket; String sentMessage=""; //发送消息 String receiveMessage=""; //接收消息 public xiaolin(InetAddress inetAddress, DatagramSocket datagramSocket) { this.inetAddress = inetAddress; this.datagramSocket = datagramSocket; }
@Override
public void run() { //开启线程
/*
当发送和接收到 “byb"时关闭,注意这里判断符号用||,但实际上要同时满足发送消息和接收消息为”byb“是才退出程序
因为run函数中的while(true)阻塞,当这两者都满足是才退出
* */
if (receiveMessage.equals("byb") || sentMessage.equals("byb")) { //
datagramSocket.close();
}
else {
if (Thread.currentThread().getName().equals("发送")) { //发送消息线程执行的代码
Scanner scanner = new Scanner(System.in);
while (true) {
if(receiveMessage.equals("byb")) //当接收消息为”byb“退出
break;
if (scanner.hasNext()) {
sentMessage = scanner.nextLine();
}
//发送消息数据包
DatagramPacket datagramSent = new DatagramPacket(sentMessage.getBytes(), 0, sentMessage.getBytes().length, inetAddress, 8484);
try {
datagramSocket.send(datagramSent); //发送数据
} catch (IOException e) {
e.printStackTrace();
}
//接收
if(sentMessage.equals("byb")) //当发送消息为”byb“退出
break;
}
} else if (Thread.currentThread().getName().equals("接收")) { //接收消息执行代码
byte[] buffer = new byte[1024];
//接收消息数据包
DatagramPacket datagramReceive = new DatagramPacket(buffer, 0, buffer.length);
while (true) {
if (sentMessage.equals("byb"))
break;
//接收
try {
datagramSocket.receive(datagramReceive);
} catch (IOException e) {
e.printStackTrace();
}
int len = datagramReceive.getLength();
receiveMessage = new String(datagramReceive.getData(), 0, len);
System.out.println(receiveMessage);
if (receiveMessage.equals("byb"))
break;
}
}
}
}
public static void main(String[] args) throws UnknownHostException, SocketException {
//获取本机地址和开启端口
InetAddress inetAddress=InetAddress.getLocalHost();
DatagramSocket datagramSocket=new DatagramSocket(9090);
xiaolin xl=new xiaolin(inetAddress,datagramSocket);
//开启线程
Thread sentT=new Thread(xl,"发送");
Thread receiveT=new Thread(xl,"接收");
sentT.start();
receiveT.start();
}
} 2.然后新建一个类"xiaop",模拟一个账号为"xiaop"的用户,来与"xiaolin"聊天,代码如下: public class xiaop implements Runnable{ DatagramSocket datagramSocket; InetAddress inetAddress; String receiveMessage=""; String sentMessage="";
public xiaop(DatagramSocket datagramSocket, InetAddress inetAddress) {
this.datagramSocket = datagramSocket;
this.inetAddress = inetAddress;
}
@Override
public void run() {
if (receiveMessage.equals("byb") || sentMessage.equals("byb")) {
datagramSocket.close();
}
else {
if(Thread.currentThread().getName().equals("发送")) {
Scanner scanner = new Scanner(System.in);
while (true) {
if (receiveMessage.equals("byb"))
break;
if (scanner.hasNext()) {
sentMessage = scanner.nextLine();
}
DatagramPacket datagramSent = new DatagramPacket(sentMessage.getBytes(), 0, sentMessage.getBytes().length, inetAddress, 9090);
try {
datagramSocket.send(datagramSent);
} catch (IOException e) {
e.printStackTrace();
}
//接收
if (sentMessage.equals("byb"))
break;
}
}
else if (Thread.currentThread().getName().equals("接收")) {
byte[] buffer = new byte[1024];
DatagramPacket datagramReceive = new DatagramPacket(buffer, 0, buffer.length);
while (true) {
if (sentMessage.equals("byb"))
break;
//接收
try {
datagramSocket.receive(datagramReceive);
} catch (IOException e) {
e.printStackTrace();
}
int len = datagramReceive.getLength();
receiveMessage = new String(datagramReceive.getData(), 0, len);
System.out.println(receiveMessage);
if (receiveMessage.equals("byb"))
break;
}
}
}
}
public static void main(String[] args) throws UnknownHostException, SocketException {
InetAddress inetAddress=InetAddress.getLocalHost();
DatagramSocket datagramSocket=new DatagramSocket(8484);
xiaop xl=new xiaop(datagramSocket,inetAddress);
Thread sentT=new Thread(xl,"发送");
Thread receiveT=new Thread(xl,"接收");
sentT.start();
receiveT.start();
}
} 3.运行结果如下:
|