关于Java中udp通信的问题,
怎么尽可能的让接收方获取更多的包呢?
我的接收方只能获取两个包,我怀疑循环那里是不是出错了。接收的包总是很小

解决方案 »

  1.   

    这是接收方
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.net.DatagramPacket;  
    import java.net.DatagramSocket;  
    import java.net.SocketException;  
      
    public class UDPFileReceiver {  
        private int port = 1220;  
        private DatagramSocket socket;  
          
        public UDPFileReceiver() throws SocketException{  
            socket = new DatagramSocket(port);  
            socket.setSoTimeout(8000);  
        }  
        public void reciveData() throws FileNotFoundException{  
            byte[] buf = new byte[8192];  
            FileOutputStream fos = new FileOutputStream( new File("641k.jpg"));  
            while(true){  
                DatagramPacket packet = new DatagramPacket(buf, 8192);  
                try{  
                    socket.receive(packet);  
                    fos.write(packet.getData(), 0, packet.getLength());  
                }catch(Exception e){  
                    try {  
                        System.out.println("发生异常传输结束");  
                        fos.flush();  
                        fos.close();  
                        socket.close();  
                    } catch (IOException e1) {  
                        // TODO Auto-generated catch block  
                        e1.printStackTrace();  
                    }  
                    break;  
                }finally{  
                
                }
            }  
        }  
        public static void main(String[] args) throws FileNotFoundException, SocketException {  
            new UDPFileReceiver().reciveData();  
        }  
      
    }  
      

  2.   

    错了,二楼的不算,从三楼开始看代码:这是接收方:import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.SocketException;public class ImageReceive { private int port = 1220;
    private DatagramSocket receiver; public ImageReceive() {
    try {
    receiver = new DatagramSocket(port);
    receiver.setSoTimeout(10000);
    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    ImageReceive imgReceive = new ImageReceive();
    imgReceive.start();
    } public void start() {
    byte[] buf = new byte[5000];
    FileOutputStream fos = null;
    try {
    fos = new FileOutputStream(new File("123.jpg"));
    } catch (FileNotFoundException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }
    while (true) {
    try {
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    receiver.receive(packet);
    System.out.println(packet.getLength());
    fos.write(packet.getData(), 0, packet.getLength());
    }catch (Exception e) {
    try {
    System.out.println("发生异常传输结束");
    fos.flush();
    fos.close();
    receiver.close();
    break;
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    }
    }
    }
      

  3.   

    这是发送方
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;public class ImageSend {


    DatagramSocket socket =null;

    public ImageSend(){
    try {
    socket=new DatagramSocket();
    } catch (SocketException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    public static void main(String[] args) throws UnknownHostException, IOException {
    ImageSend send=new ImageSend();
    send.send();
    }

    public void send() throws UnknownHostException, IOException{
    FileInputStream fis = new FileInputStream("004.jpg");
            byte[] buffer = new byte[5000];  
            int len = 0; 
            int count=0;
            while((len = fis.read(buffer)) != -1){  
                System.out.println(len);  
                count++;
                DatagramPacket packet = new DatagramPacket(buffer, len,InetAddress.getByName("localhost"),1220);  
                socket.send(packet);  
            }  
            System.out.println(count);
            socket.close();  
    }
    }
      

  4.   

    java udp发送多个包,为何不用NIO去实现?
      

  5.   

    关于udp的发包和接收包的问题。。我们一般通过抓包工具去看。看看你发送的udp包,和你接收的udp包。。比较一下。。看看是否有数据丢失不过udp的包。。有时的确会存在丢包现象抓包工具:
    wireshark-win32-1.2.5..或者64.。。
      

  6.   

    文件大小 比一下就知道了,UDP 丢包不奇怪……
      

  7.   

    udp没有反馈,发送方无法知道接收方是否成功收取,那就同一个包多发几次