UDP文件传输Java

时间:2016-11-08 21:02:32

标签: java sockets udp

我知道UDP不可靠,不应该用来发送文件,但我被要求在一个小型大学任务的应用程序的一小部分内执行。出于某种原因,当我运行代码将文件从客户端上传到服务器时,我的应用程序会冻结。谁能帮忙告诉我我做错了什么?

客户端:

String hostName = hostNameTxt.getText();
    String portAsString = portNumTxt.getText();
    int portNum = Integer.parseInt(portAsString);      
    String sentFilePath = "c:/Documents/test.txt";
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;
    try {
    servsock = new ServerSocket(portNum);
    while (true) {
        System.out.println("Waiting...");
        try {
        sock = servsock.accept(); //failing here I think
        System.out.println("Accepted connection : " + sock);
        // send file
        File myFile = new File (sentFilePath);
        byte [] mybytearray  = new byte [(int)myFile.length()];
        fis = new FileInputStream(myFile);
        bis = new BufferedInputStream(fis);
        bis.read(mybytearray,0,mybytearray.length);
        os = sock.getOutputStream();
        System.out.println("Sending " + sentFilePath + "(" + mybytearray.length + " bytes)");
        os.write(mybytearray,0,mybytearray.length);
        os.flush();
        System.out.println("Done.");
        }
        finally {
        if (bis != null) bis.close();
        if (os != null) os.close();
        if (sock!=null) sock.close();
        }
    }
    }
    catch (IOException ex) {
        Logger.getLogger(Client1Interface.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex);
    }        finally {
    if (servsock != null) try {
        servsock.close();
    } catch (IOException ex) {
        Logger.getLogger(Client1Interface.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

服务器:

String recievedFilePath = "c:/Documents/source.txt";
    String hostName = "localhost";
    int portNum = 7;
    int fileSize = 6022386;
    try{
        int bytesRead;
    int current = 0;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    Socket sock = null;
    try {
    sock = new Socket(hostName, portNum);
    System.out.println("Connecting...");

  // receive file
    byte [] mybytearray  = new byte [fileSize];
    InputStream is = sock.getInputStream();
    fos = new FileOutputStream(recievedFilePath);
    bos = new BufferedOutputStream(fos);
    bytesRead = is.read(mybytearray,0,mybytearray.length);
    current = bytesRead;

    do {
        bytesRead =
            is.read(mybytearray, current, (mybytearray.length-current));
        if(bytesRead >= 0) current += bytesRead;
    } while(bytesRead > -1);

    bos.write(mybytearray, 0 , current);
    bos.flush();
    System.out.println("File " + recievedFilePath
        + " downloaded (" + current + " bytes read)");
    }
    finally {
    if (fos != null) fos.close();
    if (bos != null) bos.close();
    if (sock != null) sock.close();
    }
    }
    catch(Exception ex){
         ex.printStackTrace( );
         System.out.println("Error Uploading File");
    }

2 个答案:

答案 0 :(得分:0)

您应该尝试使用其他端口。 IP端口7被阻止用于echo服务,它只会向您发回相同的数据。

您应该使用1024以上的端口。否则您需要超级用户从您的操作系统使用此端口。

你的应用程序命名有点令人困惑。通常,服务器应在未使用的端口上提供ServerSocket并侦听。客户端必须使用常规Socket连接到此端口并发送数据。

答案 1 :(得分:0)

我一直在测试你的程序(服务器和客户端在同一主机上运行),工作。不过,我要警告你一些重要的细节(基本上所有这些细节都已经在评论中说过了):

概念细节

  • 所选的API(java.net.Socketjava.net.ServerSocket)是 TCP套接字实现,而不是UDP。 UPD API为java.net.DatagramSocketjava.net.DatagramPacket
  • 第一个程序无疑是服务器,第二个程序是客户端

技术细节

  • 您必须首先启动服务器,然后启动客户端。
  • 服务器和客户端必须在不同的JVM上运行,因此您必须在不同的进程上运行每个JVM,无论是在命令shell中还是从IDE运行它。
  • (正如Lars Repenning所说)你必须使用超过1024的任何可用端口,例如3000.

次要技术细节

  • 不要分配与文件大小一样大的缓冲区。要将数据写入文件,您应该使用缓冲技术:使用小缓冲区(4096字节或多个),并在每次迭代时,用InputStream.read填充它并用OutputStream.write写入。您将避免内存问题,并且还可以节省您事先知道文件大小的需要。