簡體   English   中英

套接字Java不能接收太多數據

[英]Socket java can't receive too much data

這是我的套接字實現:

s = new Socket(this.host,this.port);
s.setReceiveBufferSize(2048*2048);
s.setSendBufferSize(2048*2048);

當我收到太多數據時,我的數據會像這樣裁剪:

Recieving Message: {"cmd":"209","args":{"type":1,"messages":[{...},{...},{...},{..{...},{�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������...

[編輯]我創建了一個稱為InputReader的類,該類實現了Runnable,該類內部具有run方法和while條件:

private class InputReader implements Runnable {
    private DarkStarSocketClient main;
    private InputStream in;
    private boolean disconnected = false;

    public InputReader(DarkStarSocketClient main, InputStream in) {
        this.main = main;
        this.in = in;
    }

    public void run() {
        while (!disconnected) {
            try {

                byte hi = (byte) in.read();
                if (hi == -1) {
                    main.disconnect();
                    MessageBuffer dummy = new MessageBuffer(new byte[0]);
                    postRequest(dummy);
                    break;
                }
                byte lo = (byte) in.read();
                int len = ((hi & 255) << 8) + (lo & 255) << 0;

                byte[] msgBytes = new byte[len];

                in.read(msgBytes);
                MessageBuffer buf = new MessageBuffer(msgBytes);
                handleApplicationMessage(buf);

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

這是handleApplicationMessage函數的實現:

private void handleApplicationMessage(MessageBuffer msg) throws Exception {
    byte command = msg.getByte();

    switch (command) {
        case SimpleSgsProtocol.LOGIN_SUCCESS:
            reconnectKey = msg.getBytes(msg.limit() - msg.position());
            loggedIn = true;
            responseHandler.loggedIn();
            break;
        case SimpleSgsProtocol.LOGIN_FAILURE: {
            String reason = msg.getString();
            responseHandler.loginFailed(reason);
            break;
        }
        case SimpleSgsProtocol.LOGIN_REDIRECT: {
            String host = msg.getString();
            int port = msg.getInt();
           break;
        }
        case SimpleSgsProtocol.SESSION_MESSAGE: {
            checkLoggedIn();
            byte[] msgBytes = msg.getBytes(msg.limit() - msg.position());
            responseHandler.receivedSessionMessage(msgBytes);
            break;
        }
        case SimpleSgsProtocol.RECONNECT_SUCCESS:
            loggedIn = true;
            reconnectKey = msg.getBytes(msg.limit() - msg.position());
            responseHandler.reconnected();
            break;
        case SimpleSgsProtocol.RECONNECT_FAILURE:
            String reason = msg.getString();
            this.disconnect();
            break;
        case SimpleSgsProtocol.LOGOUT_SUCCESS:
            loggedIn = false;
            break;
        default:
            throw new IOException("Unknown session opcode: " + command);
    }
}

您很可能在代碼中存在錯誤。

最常見的錯誤是在調用read時忽略了實際讀取的數據長度。 例如

for(int length; (length = in.read(buffer)) > 0; ) {
    // you must use the actual length and not assume the buffer is always full.

如果忽略該長度,則假設只有正確的開始時,整個緩沖區才具有有用的數據。


編輯:正如我猜您正在忽略實際讀取的長度。 不管您給它提供多少byte [],最小值都為1。

in.read(msgBytes);

應該

int length = in.read(msgBytes);
// keep reading until you have everything.

如果使用DataOutputStream和DataInputStream,您的生活會簡單很多

 private final DataInputStream in;

 try {
    while (!disconnected) {
        int length = in.readShort() & 0xFFFF;
        byte[] msgBytes = new byte[length];
        in.readFully(msgBytes); // keeps reading until the whole buffer is read.
        try {
            MessageBuffer buf = new MessageBuffer(msgBytes);
            handleApplicationMessage(buf);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 } catch (EOFException expected) {

 } catch (Throwable t) {
    System.err.println("Fatal error");
    t.printStackTrace();
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM