用户登录  |  用户注册
首 页源码下载网络学院最新源码源码排行屏蔽广告
当前位置:新兴网络 > 网络学院 > Java编程 > Java SE

java UDP协议传输文件

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2010-07-15 21:21:30
Java code复制代码
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class CudpSocket ...{
   
    DatagramPacket dp = null;
    DatagramSocket dgsocket=null;
    public CudpSocket()...{
        try ...{
            byte[] buf = new byte[1000];
            dgsocket = new DatagramSocket(12345);
            dp         = new DatagramPacket(buf,buf.length);
           
           
        } catch (SocketException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
    public static void main(String[] args)...{
       
        System.out.println("enter the server");
       
        CudpSocket css = new CudpSocket();
       
        try ...{
            css.dgsocket.receive(css.dp);
           
            byte data[] = css.dp.getData();
           
            System.out.println("datac.length : "+data.length);
           
            for(int i=0;i<data.length;i++)...{
                System.out.println(data[i]);
            }
           
            NetFileW nfw = new NetFileW("C:/test/b.txt");
           
            nfw.write(css.dp.getData());
        //    System.out.println(new String(css.dp.getData()));
           
           
        } catch (IOException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
   
   
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class NetFileR ...{
   
    private String filePath;
   
    public NetFileR(String filePath)...{
       
        this.filePath = filePath;
       
    }
   
    public byte[] getData() throws IOException...{
       
        File file = new File(filePath);
       
        FileInputStream filein = new FileInputStream(file);
   
        DataInputStream in = new DataInputStream(filein);
       
        byte data[] = new byte[8];
       
        in.read(data);
       
        return data;
    }
   
   
   
  

    public String getFilePath() ...{
        return filePath;
    }

    public void setFilePath(String filePath) ...{
        this.filePath = filePath;
    }

}
///////////////////////////////////////////////////////////////////////
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class NetFileW ...{
   
    public NetFileW(String filePath)...{
        this.filePath = filePath;
    }
   
    private String filePath;
   
    public void write(byte[] data) throws IOException...{
       
        File file = new File(filePath);
       
        FileOutputStream out = new FileOutputStream(file);
       
        out.write(data);
       
    }
  

    public String getFilePath() ...{
        return filePath;
    }

    public void setFilePath(String filePath) ...{
        this.filePath = filePath;
    }
   
  

}
///////////////////////////////////////////////
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SudpSocket ...{
   
    private DatagramSocket dgs = null;
   
    private DatagramPacket dgp = null;
   
    public SudpSocket(String host,int prot,byte[] data)...{
       
        try ...{
           
            dgs = new DatagramSocket(9999);
           
            Inet4Address target=null;
            try ...{
                target = (Inet4Address) Inet4Address.getByName(host);
            } catch (UnknownHostException e) ...{
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
            dgp = new DatagramPacket(data,data.length,target,prot);
        } catch (SocketException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
    }
   
    public static void main(String args[])...{

        NetFileR nf = new NetFileR("C:/test/a.txt");
       
        SudpSocket sps;
        try ...{
           
            byte[] data = nf.getData();
           
            System.out.println("data.length : "+data.length);
           
            for(int i=0;i<data.length;i++)...{
                System.out.println(data[i]);
            }
           
       
           
            sps = new SudpSocket("192.168.152.170",12345,data);
        } catch (IOException e1) ...{
            // TODO Auto-generated catch block
            e1.printStackTrace();
            sps=null;
        }
       
        try ...{
            sps.dgs.send(sps.dgp);
        } catch (IOException e) ...{
            // TODO Auto-generated catch block
            e.printStackTrace();
            sps=null;
        }
       
        System.out.println("over the sending");
       
    }
   
}
 

Tags:Java UDP 文件传输

作者:佚名
  • 好的评价 如果您觉得此文章好,就请您
      43%(3)
  • 差的评价 如果您觉得此文章差,就请您
      57%(4)

网络学院评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论