0

クライアント サーバーの Websocket 接続のハンドシェイク ステージをなんとか通過しましたが、サーバーからクライアントへのデータ送信に問題が発生しました。データを送信するコードは次のとおりです。

public void Send(String s)throws IOException{

os = socket.getOutputStream();

byte[] rawData = s.getBytes("UTF-8");

int frameCount  = 0;
byte[] frame = new byte[10];

frame[0] = (byte) 129;

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

int bLength = frameCount + rawData.length;

byte[] reply = new byte[bLength];

int bLim = 0;
for(int i=0; i<frameCount;i++){
    reply[bLim] = frame[i];
    bLim++;
}
for(int i=0; i<rawData.length;i++){
    reply[bLim] = rawData[i];
    bLim++;
}
System.out.println(frame);
os.write(reply);
os.flush();
}

これは、文字列 s をクライアントに送信することになっています。9 バイトのデータを送信できますが、echo websocket Web サイトでテスト クライアントによって受信されず、10 番目のバイトを送信すると、クライアントは「エラー: 不明」と言って切断します。私が間違っていることは何ですか?前もって感謝します!スモレット。

4

0 に答える 0