多线程和UDP--在线咨询聊天
发布日期:2022-04-11 08:53:01
浏览次数:1
分类:技术文章
本文共 3206 字,大约阅读时间需要 10 分钟。
在线咨询:两个人都可以是发送方,也都可以是接收方
一个线程发送,一个线程接收,同时开启两个线程 在线聊天
发送信息客户端:
package com.li.chat;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;//一个线程发送,一个线程接收,同时开启两个线程 在线聊天public class TalkSend implements Runnable { //发送到另一个服务端口,需要IP和端口号 //接收服务端发的信息,需要自身的端口号 private String toIP;//服务端的IP private int toPort;//服务端的端口 private int sendPort;//自身发送端口 DatagramSocket socket = null; public TalkSend(String toIP, int toPort, int sendPort) throws Exception { this.toIP = toIP; this.toPort = toPort; this.sendPort = sendPort; socket = new DatagramSocket(sendPort); } @Override public void run() { //循环发送信息 while (true) { //建立连接 try { //读取数据,建包 BufferedReader bd = new BufferedReader(new InputStreamReader(System.in)); String data = bd.readLine(); DatagramPacket packet = new DatagramPacket(data.getBytes(), 0, data.getBytes().length, new InetSocketAddress(toIP, toPort)); //发包 socket.send(packet); if (data.equals("bye")) { break; } } catch (Exception e) { e.printStackTrace(); } } }}
接收信息服务端:
package com.li.chat;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class TalkReceive implements Runnable{ DatagramSocket socket = null; //接收客户端,开放端口 private int receivePort;//自身接收端口 private String msgForm;//信息来源 public TalkReceive(int receivePort, String msgForm) { this.receivePort = receivePort; this.msgForm = msgForm; } @Override public void run() { //开放端口 try { socket = new DatagramSocket(receivePort); } catch (SocketException e) { e.printStackTrace(); } //准备接收包 while (true) { byte[] container = new byte[1024]; DatagramPacket packet = new DatagramPacket(container,0,container.length); //阻塞式接收包 try { socket.receive(packet); } catch (IOException e) { e.printStackTrace(); } byte[] data = packet.getData(); String receiveData = new String(data, 0, packet.getLength()); System.out.println(msgForm + ":" + receiveData); } }}
老师线程开启:
package com.li.chat;public class TalkTeacher { public static void main(String[] args) throws Exception { //老师6666端口发送到学生8888端扣,同时9999接收学生的,(老师发送时的端口可以不确定,但接收的端口要确定) new Thread(new TalkSend("localhost",8888,6666)).start(); new Thread(new TalkReceive(9999,"学生")).start(); }}
学生线程开启:
package com.li.chat;public class TalkStudent { public static void main(String[] args) throws Exception { //学生7777端口发送到老师9999端口,同时8888接收老师的信息(老师发送时的端口可以不确定,但接收的端口要确定) new Thread(new TalkReceive(8888,"老师")).start(); new Thread(new TalkSend("localhost",9999,7777)).start(); }}
转载地址:https://blog.csdn.net/l1341886243/article/details/118460146 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月22日 21时24分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
linux内存的寻址方式
2019-04-30
how2heap-double free
2019-04-30
how2heap-fastbin_dup_consolidate
2019-04-30
tf keras SimpleRNN源码解析
2019-04-30
MyBatisPlus简单入门(SpringBoot)
2019-04-30
攻防世界web进阶区web2详解
2019-04-30
xss-labs详解(上)1-10
2019-04-30
xss-labs详解(下)11-20
2019-04-30
攻防世界web进阶区ics-04详解
2019-04-30
sql注入总结学习
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
Ubuntu更新后终端中字体的颜色全是白色
2019-04-30
vscode git
2019-04-30
基于MATLAB的二进制数字调制与解调信号的仿真——2PSK
2019-04-30
基于MATLAB的模拟调制信号与解调的仿真——DSB
2019-04-30
HDU - 1166 敌兵布阵 (树状数组模板题/线段树模板题)
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 675A Infinite Sequence(简单数论 细节)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30