package my;
import java.io.*;
import java.net.*;
public class MyDemo
{
public static void main(String[] args) throws Exception
{
shou();
fa();
}
public static void shou() throws Exception
{
new Thread(new Runnable()
{
BufferedOutputStream bufo=new BufferedOutputStream(new FileOutputStream("D:\\pictures\\jpg\\上善若水(1).jpg"));
DatagramSocket dss=new DatagramSocket(10006);
public void run()
{
while(true)
{
byte[] b=new byte[1024*63];
DatagramPacket dps=new DatagramPacket(b,b.length);
try
{
dss.receive(dps);
System.out.println("-------");
bufo.write(dps.getData(),0,dps.getLength());
bufo.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}).start();
}
public static void fa() throws Exception
{
DatagramSocket ds=new DatagramSocket();
File f=new File("D:\\pictures\\jpg\\上善若水.jpg");
BufferedInputStream bufi=new BufferedInputStream(new FileInputStream(f));
byte[] buf=new byte[1024*63];
int num;
while((num=bufi.read(buf))!=-1)
{
Thread.sleep(1);
DatagramPacket dp=new DatagramPacket(buf,num,InetAddress.getByName("127.0.0.1"),10006);
ds.send(dp);
System.out.println("************");
}
bufi.close();
ds.close();
}
} |