java网络原理《狂神说》实现UDP双方通信(含代码)
Posted 轻狂书生han
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java网络原理《狂神说》实现UDP双方通信(含代码)相关的知识,希望对你有一定的参考价值。
UDP
发信息:不用链接,需要知道对方的地址!
发送消息
package comip.study.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//不需要连接服务器
public class UdpClientDemo01
public static void main(String[] args) throws Exception
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建个包
String msg = "你好啊,服务器";
//发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的长度,要发送给谁
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发个包
socket.send(packet);
//关闭流
socket.close();
接收端
package comip.study.lesson03;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//还是要等待客户端的连接!
public class UdpServerDemo01
public static void main(String[] args) throws Exception
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);//接收
socket.receive(packet);//阻塞接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭连接
socket.close();
咨询
发送端
package comip.study.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSenderDemo01
public static void main(String[] args) throws Exception
DatagramSocket socket = new DatagramSocket(8888);
//准备数据,控制台读取 System.in
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("localhost",6666));
socket.send(packet);
if (data.equals("bye"))
break;
socket.close();
接收端
package comip.study.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01
public static void main(String[] args) throws Exception
DatagramSocket socket = new DatagramSocket(6666);
while (true)
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接受包裹
//断开连接 bye
byte[] data = packet.getData();
String receiveDatas = new String(data,0,data.length);
System.out.println(receiveDatas);
if (receiveDatas.equals("bye"))
break;
socket.close();
修改为以下的代码,让他一直可以发送
while (true)
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (data.equals("bye"))
break;
在线咨询:两人都可以是发送方,也可以都是接收方
发送方
package comip.study.chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromIP, 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()
//准备数据,控制台读取 System.in
while (true)
try
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("bye"))
break;
catch (IOException e)
e.printStackTrace();
socket.close();
接收方
package comip.study.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 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)
//准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
try
socket.receive(packet);//阻塞式接受包裹
//断开连接 bye
byte[] data = packet.getData();
String receiveDatas = new String(data,0,data.length);
System.out.println(msgFrom+receiveDatas);
if (receiveDatas.equals("bye"))
break;
catch (IOException e)
e.printStackTrace();
socket.close();
线程一:学生
package comip.study.chat;
public class TalkStudent
public static void main(String[] args)
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师:")).start();
线程二:老师
package comip.study.chat;
public class TalkTeacher
public static void main(String[] args)
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生:")).start();
注意端口问题!
使用黑框口实现通信:
打开文件地址
打开cmd定位class文件路径
直接不能运行,需要退到包名
返回到com
没有出现报错就表示启动起来了
启动老师端
实现通信
这里由于编码格式不同,黑窗口出现乱码情况,但是我们基本简单实现了UDP的通信。
以上是关于java网络原理《狂神说》实现UDP双方通信(含代码)的主要内容,如果未能解决你的问题,请参考以下文章