Java网络编程学习——简单模拟在线聊天
学了java网络,也是该做个小案例来巩固一下了。
本次案例将使用UDP和多线程模拟即时聊天,简单练练手。
1、前提知识
需要知道简单的IO流操作,以及简单的UDP发送数据包的原理。
需要用到的类:DatagramSocket、DatagramPacket
UDP数据包基于DatagramSocket发送和接收,DatagramPacket用于封装数据包
看下案例:
客户端发送消息:
正常情况下从控制台读信息,封装到DatagramPacket之中,再由DatagramSocket的send方法发出
读取到bye的时候退出聊天
public class UdpOnlineClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(10000);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String data = bufferedReader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 9999));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
socket.close();
}
}
服务端接收消息:
DatagramSocket的receive方法接收客户端发送的消息,打印到控制台,如果信息是bye则退出聊天
public class UdpOnlineServer {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9999);
while (true){
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
String msg = new String(packet.getData(), 0, packet.getLength());
System.out.println(msg);
if(msg.equals("bye")){
break;
}
}
socket.close();
}
}
试验:正常开启客户端和服务端后:
客户端发送消息:
服务端接收到信息即成功:
有了这个思路,我们只需要把发送和接收封装成发送类和接收类,使用多线程即可实现聊天了
2、实现思路:
创建UDP发送实现类TaskSend
、UDP接收实现类TaskReceive
,以及模拟多线程通信的通信类ChatThread1
、ChatThread2
UDP发送实现类TaskSend:
public class TaskSend implements Runnable{
private String sendIP;
//自己的服务端口
private int ownPort;
private int sendPort;
//读取控制台输入流
private BufferedReader bufferedReader;
private DatagramSocket socket;
public TaskSend(String sendIP, int ownPort, int sendPort) {
this.sendIP = sendIP;
this.sendPort = sendPort;
try {
socket = new DatagramSocket(ownPort);
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
} finally {
}
}
//发送消息
public void run() {
while (true) {
try {
String data = bufferedReader.readLine();
//打印自己的消息
System.out.println("我:"+data);
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress(sendIP,sendPort));
socket.send(packet);
if (data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
socket.close();
}
}
UDP接收实现类TaskReceive:
public class TaskReceive implements Runnable{
private int ownPort;
//发送信息的人
private String msgOwner;
private DatagramSocket socket;
public TaskReceive(int ownPort, String msgOwner) {
this.msgOwner = msgOwner;
try {
socket = new DatagramSocket(ownPort);
} catch (SocketException e) {
e.printStackTrace();
} finally {
}
}
//接收消息
public void run() {
while (true) {
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
String msg = new String(packet.getData(), 0, packet.getLength());
System.out.println(msgOwner+":"+msg);
if (msg.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
socket.close();
}
}
之后使用两个通信类开启多线程模拟一下:
模拟老师
public class ChatThread1 {
public static void main(String[] args) {
new Thread(new TaskSend("localhost",9999,10000)).start();
new Thread(new TaskReceive(9998,"老师")).start();
}
}
模拟学生
public class ChatThread2 {
public static void main(String[] args) {
new Thread(new TaskReceive(9999,"学生")).start();
new Thread(new TaskSend("localhost",9998,10001)).start();
}
}
实例:
学生
老师
到此简单的在线聊天功能就完成了,可以巩固一下之前学习的网络知识