Java UDP服务器是一种基于UDP协议的服务器,它可以接收来自客户端的数据包,并将其发送到相应的客户端。UDP是一种无连接的协议,因此不需要在发送数据之前建立连接。UDP服务器可以处理大量的数据包,并且可以在短时间内处理大量的请求。
使用Java UDP服务器需要准备一些必要的工具,如Java开发工具、UDP协议库、Socket类库等。首先,使用Java开发工具创建一个新项目,然后将UDP协议库导入到该项目中。之后,使用Socket类库来创建一个新的Socket对象,并根据所需要的端口号来连接该Socket对象。最后,使用DatagramPacket类来创建一个新的DatagramPacket对象,并根据所需要的数据包格式来封装该DatagramPacket对象。
// 创建socket对象 DatagramSocket socket = new DatagramSocket(port); // 创建datagrampacket对象 DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // 接收数据包 socket.receive(packet); // 解码数据包 String data = new String(packet.getData()); // 处理数据包 processData(data); // 处理数据函数
以下代码显示了如何编写UDP回显服务器:
DatagramSocket socket = new DatagramSocket(12345); DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); while(true) { // Receive the packet socket. receive(packet); //Send back the same packet to the sender socket. send(packet); }
基于UDP套接字的Echo服务器
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class Main { public static void main(String[] args) throws Exception { final int LOCAL_PORT = 12345; final String SERVER_NAME = "localhost"; DatagramSocket udpSocket = new DatagramSocket(LOCAL_PORT, InetAddress.getByName(SERVER_NAME)); System.out.println("Created UDP server socket at " + udpSocket.getLocalSocketAddress() + "..."); while (true) { System.out.println("Waiting for a UDP packet..."); DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); udpSocket.receive(packet); displayPacketDetails(packet); udpSocket.send(packet); } } public static void displayPacketDetails(DatagramPacket packet) { byte[] msgBuffer = packet.getData(); int length = packet.getLength(); int offset = packet.getOffset(); int remotePort = packet.getPort(); InetAddress remoteAddr = packet.getAddress(); String msg = new String(msgBuffer, offset, length); System.out.println("Received a packet:[IP Address=" + remoteAddr + ", port=" + remotePort + ", message=" + msg + "]"); } }
上面的代码生成以下结果。
基于UDP套接字的Echo客户端
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.UnknownHostException; public class Main { public static void main(String[] args) throws Exception { DatagramSocket udpSocket = new DatagramSocket(); String msg = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter a message (Bye to quit):"); while ((msg = br.readLine()) != null) { if (msg.equalsIgnoreCase("bye")) { break; } DatagramPacket packet = Main.getPacket(msg); udpSocket.send(packet); udpSocket.receive(packet); displayPacketDetails(packet); System.out.print("Please enter a message (Bye to quit):"); } udpSocket.close(); } public static void displayPacketDetails(DatagramPacket packet) { byte[] msgBuffer = packet.getData(); int length = packet.getLength(); int offset = packet.getOffset(); int remotePort = packet.getPort(); InetAddress remoteAddr = packet.getAddress(); String msg = new String(msgBuffer, offset, length); System.out.println("[Server at IP Address=" + remoteAddr + ", port=" + remotePort + "]: " + msg); } public static DatagramPacket getPacket(String msg) throws UnknownHostException { int PACKET_MAX_LENGTH = 1024; byte[] msgBuffer = msg.getBytes(); int length = msgBuffer.length; if (length > PACKET_MAX_LENGTH) { length = PACKET_MAX_LENGTH; } DatagramPacket packet = new DatagramPacket(msgBuffer, length); InetAddress serverIPAddress = InetAddress.getByName("localhost"); packet.setAddress(serverIPAddress); packet.setPort(15900); return packet; } }
上面的代码生成以下结果。
JavaFX教程 -JavaFX文本另一个基本的JavaFX节点是Text节点,它允许我们在场景图上显示测试。要创建 Text 节点,请使用 javafx.sc...
JavaFX教程 -JavaFX矩形椭圆avaFX Shape类定义了常见的形状,如线,矩形,圆,Arc,CubicCurve,Ellipse和QuadCurve。在场景图上...
JavaFX教程 -JavaFX绑定JavaFX绑定同步两个值:当因变量更改时,其他变量更改。要将属性绑定到另一个属性,请调用bind()方法,该...
JavaFX教程 -JavaFX按钮当用户单击按钮时,JavaFX Button类可以触发事件。Button类扩展标记的类,它可以显示文本,图像,或两者...
JavaFX教程 -JavaFX 文本字段TextField用于单行文本输入。import javafx.application.Application;import javafx.geometry.Inset...