UDP_TalkReceive键盘聊天

package Day32_Net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @Author quzheng
 * @Date 2019/10/6 17:40
 * @Version 1.0
 */
public class UDP_TalkReceive {
    public static void main(String[] args) throws IOException {
         DatagramSocket ds = new DatagramSocket(6000);

         byte [] data = new byte[1024];

         while (true){
             DatagramPacket dp = new DatagramPacket(data,data.length);

             ds.receive(dp);

             String ip = dp.getAddress().getHostAddress();

             int port = dp.getPort();

             int length = dp.getLength();
             System.out.println(new String(data,0,length)+"(来自"+ip+":"+port+"的消息");
         }


    }
}


你可能感兴趣的:(UDP_TalkReceive键盘聊天)