简体   繁体   中英

Get an array from Server/Client app (Java)

So I have a Server/Client layer app running between my application and database. I would like to get an array from the Server. I will paste some pieces of code which I think is enough to give you an idea of what is going on:

I send to the server the keyword for search in database (user and his password)

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

Here is the code of the Server:

  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: NOTE that some array elements are actually long text.

I recommended you use other technically. what happended if username and password that you send to the server contain a ",". When you split you obtain a wrong data.

Before send: Example:

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

In your server:

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

The server should response a string like this:

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

The client side read the response like this:

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM