0

好的,以下是我在创建简单 TFTP(普通文件传输协议)程序时使用的 2 个类。我正在运行该程序的服务器实例和客户端实例。我想要完成的是:

客户端连接到服务器并发送它想要的特定文件的消息。服务器定位文件并发送响应(1 或 0...1 表示文件存在,0 表示不存在)。然后服务器将文件的内容发送到客户端应用程序。我要发送的文件只是简单的文本文件。

现在,我能够收到客户想要的文本文件的名称,但是当我去发回响应时,我没有得到任何返回。以下是服务器和客户端都运行的方法。

这是服务器实例

SwingUtilities.invokeLater(new Runnable(){
    public void run(){
        String fileRequest = UDPReceiver(SERVER_PORT_NUMBER);
        outputArea.append("\n" + "File Requested: " + fileRequest + "\n");
        outputArea.append("Determining if file exists...\n");
        String checkFile = SHARED_DIR + "\\" + fileRequest;
        outputArea.append("Checking location: " + checkFile + "\n");

        boolean check = fileCheck(checkFile);
        if(check == true){
            outputArea.append("File location verified..." + "\n");
            outputArea.append("Initiating transfer...." + "\n\n");

            UDPSender(CLIENT_HOSTNAME, CLIENT_PORT_NUMBER, "1");

            }
            else{
                outputArea.append("File does not exist..." + "\n");
                outputArea.append("Exiting run..." + "\n");
            }
       } 
  });

客户端实例。

SwingUtilities.invokeLater(new Runnable(){
    public void run(){
        UDPSender(SERVER_HOSTNAME, SERVER_PORT_NUMBER, FILE_REQUEST);

        String message = UDPReceiver(CLIENT_PORT_NUMBER);
        outputArea.append("\n\n" + message + "\n");

        if(message == "1"){
            // File exists
            outputArea.append("\n");
            outputArea.append("File verified..." + "\n");
            outputArea.append("Transfer initiated..." + "\n");
        }
        else{
            // File doesn't exist
            outputArea.append("\n");
            outputArea.append("File does not exist..." + "\n");
            outputArea.append("Terminating connection...");
        }
    } 
});

下面是 Sender 和 Receiver 方法。

 private void UDPSender(String hostname, String port, String message){
   DatagramSocket socket = null;
   try{
       // Create a datagram socket, look for the first available port
       socket = new DatagramSocket();

       outputArea.append("Using local port: " + socket.getLocalPort() + "\n");
       ByteArrayOutputStream bOut = new ByteArrayOutputStream();
       PrintStream pOut = new PrintStream(bOut);
       pOut.print(message);
       // Convert printstream to byte array
       byte[] bArray = bOut.toByteArray();
       // Create a DatagramPacket, containing a maximum buffer of 256 bytes
       DatagramPacket packet = new DatagramPacket(bArray, bArray.length);

       outputArea.append("Looking for hostname " + hostname + "\n");
       // Get the InetAddress object
       InetAddress remote_addr = InetAddress.getByName(hostname);
       // Check its IP Number
       outputArea.append("Hostname has IP Address = " + remote_addr.getHostAddress() + "\n");
       // Configure the DatagramPacket
       packet.setAddress(remote_addr);
       packet.setPort(Integer.parseInt(port));
       // Send the packet
       socket.send(packet);
       outputArea.append("Packet sent at: " + new Date() + "\n");

       // Display packet information
       outputArea.append("Sent by: " + remote_addr.getHostAddress() + "\n");
       outputArea.append("Sent from: " + packet.getPort() + "\n");
       socket.close();
   }
   catch(UnknownHostException ue){
       outputArea.append("Unknown host: " + hostname + "\n");
       outputArea.append("Unknown host: " + ue + "\n");
   }
   catch(IOException e){
       outputArea.append("Error: " + e + "\n");
   }
}


private String UDPReceiver(String portNum){
   String message = "";
   DatagramSocket socket = null;
   try{
       // Create a DatagramSocket
       socket = new DatagramSocket(Integer.parseInt(portNum));

       outputArea.append("Listening on local port " + socket.getLocalPort() + "\n");

       // Create a DatagramPacket, containing a maximum buffer of 256 bytes
       DatagramPacket packet = new DatagramPacket(new byte[256], 256);

       // Receive a packet - remember by default this is a blocking operation
       socket.receive(packet);

       outputArea.append("Packet received at " + new Date() + "\n");
       // Display packet information
       InetAddress remote_addr = packet.getAddress();
       outputArea.append("Sender: " + remote_addr.getHostAddress() + "\n");
       outputArea.append("From Port: " + packet.getPort() + "\n");

       CLIENT_HOSTNAME = remote_addr.getHostAddress();
       //CLIENT_PORT_NUMBER = Integer.toString(packet.getPort());

       // Display packet contents, by reading from byte array
       ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData());

       // Display only up to the length of the original UDP packet
       for(int i = 0; i < packet.getLength(); i++){
           int data = bin.read();
           if(data == -1){
               break;
           }
           else{
               message = message + (char)data;
               //outputArea.append(Character.toString((char)data));
           }
       }
       socket.close();
       return message;
   }
   catch(IOException e){
       outputArea.append("Error: " + e + "\n");
       message = "Error: " + e;
       return message;
   }
}

你们可以提供的任何帮助将不胜感激。我想弄清楚的主要事情是如何让服务器和客户端能够来回发送消息。提前谢谢各位。

编辑:

当我运行这个项目时,我现在在 Netbeans 中也遇到了一个错误。我认为它与UDPReceiver方法中的这行代码有关:

socket = new DatagramSocket(Integer.parseInt(portNum));

但我无法弄清楚这有什么问题。

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at tftp_gui.main.UDPReceiver(main.java:508)
at tftp_gui.main.access$800(main.java:20)
at tftp_gui.main$10.run(main.java:374)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
4

1 回答 1

1

你的方法是错误的,因为它需要打开客户端端口,无论如何你的特殊问题是因为你没有在找不到文件时发回结果:

查看:

if(check == true){
    outputArea.append("File location verified..." + "\n");
    outputArea.append("Initiating transfer...." + "\n\n");
    UDPSender(CLIENT_HOSTNAME, CLIENT_PORT_NUMBER, "1");
}
else {
    outputArea.append("File does not exist..." + "\n");
    outputArea.append("Exiting run..." + "\n");
    UDPSender(CLIENT_HOSTNAME, CLIENT_PORT_NUMBER, "0"); // here you should send 0
}

另外,你有一个小问题:if(message == "1")它应该看起来像if ("1".equals(message))

您的错误是可以自我解释的 - 您没有声明正确的 CLIENT_PORT_NUMBER 或 SERVER_PORT_NUMBER,这是我用于测试的内容:

private final static String SERVER_PORT_NUMBER = "1234";
private static String CLIENT_HOSTNAME;
private static final String CLIENT_PORT_NUMBER = "2345";
private static final String FILE_REQUEST = "a.txt";
private static final String SHARED_DIR = "d:/";
private static final String SERVER_HOSTNAME = "localhost";
于 2013-10-22T23:25:31.370 回答