一、任务描述:
二、运行结果:
三、实现思路:
1、聊天室类
在同一个窗口既发送又接收,创建两个线程
package cn.itcast.task01;
import java.util.Scanner;
/**
* @author wangyue
* @version 1.0
* @date 2019/7/10 10:21
* @describe: 聊天室类
*/
public class ChatRoom {
public static void main(String[] args){
System.out.println("欢迎您进入聊天室");
Scanner scanner=new Scanner(System.in);
System.out.print("请输入本程序发送端端口:");
int sendPort=scanner.nextInt();
System.out.print("请输入本程序接收端端口:");
int receivePort=scanner.nextInt();
System.out.println("聊天程序启动");
new Thread(new SendTask(sendPort), "发送端").start();
new Thread(new ReceiveTask(receivePort), "接收端").start();
}
}
2、发送数据的任务类:
重写构造方法,指定发送端口
private final int sendPort; //需要 final 吗?
public SendTask(int sendPort){
System.out.println("构造了SendTask对象 ");
this.sendPort=sendPort;
}
run()方法里发送数据
发送数据一定是多次,用while死循环
广播地址 127.0.0.255 (127.0.0.1 也可以,不知道为什么用广播?)
3、接收数据的任务类
创建接收的DatagramSocket对象,必须指定port
DatagramSocket ds=new DatagramSocket(receivePort);
四、实现代码:
程序运行两次,开两个console窗口,
每个窗口里都存在send,receive线程,一旦键盘输入,就会执行send线程,然后继续等待
一旦接收到数据,就会继续receive线程,然后继续等待
package cn.itcast.task01;
import java.util.Scanner;
/**
* @author wangyue
* @version 1.0
* @date 2019/7/10 10:21
* @describe: 聊天室类
*/
public class ChatRoom {
public static void main(String[] args) {
System.out.println("欢迎您进入聊天室");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入本程序发送端端口:");
int sendPort = scanner.nextInt();
System.out.print("请输入本程序接收端端口:");
int receivePort = scanner.nextInt();
System.out.println("聊天程序启动");
new Thread(new SendTask(sendPort), "发送端").start();
new Thread(new ReceiveTask(receivePort), "接收端").start();
}
}
package cn.itcast.task01;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
* @author wangyue
* @version 1.0
* @date 2019/7/10 10:58
* @describe: 发送任务类
*/
public class SendTask implements Runnable {
private final int sendPort; //需要 final 吗?
public SendTask(int sendPort) {
System.out.println("构造了SendTask对象 ");
this.sendPort = sendPort;
}
@Override
public void run() {
try {
//1、创建DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println(Thread.currentThread().getName() + "进入了 SendTask 的run");
//2、准备待发送的数据
String message = scanner.nextLine();
//3、创建DatagramPacket
byte[] buf = message.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), sendPort);
//4、发送
ds.send(dp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cn.itcast.task01;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @author wangyue
* @version 1.0
* @date 2019/7/10 11:01
* @describe: 接收数据的任务类
*/
public class ReceiveTask implements Runnable {
private final int receivePort; //需要 final 吗?
public ReceiveTask(int receivePort) {
System.out.println("构造了ReceiveTask对象 ");
this.receivePort = receivePort;
}
@Override
public void run() {
try {
//1、创建DatagramSocket对象 (创建接收的socket时,必须制定port)
DatagramSocket ds = new DatagramSocket(receivePort);
//2、创建DatagramPacket对象
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
while (true) {
System.out.println(Thread.currentThread().getName() + "进入了 receiveTask 的run");
//3、接收数据
ds.receive(dp);
//4、显示收到的数据
String message = new String(dp.getData(), 0, dp.getLength());
System.out.println("收到来自 " + dp.getAddress().getHostAddress() + " 的数据-----" + message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}