当前位置:网站首页>UDP多线程在线聊天
UDP多线程在线聊天
2022-07-26 14:03:00 【看小虫子】
TalkSend
package UdpCommuncation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable {
DatagramSocket socket=null;
BufferedReader reader=null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket=new DatagramSocket(fromPort);
reader=new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
String data= null;
try {
data = reader.readLine();
byte[] datas=data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(data.equals("bye")){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
TalkReceive
package UdpCommuncation;
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 port;
private String msgFrom;
public TalkReceive( int port,String msgFrom) {
this.port = port;
this.msgFrom=msgFrom;
try {
socket=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
try {
//准备接收包裹
byte[] container =new byte[1024];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
socket.receive(packet);
//断开连接,bye
byte[] data=packet.getData();
String receiveData=new String(data,0,data.length);
System.out.println(msgFrom+":"+receiveData);
if(receiveData.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TalkStudent
package UdpCommuncation;
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
TalkTeacher
package UdpCommuncation;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
边栏推荐
- 最新战报:十项认证,五项最佳实践
- Frisbee, 2022 "black red" top stream
- The picture moves horizontally with the phone - gyroscope. 360 degree setting conditions
- Pytoch learning notes (I) installation and use of common functions
- Intercept the coordinate points (four point coordinates of the face frame) face image from the marked XML file and save it in the specified folder
- Difference between base addressing and index addressing
- 作业7.25 排序与查找
- Segmentation fault (core dumped)
- @千行百业,一起乘云而上!
- 关于存储芯片的入门基础知识
猜你喜欢

DP sword finger offer II 100. sum of minimum paths in triangle

MySql的DDL和DML和DQL的基本语法

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

Circular queue (implemented in C language)

The difference between V-model and.Sync modifier

TDSQL-C Serverless:助力初创企业实现降本增效

JS download files, filesaver.js export txt and Excel files

【论文阅读】GRAW+:A Two-View Graph Propagation Method With Word Coupling for Readability Assessment

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

Pytoch learning notes (III) use, modification, training (cpu/gpu) and verification of the model
随机推荐
[noip2003 popularity group] stack
Pytoch learning notes (II) the use of neural networks
Comparison between agile development and Devops
Multithreaded completable future usage
Technology evolution analysis framework based on two-level topic model and its application
Mobile dual finger scaling event (native), e.originalevent.touches
作业7.25 排序与查找
Pytorch学习笔记(一)安装与常用函数的使用
Pytorch学习笔记(二)神经网络的使用
[GYCTF2020]FlaskApp
Force deduction ----- the number of words in the string
MLX90640 红外热成像仪测温传感器模块开发笔记(六)
JS page turning, kkpager.js page turning
Intercept the coordinate points (four point coordinates of the face frame) face image from the marked XML file and save it in the specified folder
[oauth2] VII. Wechat oauth2 authorized login
[shaders realize overlay to re cover cross dressing effect _shader effect Chapter 9]
TDSQL-C Serverless:助力初创企业实现降本增效
基于机器学习的技术术语识别研究综述
GDB common commands
redis学习笔记