⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dgsserver.java

📁 UDP服务器和客户端通信的简单例子
💻 JAVA
字号:
package inetaddressdemo;

// DGSServer.java

import java.io.*;
import java.net.*;

class DGSServer
{
   public static void main (String [] args) throws IOException  {
      System.out.println ("Server starting ...\n");
      // Create a datagram socket bound to port 10000. Datagram
      // packets sent from client programs arrive at this port.
      DatagramSocket s = new DatagramSocket (10000);
      // Create a byte array to hold data contents of datagram
      // packet.
      byte [] data = new byte [100];
      // Create a DatagramPacket object that encapsulates a reference
      // to the byte array and destination address information. The
      // DatagramPacket object is not initialized to an address
      // because it obtains that address from the client program.
      DatagramPacket dgp = new DatagramPacket (data, data.length);
      // Enter an infinite loop. Press Ctrl+C to terminate program.
      while (true){
      // Receive a datagram packet from the client program.
        s.receive (dgp);
        // Display contents of datagram packet.
        System.out.println (new String (data));
        // Echo datagram packet back to client program.
        s.send (dgp);
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -