黑马程序员技术交流社区
标题:
UDP大文件传输相关(还有点问题,需要发送端线程休眠1毫秒)
[打印本页]
作者:
刘汉文
时间:
2013-12-27 15:54
标题:
UDP大文件传输相关(还有点问题,需要发送端线程休眠1毫秒)
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();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2