第一个问题:调用receive时,覆盖bigArray
第二个问题:进行ANSI和unicode转换,
第三个问题:看看server端是否在发数据

解决方案 »

  1.   

    下面是一个客户端向服务端发文件的例子。服务端发向客户端大致相同。
    希望能对你有帮助。import java.io.FileOutputStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;public class UDPServer {    final int PORT = 1111;
        final int PACKET_SIZE = 512;    void start() throws Exception {
            String filename = "test1.txt"; // target file name, change it
            System.out.println("UDPServer started...");
            DatagramSocket socket = new DatagramSocket(PORT);
            FileOutputStream fos = new FileOutputStream(filename);
            boolean endFlag = false;
            int i = 0;
            while (true) {
                i++;
                byte[] buf = new byte[PACKET_SIZE];
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
                socket.receive(dp);
                fos.write(dp.getData(), 0, dp.getLength());
                if (dp.getLength() < PACKET_SIZE) {
                    break;
                }
            }
            fos.close();
            System.out.println("File received and wrote to: \"" + filename + "\"");
            socket.close();
        }    public static void main(String[] args) throws Exception {
            new UDPServer().start();
        }
    }
    //////////
    import java.io.FileInputStream;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;public class UDPClient {    final int PORT = 1111; // server port
        final int PACKET_SIZE = 512;
        final String serverIp = "127.0.0.1"; // change it    void sendFile(String filename) throws Exception {
            DatagramSocket socket = new DatagramSocket();
            byte[] buf = new byte[PACKET_SIZE];
            InetAddress address = InetAddress.getByName(serverIp);
            FileInputStream fis = new FileInputStream(filename);
            boolean endFlag = false;
            int i = 0;
            int len = -1;
            while ((len = fis.read(buf, 0, buf.length)) != -1) {
                i++;
                DatagramPacket packet = new DatagramPacket(buf, len, address, PORT);
                socket.send(packet);
            }
            fis.close();
            System.out.println("File sent to: " + serverIp);
            socket.close();
        }    public static void main(String[] args) throws Exception {
            String filename = "test.txt";
            if (args.length == 1) {
                filename = args[0];
            }
            UDPClient client = new UDPClient();
            client.sendFile(filename);
        }
    }
      

  2.   

    关注..学习中...给点系统资料好不好[email protected]