java UDP协议传输文件



java UDP协议传输文件。

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");

    }

}