0

Java:1.7操作系统:Linux(但我不知道它是哪个Linux)

我有一个 CharBuffer 来包含我从套接字的 BufferedReader 读取的内容。Socket 是肯定建立的,当我试图转储它时,它给了我一个ERROR_FORMAT结果。

之后,我从套接字读取数据但导致java.net.SocketException: Connection reset

连接重置问题是我要解决的问题,但是 CharBuffer.toString() 怎么会给出ERROR_FORMAT结果

下面是我的代码,有什么问题吗?

Socket connectionSocket=xxxxxx;  //Connected socket given by other library

connectionSocket.setSoTimeout(75*1000);
CharBuffer charBuffer=CharBuffer.allocate(1024);
BufferedReader bufferedReader=null;

bufferedReader=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream(), "BIG5"));

while((bufferedReader.read(charBuffer))!=-1)  // in 2nd time, this line throw a java.net.SocketException: Connection reset
{
  charBuffer.flip();
  respStr.append(charBuffer);
  log.info("CharBuffer: "+charBuffer.toString());  // this line runs just 1 tine, print result: "CharBuffer: 2016102618353211301 : ERROR_FORMAT"
  charBuffer.clear();
}
log.info("CharBuffer all: "+charBuffer.toString());  // dodn't been execute
4

1 回答 1

0

我无法复制您的问题

public static void main(String[] args) throws IOException {
    StringBuilder respStr = new StringBuilder();
    CharBuffer charBuffer=CharBuffer.allocate(1024);
    BufferedReader bufferedReader=null;
    Logger log = Logger.getAnonymousLogger();

    // create a byte stream with some big 5 characters in it
    // to simulate what a socket should produce
    byte[] bytes = new byte[100];
    int index = 0;
    for(int i = 0xc940;i<0xc940+50;i++){
        bytes[index++] = (byte)((i & 0x0ff00)>>8);
        bytes[index++] = (byte)((i & 0x0ff));
    }
    InputStream byteStream = new ByteArrayInputStream(bytes);

    // code to process the byte stream
    bufferedReader=new BufferedReader(new InputStreamReader(byteStream, "BIG5"));
    while((bufferedReader.read(charBuffer))!=-1){
        charBuffer.flip();
        respStr.append(charBuffer);
        log.info("CharBuffer: "+charBuffer.toString());
        charBuffer.clear();
    }
    log.info("CharBuffer all: "+charBuffer.toString());
}

2016 年 10 月 27 日下午 6:27:18 nz.test.buffers.CharBufferTest main

INFO: CharBuffer: <中文字符>

2016 年 10 月 27 日下午 6:27:18 nz.test.buffers.CharBufferTest main

INFO: CharBuffer all: <中文字符>

于 2016-10-27T05:29:39.260 回答