Internet TCPとUDPプロトコル

5477 ワード

一.概念
    :
       :HTTP/WebSocket(Html)、DNS、FTP、SMTP/POP3/IMAP、XMPP/MQTT、TELNET/SSH
       :TCP/UDP(Socket)、DCCP、RTP、PPTP 
       :IP、ICMP(ping)、IGMP、RIP      
       : IP                  
       :   bit       ,      ,        

     MTU(      ):
    FDDI  :4352  
       Ethernet  :1500  
    PPPoE(ADSL)  :1492  
    X.25  (Dial Up/Modem):576  
    Point-to-Point:4470  
二.違い
        MTU, IP     ,IP            !    
TCP            ,    576    ,  IP    !   

     (     ),UDP        1472    ! 
     ,         ,  MTU 576  ,UDP        576  !   

TCP  :       ,        ,    ,        ;
                   
           ,       
        java  : Socket/ServerSocket

UDP  :       ,      ,        ;
               ,        
           ,             
        java  : DatagramSocket/DatagramPacket

 HTTP  :
        HTTP  TCP      ,         ,
              :         ,         (  )  ,
                       ,               !            
                  ,         ,         ,     !
          HTTP1.1     (    TCP  ),          ,    !
            HTTP          TCP  ,              !
                   :       ,   ,           !
          ,Html5   WebSocket     Http        !
                                
        TCP     ,     ,      ip  ,         (        )               
三.javaはTCPプロトコルを使用する

// TCP   
public class TCPClient{
    public static void main(String args[]) throws Exception{
        // 1.     IP   ,      
        String ip = args[0];
        int port = Integer.parseInt(args[1]);
        String filePath = args[2];
        Socket socket = new Socket(ip, port);
        // 2.  TCP  ,      
        new DataOutputStream(socket.getOutputStream()).writeUTF(filePath);
        // 3.     ,  ,  
        DataInputStream sin = new DataInputStream(socket.getInputStream()); 
        FileOutputStream fou = new FileOutputStream(System.currentTimeMillis()+"_"+sin.readUTF());      
        long len = sin.readLong();
        int pro=0;  
        int l; 
        byte[] b = new byte[1024*1024];
        while ((l = sin.read(b)) != -1) {
            fou.write(b,0,l);
            pro += l;
            System.out.println("    "+String.format("%.2f",pro/1024f/1024)+"MB, "+
                                "  "+String.format("%.2f",pro*100f/len)+"%"); 
        }       
        sin.close();
        fou.close();
        socket.close();
    }
}

// TCP   
public class TCPServer{
    public static void main(String arg[]) throws Exception{
        // 1.         
        String port = arg[0];
        ServerSocket serverSocket = new ServerSocket(Integer.parseInt(port)); 
        while (true) {
            // 2.       ,       
            System.out.println("    "+port+"  。。。
"); Socket socket = serverSocket.accept(); // 3.TCP , String filePath = new DataInputStream(socket.getInputStream()).readUTF(); File file = new File(filePath); FileInputStream fin = new FileInputStream(file); System.out.println("TCP , "+filePath); // 4. DataOutputStream sou = new DataOutputStream(socket.getOutputStream()); sou.writeUTF(file.getName()); sou.writeLong(file.length()); byte[] b = new byte[1024*1024]; int l; while ((l = fin.read(b)) != -1) { sou.write(b, 0, l); System.out.println(" 。。。"); } System.out.println("
"); fin.close(); sou.close(); socket.close(); } } }
四.javaはUDPプロトコルを使う

// UDP   
public class UDPClient{
    public static void main(String[] args)throws IOException{
        // 1.     (     )
        DatagramSocket dsocket = new DatagramSocket();
        byte[] sen = "  !  UDP   ".getBytes("utf-8");
        dsocket.send(new DatagramPacket(sen,sen.length,InetAddress.getByName("localhost"),9999));
        System.out.println("     ");
        // 2.    
        DatagramPacket rec = new DatagramPacket(new byte[100],100);
        dsocket.receive(rec);   
        dsocket.close();
        System.out.println("     :" + new String(rec.getData(),0,rec.getLength()));
    }
}

// UDP   
public class UDPServer{
    public static void main(String[] args)throws IOException{           
        DatagramSocket dsocket = new DatagramSocket(9999);
        DatagramPacket rec = new DatagramPacket(new byte[100], 100);            
        while (true) {
            // 1.    
            System.out.println("    ...");
            dsocket.receive(rec);   //     ,                        
            // 2.    
            System.out.println("     :"+new String(rec.getData(), 0, rec.getLength()));
            // 3.    
            byte[] sec = "  !     ".getBytes("utf-8");
            dsocket.send(new DatagramPacket(sec, sec.length, rec.getAddress(), rec.getPort()));
            System.out.println("     !");
        }
}

:http://www.jianshu.com/p/83330e36b4de CSDNブログ:http://blog.csdn.net/qq_32115349/articale/details/55798783 GitHubブログ:http://lioil.win/2017/02/19/TCP-UDP.html Codingブログ:http://c.lioil.win/2017/02/19/TCP-UDP.html