Writing WebSocket Server on Java. Issue on Windows 7 64 bits

185 views Asked by At

I having a problem with WebSocket protocol.

When I execute the code when develop (NetBeans) and works fine, even when I execute the jar file still works, but... When I execute the jar on Windows 7 64 bits platform... The WebSocket close by itself.

I'm working on local machine both the server and client, even I deactivate windows firewall and nothing changes.

Even I test with 3 browsers (Firefox, Chrome and Opera) and I fund the same result.

Any ideas?

Code of server listening thread

InputStream listen;

    while (true) {
        try {
            if (socket.isClosed()) {
                socket.close();
                return;
            } else {
                listen = socket.getInputStream();
                byte[] readData = new byte[1024];
                int totalRead = listen.read(readData);

                if (totalRead > 0) {
                    byte[] result = new byte[totalRead];

                    System.arraycopy(readData, 0, result, 0, totalRead);

                    String resultString = new String(result);

                    if (resultString.substring(0, 3).equals("GET")) {
                        HtmlRequest myRequest = new HtmlRequest(resultString);
                        caller.push(myRequest);
                    } else {
                        caller.push(WebsocketProtocol.decrypt(result, totalRead));
                    }
                }

                System.out.println(getName()+": total read -> "+totalRead);
            }
        }catch(SocketException e){
            System.out.println(getName() + ": SocketException "+e.getMessage());
        } catch (IOException ex) {
            System.out.println(getName() + ": IOException "+ex.getMessage());
        } catch (NullPointerException ex){
            System.out.println(getName() + ": NullPointerException "+ex.getMessage());
        } catch (Exception e) {
            System.out.println(getName() + ": Exception "+e.getMessage());
        }
    }

Code of websocket protocol:

public static byte[] decrypt(byte[] leido, int largo) {
    byte rLength = 0;
    int rMaskIndex = 2;
    int rDataStart = 0;

    byte data = leido[1];
    byte op = (byte) 127;
    rLength = (byte) (data & op);

    if (rLength == (byte) 126) {
        rMaskIndex = 4;
    }
    if (rLength == (byte) 127) {
        rMaskIndex = 10;
    }

    byte[] masks = new byte[4];

    int j = 0;
    int i = 0;
    for (i = rMaskIndex; i < (rMaskIndex + 4); i++) {
        masks[j] = leido[i];
        j++;
    }

    rDataStart = rMaskIndex + 4;

    int messLen = largo - rDataStart;

    byte[] message = new byte[messLen];

    for (i = rDataStart, j = 0; i < largo; i++, j++) {
        message[j] = (byte) (leido[i] ^ masks[j % 4]);
    }
    return message;
}

public static byte[] encrypt(String message) {
    byte[] bytesContent = message.getBytes();

    int countFrames = 0;

    byte[] frame = new byte[10];

    frame[0] = (byte) 129;

    if (bytesContent.length <= 125) {
        frame[1] = (byte) bytesContent.length;
        countFrames = 2;

    } else if (bytesContent.length > 125 && bytesContent.length <= 65535) {
        frame[1] = (byte) 126;
        int largo = bytesContent.length;
        frame[2] = (byte) ((largo >> 8) & (byte) 255);
        frame[3] = (byte) (largo & (byte) 255);
        countFrames = 4;
    } else {
        frame[1] = (byte) 127;
        int largo = bytesContent.length;
        frame[2] = (byte) ((largo >> 56) & (byte) 255);
        frame[3] = (byte) ((largo >> 48) & (byte) 255);
        frame[4] = (byte) ((largo >> 40) & (byte) 255);
        frame[5] = (byte) ((largo >> 32) & (byte) 255);
        frame[6] = (byte) ((largo >> 24) & (byte) 255);
        frame[7] = (byte) ((largo >> 16) & (byte) 255);
        frame[8] = (byte) ((largo >> 8) & (byte) 255);
        frame[9] = (byte) (largo & (byte) 255);
        countFrames = 10;
    }

    byte[] answer = new byte[countFrames + bytesContent.length];

    int currentPosition = 0;
    for (int i = 0; i < countFrames; i++) {
        answer[currentPosition] = frame[i];
        currentPosition++;
    }
    for (int i = 0; i < bytesContent.length; i++) {
        answer[currentPosition] = bytesContent[i];
        currentPosition++;
    }
    return answer;
}
1

There are 1 answers

0
Nacho Escursell On BEST ANSWER

Looks like the fail is about the default character set on JVM on windows 7.

In the call

call java -jar filename.jar

You have to add

call java -Dfile.encoding=UTF-8 -jar filename.jar