0

我正在尝试通过Weberknecht Java library连接到 WebSocket 。当 Java 客户端尝试创建连接时,我收到以下错误消息:

W/System.err(805): java.lang.ArrayIndexOutOfBoundsException: length=1000; index=1000
W/System.err(805): at ...WebSocketConnection.connect(WebSocketConnection.java:86)

这是导致问题的部分:

public void connect() throws WebSocketException {
    try {
        if (connected) {
            throw new WebSocketException("already connected");
        }

        socket = createSocket();
        input = new DataInputStream(socket.getInputStream());
        output = new PrintStream(socket.getOutputStream());

        output.write(handshake.getHandshake());

        boolean handshakeComplete = false;
        int len = 10000;
        byte[] buffer = new byte[len];
        int pos = 0;
        ArrayList<String> handshakeLines = new ArrayList<String>();

        while (!handshakeComplete) {
            int b = input.read();
            buffer[pos] = (byte) b; // THIS LINE CAUSES THE EXCEPTION
            pos += 1;

            if (buffer[pos - 1] == 0x0A && buffer[pos - 2] == 0x0D) {
                String line = new String(buffer, "UTF-8");
                if (line.trim().equals("")) {
                    handshakeComplete = true;
                } else {
                    handshakeLines.add(line.trim());
                }

                buffer = new byte[len];
                pos = 0;
            }
        }

什么可能导致这个问题?是不是这个客户端库可能不符合服务器使用的 WebSocket 版本?

Weberknecht那边的问题还没有解决。我想在这里问这个问题,因为这似乎是一个相当普遍的问题(读取 WebSocket 标头和握手数据)。

4

3 回答 3

1

代码需要检查-1input.read() 以表示连接命中 EOF。

此外,我将假设基于相同的代码位,如果握手在10000字节之后没有到达,则它是无效的握手。

while (!handshakeComplete) {
            int b = input.read();

            if (b == -1)
                throw new WebSocketException ("Connected closed.");                

            if (pos == buffer.length)
                throw new WebSocketException ("Invalid handshake.");

            buffer[pos] = (byte) b; // THIS LINE CAUSES THE EXCEPTION
            pos += 1;

            if (buffer[pos - 1] == 0x0A && buffer[pos - 2] == 0x0D) {
                String line = new String(buffer, "UTF-8");
                if (line.trim().equals("")) {
                    handshakeComplete = true;
                } else {
                    handshakeLines.add(line.trim());
                }

                buffer = new byte[len];
                pos = 0;
            }
        }

更新

这是一个更好的实现:

public void connect() throws WebSocketException {
    try {
        if (connected) {
            throw new WebSocketException("already connected");
        }

        socket = createSocket();
        input = new DataInputStream(socket.getInputStream());
        output = new PrintStream(socket.getOutputStream());

        output.write(handshake.getHandshake());

        boolean handshakeComplete = false;

        ArrayList<String> handshakeLines = new ArrayList<String>();

        BufferedReader reader = new BufferedReader(input);

        String handshakeData;
        while ((handshakeData = reader.readLine()) != null) {
            if (handshakeData.trim().equals("")) {
               handshakeComplete = true;
               break;
            }
            handshakeLines.add(handshakeData);
        }

        if (!handshakeComplete)
            throw new WebSocketException ("Failed to establish handshake.");
于 2012-11-04T02:04:14.243 回答
1

我只是在想,你为什么不使用int read(byte[] b)which 为你读取字节。int read(byte[] b) --> Reads some number of bytes from the contained input stream and stores them into the buffer array b.

 while (!handshakeComplete) {
   int bytesRead = input.read(buffer);//returns number of bytes read
   if(bytesRead  < 10000 || bytesRead == -1 ||
      (buffer[bytesRead - 1] == 0x0A && buffer[bytesRead - 2] == 0x0D)){
        //done with reading
        handshakeComplete = true;
   }
   if(bytesRead != -1){
      String line = new String(buffer, "UTF-8");
      if (!line.trim().equals("")) {
         handshakeLines.add(line.trim());
      }
   }
 }
于 2012-11-04T02:39:13.820 回答
0

只是简单地看一下,你不能保证之前停止buffer.length == pos。这是需要仔细检查的一件事 -pos必须严格小于缓冲区的长度。

于 2012-11-04T01:59:51.263 回答