I.Ruskov
I.Ruskov

Reputation: 73

Corrupted string through Sockets in java

I have the a problem transfering strings through sockets in Java. When i try to receive data from the server the data is transfered successfully but it's corrupted. I can't find out what is going wrong.

Here is the client piece of code:

public String getUserNameServerResult(String userNameParameter) {
    Socket socket;
    BufferedReader socketReader;
    PrintWriter socketWriter;
    String userNameResultParameter = null;
    try {
        socket = new Socket(HOST_NAME, PORT_NUMBER);
        socket.setSoTimeout(SERVER_REQUEST_TIMEOUT);
        socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        socketWriter = new PrintWriter(socket.getOutputStream());
        socketWriter.println(numberRequest);
        socketWriter.flush();
        socketWriter.println(userNameParameter);
        socketWriter.flush();
        userNameResultParameter = socketReader.readLine();
        socketReader.close();
        socketWriter.close();
        socket.close();
        System.out.println(userNameResultParameter);
    } catch(IOException ioEx) {
        ioEx.printStackTrace();
    }
        return userNameResultParameter;
}

The read line from readLine() method transfers a corrupted string.

Here is the server piece of code:

public synchronized void run(){
    System.out.println(new Date().toString()+" :\nAccepted client: "
            +socket.getInetAddress()+":"+socket.getPort()+"\n");
    try {
        while(!isInterrupted()) {
            String numberRequest = socketReader.readLine();
            if(numberRequest == null) {
                break;
            }
            switch(numberRequest) {
                case "1":    // Determines if the user name exists in the Database.
                    String userName = socketReader.readLine(); 
                    boolean isUserName = ServerRequest.isUserNameExists(userName);
                    socketWriter.println(isUserName);
                    socketWriter.flush();
                    break;
                default: break;
            }
        }
    } catch(IOException ex) {
        System.err.println(ex.getMessage());
    }
}

The "isUserName" variable is transfered and it gets corrupted in the client.

Can you help to solve this problem? Thanks!

Upvotes: 3

Views: 372

Answers (2)

durron597
durron597

Reputation: 32323

isUserName is a boolean. Perhaps you meant socketWriter.println(userName); ?

Please use the code below to show us what the corrupted String looks like!

System.out.println(userNameResultParameter);
System.out.print("Chars:");
for (char c : userNameResultParameter.toCharArray())
    System.out.print(" 0x" + Integer.toHexString((int)c));

Upvotes: 1

Isaac
Isaac

Reputation: 16736

The problem, I think, is with the construction of the InputStreamReader. By default, the reader is assuming that the encoding of the incoming character data is your platform's default encoding, however the Writer (on the server side) is sending UTF-8 data.

Try constructing the InputStreamReader like this:

new InputStreamReader(socket.getInputStream(), "UTF-8")

Upvotes: 0

Related Questions