-1

我正在用java编写一个简单的客户端服务器程序。客户端向服务器发送文件名。服务器接收它,然后对其进行解析,并在解析文件名后向客户端发送响应,无论文件名是正确还是无效。

在服务器中,当它解析文件名时会抛出 NullPointerException 错误。

这是代码:

Server.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;


public class Server extends Thread {

private static int port;
private ServerSocket serverSocket=null;
private String ReceivedFilename=null;
private String ServerResponse=null;

public Server(int port) throws IOException {
    serverSocket = new ServerSocket(port);
    //serverSocket.setSoTimeout(20000);
}

public void run() {
    while (true) {
        try {
            System.out.println("Waiting for client to send File name...");
            Socket server = serverSocket.accept();
            System.out.println("Connected to Client");
            DataInputStream in = new DataInputStream(server.getInputStream());
            ReceivedFilename = in.readUTF();
            System.out.println("Received Filename: "+ReceivedFilename);
            if(ReceivedFilename == "")
                ServerResponse = "Filename is empty!";
            else{
                if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)
                {
                    ServerResponse = "Correct file name provided.";
                }
                else
                {
                    ServerResponse = "Invalid Filename! Server only accepts .bat files";
                }
            }
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF(ServerResponse);
            server.close();
        } catch (SocketTimeoutException s) {
            System.out.println("Socket timed out!");
            break;
        } catch (IOException e) {
            e.printStackTrace();
            break;
        }
    }
}

public static void main(String[] args) {
    port = 9000;
    try {
        Thread t = new Server(port);
        t.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Client.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

private static String serverName;

public static void main(String[] args) {
    String sName = "localhost";
    int port = 9000;
    String Filename = "Scripts.bat";
    try {
        System.out.println("Connecting to Server");
        Socket client = new Socket(sName, port);
        System.out.println("Connected to Server");
        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        System.out.println("Sending filename to Server");
        out.writeUTF(Filename);
        System.out.println("Filename sent");
        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());
        client.close();
    } catch (IOException e) {
    }
}
}

以下是服务器上的输出和异常:

在此处输入图像描述

请帮忙。

4

2 回答 2

1
ServerResponse=null

您没有在除 . 之外的任何地方设置 ServerResponse if branch,但看起来它会直接到达您else branch调用的位置ServerResponse.indexOf- 此时它仍然为空。

此外,您应该以小写字母开头命名变量,ServerResponse应该是serverResponse.

于 2014-07-02T10:36:46.170 回答
1

仅当为空时,才将ServerResponsenull 作为if检查ReceivedFilename分配默认值的条件ReceivedFilename

if(ReceivedFilename.equals("")) // I changed it to use equals method
    ServerResponse = "Filename is empty!"; // only here the default value is assigned

您需要将 分配Receivedfilename给您ServerResponse查看代码中的逻辑。

if(ReceivedFilename.equals("")) // I changed it to use equals method
    ServerResponse = "Filename is empty!";
else{
     // If it comes to the else part, the ServerResponse is still null as initialized at the start and therefore throws the NPE
     ServerResponse = ReceivedFilename; // incase ReceivedFilename is not blank
     if(ServerResponse.indexOf(".bat") == ServerResponse.length()-4)
     ...
}

作为旁注,您需要使用equals()方法来比较String值,而不是使用==比较字符串对象引用的运算符。trim()在尝试检查其是否等于空白字符串时,您可能会使用 ReceivedFilename,但这取决于您。当然,您有许多不同的方法来检查字符串是否为空。您可以自己调整方法来执行此操作,也可以从已经具有该方法的第三方库中获取帮助。

于 2014-07-02T10:37:17.160 回答