服务器 S.javaimport java.net.*;
import java.io.*;
public class S{
public static void main (String[] args) throws Exception{
DatagramSocket ds=new DatagramSocket(6000);
File f=new File("2.jpg");
FileOutputStream ou=new FileOutputStream(f);
byte[] buf=new byte[10000];
DatagramPacket dp=new DatagramPacket(buf,10000);
System.out.println(dp.getLength());
ds.receive(dp);
ou.write(buf);
System.out.println(new String(buf,0,dp.getLength()));
ds.close();
}
}
客户端import java.net.*;
import java.io.*;
public class C{
public static void main (String[] args) throws Exception{
DatagramSocket ds=new DatagramSocket();
File f=new File("Humpback Whale.jpg");
InputStream in=new FileInputStream(f);
byte[] buf=new byte[10000];
in.read(buf);
DatagramPacket dp=new DatagramPacket(buf,10000,InetAddress.getByName("localhost"),6000);
System.out.println("before send");
ds.send(dp);
System.out.println("after send");
ds.close();
}
}
这个程序的问题就是当图像文件小于10kb的时候还可以,但大于他的时候就不行了。我觉得是当把文件流放入byte数组的时候就把byte new成了10000所以就不行了问题是DatagramPacket dp=new DatagramPacket(buf,10000,InetAddress.getByName("localhost"),6000);这句话种的10000如何知道buf有用数据的实际大小,而不是10000呢?

解决方案 »

  1.   

    在服务器端用ou.length();在客户端用in.available()试试 
      

  2.   

    UDP传输不保证传输数据的正确性和先后性,有可能后传的数据比先传的数据先被接收到,
    UDP每一次传输的数据量最好控制在 8000(具体多少记得不是很清楚) 字节以内。
      

  3.   

    ou哪有length方法呀。客户端的f倒是有这个方法。
      

  4.   

    要循环输出所有的数据流,客户端改成这样,服务端也要类似处理!
    import java.net.*;
    import java.io.*;
    public class C{
        public static void main (String[] args) throws Exception{
            DatagramSocket ds=new DatagramSocket();
            File f=new File("Humpback Whale.jpg");
            InputStream in=new FileInputStream(f);
            byte[] buf=new byte[10000];
            int actReadNum;
            DatagramPacket dp = null;
            System.out.println("before send");
            while ((actReadNum=in.read(buf)) != -1){
                dp = new DatagramPacket(buf,actReadNum,InetAddress.getByName("localhost"),6000);
                ds.send(dp);
            }
            System.out.println("after send");
            ds.close();
        }
    }
      

  5.   

    或者把客户端改成:import java.net.*;
    import java.io.*;
    public class C{
        public static void main (String[] args) throws Exception{
            DatagramSocket ds=new DatagramSocket();
            File f=new File("Humpback Whale.jpg");
            InputStream in=new FileInputStream(f);
            byte[] buf=new byte[in.available()];
            int actReadNum;
            DatagramPacket dp = null;
            System.out.println("before send");
            while ((actReadNum=in.read(buf)) != -1){
                dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("localhost"),6000);
                ds.send(dp);
            }
            System.out.println("after send");
            ds.close();
        }
    }
      

  6.   

    xunyiren 你调试好了在发代码呀,用你的代码传输得到的图像是错误的图像。
      

  7.   

    刚才试了一下,加大server端的byte大小就可以收到了,不过这似乎不是很好的方法。至于lz的问题,我认为是这样:发送方的UDP包长根据你实际需要来设定,而接收方的UDP包长你可以设置为最大的IP包长。如果还不能满足需要那你只能将数据包切片+偏移发送了。不过不推荐这样,因为UDP本身也不是为了发送大文件而生的。
      

  8.   

    抱歉说错了,的确没有length方法
      

  9.   

    我不是说了服务器端也要改动吗?可以这样,这是调试过的代码:
    server 端:
    import java.net.*;
    import java.io.*;
    public class UDPServer{
        public static void main (String[] args) throws Exception{
            DatagramSocket ds=new DatagramSocket(6000);
            File f=new File("d:/2.jpg");
            FileOutputStream ou=new FileOutputStream(f);
            byte[] buf=new byte[32];
            DatagramPacket dp=new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            buf=new byte[Integer.parseInt(new String(buf, 0, dp.getLength()))];
            dp.setData(buf);
            ds.receive(dp);
            ou.write(buf);
            System.out.println(new String(buf,0,dp.getLength()));
            ds.close();
        }
    }client 端:
    import java.net.*;
    import java.io.*;
    public class UDPClient{
        public static void main (String[] args) throws Exception{
            DatagramSocket ds=new DatagramSocket();
            
            
            File f=new File("d:/Humpback Whale.jpg");
            InputStream in=new FileInputStream(f);
            String fileLength = String.valueOf(in.available());
            DatagramPacket dp=new DatagramPacket(fileLength.getBytes(),fileLength.getBytes().length,InetAddress.getByName("localhost"),6000);
            ds.send(dp);
            byte[] buf=new byte[in.available()];
            in.read(buf);
            dp.setData(buf);
            System.out.println("before send");
            ds.send(dp);
            System.out.println("after send");
            ds.close();
        }
      

  10.   

    udp的最大包长也不是太大。而且我的第一个程序,传输 小于10kb的图像勉强可以,但是问题还是挺多的,比如原来是8kb,生成的图像虽然显示正常,但是却是10kb。
    我觉得把udp分包的思想是对的,但是如何分的好好考虑一下。