举报投诉联系我们 手机版 热门标签 VUE中文网
您的位置:VUE中文网 > java udp服务器 Java 网络UDP服务器

java udp服务器 Java 网络UDP服务器

2023-03-25 03:17 Java教程

java udp服务器 Java 网络UDP服务器

java udp服务器

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);   // 处理数据函数  

Java 网络UDP服务器

Java网络教程 - Java网络UDP服务器


以下代码显示了如何编写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);
}

Echo 服务器

基于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 + "]");
  }
}

上面的代码生成以下结果。

Echo 客户端

基于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;
  }
}

上面的代码生成以下结果。

阅读全文
以上是VUE中文网为你收集整理的java udp服务器 Java 网络UDP服务器全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 VUE中文网 vue88.com 版权所有 联系我们