日期:2014-05-20  浏览次数:20868 次

Java DatagramSocket类在如何传送大文件?
在使用DatagramSocket类传送一个大文件(这里假设是3M的,而我设的类中的缓冲区只有1M),这样客户端接收到的将只有1M的东西,请问有什么办法处理这类事情?

Server类:
Java code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class Server {
    
    private static final int CLIENT_PORT_NUM = 7000;
    private static final int SERVER_PORT_NUM = 6000;
    private static final int MAX_URL_LENGTH = 200;
    private static final int LENGTH_PER_TRANSMIT = 1024 * 10;
    
    private DatagramSocket server;
    private DatagramSocket urlReceiver;
    private String url;
    
    public Server() throws SocketException {
        server = new DatagramSocket();
        urlReceiver = new DatagramSocket( SERVER_PORT_NUM );
    }
    
    private void receiveURL() {
        byte[] buff = new byte[ MAX_URL_LENGTH ];
        DatagramPacket packet = new DatagramPacket( buff, buff.length );
        try {
            urlReceiver.receive( packet );
        } catch ( IOException e ) {
            e.printStackTrace();
        }
        url = new String( packet.getData() );
    }
    
    public void sendData() {
        receiveURL();
        File outputFile = new File( url );
        try {
            FileInputStream fis = new FileInputStream( outputFile );
            
            byte[] buff = new byte[ LENGTH_PER_TRANSMIT ];
            
            int len = 0;
            while ( ( len = fis.read( buff ) ) != -1 ) { 
                DatagramPacket packet = new DatagramPacket( buff, 
                                                            len, 
                                                            InetAddress.getByName( "localhost" ), 
                                                            CLIENT_PORT_NUM );
                server.send( packet );
            }
        } catch ( FileNotFoundException e1 ) {
            e1.printStackTrace();
        } catch ( IOException e ) {
            e.printStackTrace();
        } finally {
            if ( server != null ) {
                server.close();
            }
        }
    }
    
    public static void main( String[] args ) {
        try {
            Server server = new Server();
            server.sendData();
        } catch ( SocketException e ) {
            e.printStackTrace();
        }
    }
}


Client类:
Java code

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Client {
    
    private static final int CLIENT_PORT_NUM = 7000;
    private static final int SERVER_PORT_NUM = 6000;
    private static final int LENGTH_PER_TRANSMIT = 8888;
    
    private DatagramSocket client;
    private DatagramSocket urlSender;
    private String url;
    
    public Client() throws SocketException {
        client = new DatagramSocket( CLIENT_PORT_NUM );
        urlSender = new DatagramSocket();
    }
    
    public void receiveData() {
        sendURL();
        File outputFile = new File( getDestURL() );
        try {
            BufferedWriter bw = new BufferedWriter( new FileWriter( outputFile ) );
            byte[] buff = new byte[ LENGTH_PER_TRANSMIT ];

            DatagramPacket packet = new DatagramPacket( buff, buff.length );
            client.receive( packet );
            bw.append( new String( packet.getData(), 0, packet.getLength() ) );
            }
        } catch ( IOException e1 ) {
            e1.printStackTrace();
        }
    }
    
    public String getDestURL() {
        Scanner scanner = new Scanner( System.in );
        System.out.println( "请输入目标文件的存储URL" );
        return scanner.nextLine();
    }
    private void sendURL() {
        getURL();
        try {
            byte[] buff = url.getBytes();
            DatagramPacket packet = new DatagramPacket( 
                                                        buff, 
                                                        buff.length, 
                                                        InetAddress.getByName( "localhost" ) , 
                                                        SERVER_PORT_NUM );
            urlSender.send( packet );
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }
    
    private void getURL() {
        Scanner scanner = new Scanner( System.in );
        System.out.println( "请输入指定文件url:");
        url = scanner.nextLine();
    }
    
    public static void main( String[] args ) {
        try {
            Client client = new Client();
            client.receiveData();
        } catch ( SocketException e ) {
            e.printStackTrace();
        }
    }