美文网首页
UDP_TalkReceive键盘聊天

UDP_TalkReceive键盘聊天

作者: 勇者与王者 | 来源:发表于2019-10-06 19:56 被阅读0次
    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键盘聊天

          本文链接:https://www.haomeiwen.com/subject/rdxppctx.html