- 2023-10-14 13:13:30
- 203 热度
- 0 评论
UDP的理论不再多说,我这里直接给出一个关于UDP的HelloWorld程序,代码明了,希望对刚入门的学生有所帮助!
当然,实际上,在这块我也刚入门!
首先写服务端代码,服务端邦定本地的IP和端口来监听访问:
package udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; /** * UDP服务类 */ public class UdpServerSocket { private byte[] buffer = new byte[1024]; private static DatagramSocket ds = null; private DatagramPacket packet = null; private InetSocketAddress socketAddress = null; /** * 测试方法 */ public static void main(String[] args) throws Exception { String serverHost = "127.0.0.1"; int serverPort = 3344; UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost, serverPort); while (true) { udpServerSocket.receive(); udpServerSocket.response("你好,吃了吗!"); } } /** * 构造函数,绑定主机和端口 */ public UdpServerSocket(String host, int port) throws Exception { socketAddress = new InetSocketAddress(host, port); ds = new DatagramSocket(socketAddress); System.out.println("服务端启动!"); } /** * 接收数据包,该方法会造成线程阻塞 */ public final String receive() throws IOException { packet = new DatagramPacket(buffer, buffer.length); ds.receive(packet); String info = new String(packet.getData(), 0, packet.getLength()); System.out.println("接收信息:" + info); return info; } /** * 将响应包发送给请求端 */ public final void response(String info) throws IOException { System.out.println("客户端地址 : " + packet.getAddress().getHostAddress() + ",端口:" + packet.getPort()); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet .getAddress(), packet.getPort()); dp.setData(info.getBytes()); ds.send(dp); } }
运行后提示服务端运行成功,程序开始监听端口,接收方法堵塞,当有访问时才会向下进行!
我们写客户端进行访问,看到网上的例子都是直接创建了 DatagramSocket 对象,而其实自己都不知道自己使用的端口是那个,这里我创建时会指定自己邦定的端口,其实很简单,就是初始化该对象时传递一个端口参数。
这里你访问客户端时客户端会打印你的IP和端口!
看一看客户端代码:
package udp; import java.io.*; import java.net.*; /** * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息 */ public class UdpClientSocket { private byte[] buffer = new byte[1024]; private static DatagramSocket ds = null; /** * 测试客户端发包和接收回应信息的方法 */ public static void main(String[] args) throws Exception { UdpClientSocket client = new UdpClientSocket(); String serverHost = "127.0.0.1"; int serverPort = 3344; client.send(serverHost, serverPort, ("你好,亲爱的!").getBytes()); byte[] bt = client.receive(); System.out.println("服务端回应数据:" + new String(bt)); // 关闭连接 try { ds.close(); } catch (Exception ex) { ex.printStackTrace(); } } /** * 构造函数,创建UDP客户端 */ public UdpClientSocket() throws Exception { ds = new DatagramSocket(8899); // 邦定本地端口作为客户端 } /** * 向指定的服务端发送数据信息 */ public final void send(final String host, final int port, final byte[] bytes) throws IOException { DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port); ds.send(dp); } /** * 接收从指定的服务端发回的数据 */ public final byte[] receive() throws Exception { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ds.receive(dp); byte[] data = new byte[dp.getLength()]; System.arraycopy(dp.getData(), 0, data, 0, dp.getLength()); return data; } }
直接运行程序看效果!
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)