集册 Java实例教程 创建UDP服务器

创建UDP服务器

欢马劈雪     最近更新时间:2020-01-02 10:19:05

451
创建UDP服务器

import java.io.*;

import java.net.*;


class UDPServer {
/** from 
n o w j a v a . c o m**/

   public static void main(String args[]) throws IOException {


      DatagramSocket serverSocket = new DatagramSocket(9876);

      byte[] receiveData = new byte[1024];

      byte[] sendData = new byte[1024];

      

      while(true) {

         DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

         serverSocket.receive(receivePacket);

         String sentence = new String(receivePacket.getData());

         System.out.println("RECEIVED: " + sentence);

         InetAddress IPAddress = receivePacket.getAddress();
         /**来自 
          时   代     Java  公  众  号 - nowjava.com**/

         int port = receivePacket.getPort();

         String capitalizedSentence = sentence.toUpperCase();

        
展开阅读全文