服务器 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呢?
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呢?
UDP每一次传输的数据量最好控制在 8000(具体多少记得不是很清楚) 字节以内。
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();
}
}
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();
}
}
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();
}
我觉得把udp分包的思想是对的,但是如何分的好好考虑一下。