在Java上编写WebSocket服务器。 Windows 7 64位问题

时间:2015-06-23 18:58:01

标签: javascript sockets windows-7-x64 java-websocket

我遇到了WebSocket协议的问题。

当我在开发(NetBeans)时执行代码并且工作正常,即使我执行jar文件仍然有效,但是...当我在Windows 7 64位平台上执行jar时... WebSocket自行关闭

我在服务器和客户端的本地计算机上工作,即使我停用了Windows防火墙,也没有任何变化。

即使我使用3种浏览器(Firefox,Chrome和Opera)进行测试,我也可以获得相同的结果。

有什么想法吗?

服务器侦听线程的代码

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());
        }
    }

websocket协议代码:

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 个答案:

答案 0 :(得分:1)

看起来失败是关于Windows 7上JVM上的默认字符集。

在通话中

call java -jar filename.jar

你必须添加

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