• Java TCP/ UDP 传输文件


    Net

    URL

    URL url=new URL("http://www.swu.edu.cn/");
    InputStream in= url.openStream();

    URLConnection

    ``` URL url=new URL("http://www.swu.edu.cn/"); URLConnection con=URL.openConnection; InputStream in= con.getInputStream(); ```

    TCP/UDP

    TCP是面向连接的传输协议,UDP是没有建立连接的。UDP客户端只管往指定IP和端口发送数据包,UDP服务器只管接受从指定端口接受数据包,不管是不是想要的客户端发的数据包都接收,因为双方不知道对方的状态。不管对方是否在线,都只管发和收。

    客户端用DatagramPacket对象来对数据包进行写入数据,再用DatagramSocket数据报套接字发送该数据包;服务器用DatagramSocket接受网络发来的数据包,再DatagramPacket对象打开数据包,读取数据。

    由于UDP不保证数据传输的可靠性,数据量大的情况下会有丢包产生

    TCP: Socket/ServerSocket

    客户端

    Socket client = new Socket("localhost",8888) ; // 客户端连服务器
    BufferedReader buf = new BufferedReader(new InputStreamReader(client.getInputStream())) ; // 输入流,之后用于读取
    PrintStream out = new PrintStream(client.getOutputStream())  ; // 输出流,发送数据
    

    服务器

    ServerSocket ss=new ServerSocket(8888);
    
    Socket byclient = ss.accept() ; // 这个client是服务器到客户端的socket; 注意后面IO流!!!得到连接,程序进入到阻塞状态
    out = new PrintStream(byclient.getOutputStream()) ; //输出流
    BufferedReader buf = new BufferedReader(new InputStreamReader(byclient.getInputStream())) ;// 输入流 !!!注意对比!准备接收客户端的输入信息
    

    scanner | bufferedreader

    BufferedReader input = new BufferedReader(new InputStreamReader(System.in)) ; // 接收键盘数据
    Scanner input=new Scanner(System.in)
    

    UDP DatagramPacket DatagramSocket

    //只有点对点,不分sv,clt!

    DatagramSocket skt=new DatagramSocket(8000);//接收端
    DatagramPacket box=new DatagramPacket(c,1024);//接收容器
    DatagramPacket pkt=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),8000);//发送容器

    skt.send(pkt)
    skt.receive(box)

    UDP传输文件代码

    使用方法:先运行接收端,再运行发送端
    但是存在一个问题:接收端需要手动关闭,改了半天还是只能用超时来限制
    skt.setSoTimeout(5000);

    发送端

    import java.io.*;
    import java.io.InputStream;
    import java.net.*;
    class sender {//sender
        public static void main(String[] args) throws Exception {
            DatagramSocket skt=new DatagramSocket(6666);//???????reply?
            File f=new File("bank.txt");
            byte[] d=new byte[1024];
            InputStream in = new FileInputStream(f);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
            while (in.read(d)!=-1) {
                DatagramPacket pkt=new DatagramPacket(d,d.length,InetAddress.getLocalHost(),8000);
                skt.send(pkt);
    
            }
    
            System.out.println("sent");
    
            skt.close();
        }
    }
    

    接收端

    改之前
    import java.io.*;
    import java.net.*;
    class receive {//receiver
        public static void main(String[] args) throws Exception {
        DatagramSocket skt=new DatagramSocket(8000);
        byte c[]=new byte[1024];
        DatagramPacket box=new DatagramPacket(c,1024);//receive 到c[]
            File f=new File("a.txt");
            FileOutputStream fos= new FileOutputStream(f);
        int l=0;
        int x=0;
        while (l==0){
            skt.receive(box);
            l=box.getLength();
    
            if(box instanceof DatagramPacket){
                fos.write(c,0,l);
                fos.flush();
                System.out.println(l+"bytes");
                x=l;
                l=0;
            }
            else if(x>0){  //没用
                break;
            }
    
        }
        System.out.println("ok");
        }
    
    }
    
    改后
    import java.io.*;
    import java.util.Scanner;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.*;
    class receive {//receiver
        public static void main(String[] args) throws SocketException, FileNotFoundException {
        DatagramSocket skt=new DatagramSocket(8000);
        byte c[]=new byte[1024];
        DatagramPacket box=new DatagramPacket(c,1024);//receive 到c[]
        skt.setSoTimeout(5000);//!!!!!!!!!!!!!
        int l=0;
            try{
                File f=new File("a.txt");
                FileOutputStream fos= new FileOutputStream(f);
                while (true) {
                    skt.receive(box);
            //        System.out.println(box);
                    fos.write(c);
                    fos.flush();
                    l++;
                }
    
            } catch (IOException e) {
                skt.close();
                e.printStackTrace();
                System.out.println(l + "kb");
    
            }
    
        }
    
    }
    
    
  • 相关阅读:
    java8新特性学习:stream与lambda
    Storm实践(一):基础知识
    Google Protocol Buffer入门
    zeromq实践
    maven实践--你所需要了解的maven
    springcloud实践(一)服务发现:Eureka
    职责链模式(chain of responsibility Pattern)
    代理模式 (Proxy Pattern)
    享元模式(Flyweight Pattern)
    外观模式(Facade Pattern)
  • 原文地址:https://www.cnblogs.com/impw/p/15441455.html
Copyright © 2020-2023  润新知