网络编程
Java模拟UDP聊天
第一阶段
发送端and接收端
发送端
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据,控制台读取
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//循环发送
while (true){
//读取控制台的一行数据
String data = reader.readLine();
//必须转化成流
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("127.0.0.1",9999));
//发送消息
socket.send(packet);
if (data.equals("拜拜")){
break;
}
}
socket.close();
}
}
接收端
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
//设置端口号
DatagramSocket socket = new DatagramSocket(9999);
while (true){
//准备接收包裹
byte[] bytes = new byte[1024];
//设置包
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
//阻塞式接收数据包
socket.receive(packet);
//断开链接判断
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
System.out.println(s.trim());
if (s.equals("拜拜")){
break;
}
}
socket.close();
}
}
进阶
互为发送端和接收端,这里用到了多线程哦~
接收线程
TalkReceive
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[] bytes = new byte[4056];
//设置包
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
//阻塞式接收数据包
socket.receive(packet);
//断开链接判断
byte[] data = packet.getData();
String s = new String(data, 0, data.length);
System.out.println(msgFrom + "说:" + s.trim());
if (s.trim().equals("拜拜")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
发送线程TalkSend
public class TalkSend implements Runnable{
DatagramSocket socket =null;
BufferedReader reader =null;
private int toPort;
private String toIp;
private int fromPort;
public TalkSend(int fromPort,String toIp,int toPort) {
this.toIp = toIp;
this.fromPort = fromPort;
this.toPort = toPort;
try {
socket=new DatagramSocket(fromPort);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
//循环发送
while (true){
//读取控制台的一行数据
try {
//准备数据,控制台读取
reader = new BufferedReader(new InputStreamReader(System.in));
String 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("拜拜")){
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
测试类一
public class TalkTest01 {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"127.0.0.1",9999)).start();
new Thread(new TalkReceive(8888,"老码农")).start();
}
}
测试类二
public class TalkTest02 {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"127.0.0.1",8888)).start();
new Thread(new TalkReceive(9999,"小码农")).start();
}
}
对话框
中间遇到了空指针异常,也搞了将近两个小时吧,最后还是写出来了,心里很开心,随着学习的深入,让我发现要走的路还很远,希望每个程序员都能一直走下去!!!,Java有无限魅力!!!