zoukankan      html  css  js  c++  java
  • java===UDP

    import java.net.DatagramSocket;
    import java.net.*;
    import java.io.*;
    public class Send implements Runnable{
         private DatagramSocket ds;
         public Send(DatagramSocket ds){
               this.ds=ds;
         }
       public void run(){
           try{ 
                       BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
                     String text = null;
                     while((text=bfr.readLine())!=null){
                             byte[]buf = text.getBytes();
                             DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.255"),10001);
                             ds.send(dp);
                             if("88".equals(text)){
                                      break;
                             }
                            // ds.close();
                     }
               } catch(Exception e){
                   
               }finally{
                    ds.close();
               }
       }
    }
    import java.net.*;
    import java.io.*;
    public class Rece implements Runnable{
            private DatagramSocket ds;
            public Rece(DatagramSocket ds){
                    this.ds=ds;
            }
          public void run(){
                while(true){
                     try{
                             byte[]buf = new byte[1024];
                          DatagramPacket dp = new DatagramPacket(buf,buf.length);
                          ds.receive(dp);
                          String ip = dp.getAddress().getHostAddress();
                          String text = new String(dp.getData(),0,dp.getLength());
                          System.out.println(ip+"
    "+text);
                          if(text.equals("88")){
                               System.out.println(ip+"::退出聊天室");
                          }
                     }catch(Exception e){}
                }
          }
    }
    import java.io.IOException;
    import java.net.DatagramSocket;
    import java.net.SocketException;
    public class Exe{
          public static void main(String[]args)throws IOException{
                  DatagramSocket send = new DatagramSocket();
                  DatagramSocket rece = new DatagramSocket(10001);
                Send s = new Send(send);
                Rece r = new Rece(rece);
                new Thread(s).start();
                new Thread(r).start();
          
          }
    }

     UDP是比较常见的传输协议,通过网络流DatagramSocket完成传输,Datagrampacket作为数据包,以上面的聊天软件代码为例;虽然UDP传输安全性不高,但是速度快,适合于聊天软件,视频软件等开发使用;相对于TCP来说UDP不需要建立连接,提高了效率;最近比较忙于敲代码,所以博客更新较慢,后续会给大家带来TCP传输协议的使用;

  • 相关阅读:
    bzoj 4012: [HNOI2015]开店
    POJ 1054 The Troublesome Frog
    POJ 3171 Cleaning Shifts
    POJ 3411 Paid Roads
    POJ 3045 Cow Acrobats
    POJ 1742 Coins
    POJ 3181 Dollar Dayz
    POJ 3040 Allowance
    POJ 3666 Making the Grade
    洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6911668.html
Copyright © 2011-2022 走看看