0

所以我在我的应用程序和数据库之间运行了一个服务器/客户端层应用程序。我想从服务器获取一个数组。我将粘贴一些我认为足以让您了解正在发生的事情的代码:

我将在数据库中搜索的关键字(用户和他的密码)发送到服务器

    fromUser = Musername + "," + Password;
    out.println(fromUser);

这是服务器的代码:

  public class Server {

    public static String[] theOutput;
    public static String inputLine;
    public static String[] string_array;
    public static String output = "";

    public static String[] process(String Input) throws Exception {

        String[] data = Input.split(",");

        // Call database class to get the results and store them into the array
        load_login pridobi = new load_login();
        theOutput = pridobi.nalozi(data[0], data[1]);

        return theOutput;

    }

    public static void main(String[] args) throws Exception {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

// get the username and password
        inputLine = in.readLine();

        if (inputLine.length() != 0) {

            string_array = process(inputLine);
        }

// And here I would like to do something like that :/
        out.println(string_array);

    }
}

PS:请注意,某些数组元素实际上是长文本。

4

1 回答 1

0

我建议你在技术上使用其他。如果您发送到服务器的用户名和密码包含“,”会发生什么。当您拆分时,您会获得错误的数据。

发送前:示例:

String username = URLEncoder.encoder("myusername", "utf-8");
String password = URLEncoder.encoder("mypassword", "utf-8");
String dataToSend = username + "," + password;

在您的服务器中:

String[] data = Input.split(",");
data[0] = URLDecoder.decoder(data[0],"utf-8");
data[1] = URLDecoder.decoder(data[1],"utf-8");

服务器应该响应这样的字符串:

String responseData =  URLEncoder.encoder(theOutput[0], "utf-8") + "," + URLEncoder.encoder(theOutput[1], "utf-8");
out.println(responseData);

客户端读取这样的响应:

String dataReceived = inputLine = in.readLine();
String data[] = dataReceived.split(",");
data[0] = URLDecoder.decoder(data[0],"utf-8");
data[1] = URLDecoder.decoder(data[1],"utf-8");
于 2013-09-15T23:47:48.640 回答