作者darkk6 (Mr. Pan)
看板java
标题Re: [问题] Client接收String的问题
时间Mon Sep 12 00:48:55 2011
基本上很难知道收几次可以收完,一般来说应该是不会要求到这个部分
就算这样用:
byte[] arr=new byte[1000];
input.read(arr,0,arr.length);
也不能保证每次都会收到 1000 个 byte
所以重点应该在收到资料要怎麽处理的部分吧
! ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
! String []hh = new String[6]; //demo 长度给少一点
hh[0]="String1"; hh[1]="String2";
hh[2]="String3"; hh[3]="String4";
hh[4]="String5"; hh[5]="String6";
- for(int i=0;i<=5;i++){
- String outData = hh[i];
- byte [] outByte = outData.getBytes();
- out.write(outByte , 0 , outByte.length);
- out.flush();
- System.out.println(outData);
- }
+ out.writeObject(hh);
+ out.close();
! ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
+ String[] strArray=(String[])(in.readObject());
! for(int i=0;i<=strArray.length;i++){
- byte[] inByte = new byte[1000];
- in.read(inByte);
- String servermsg = new String(inByte , 0 , inByte.length);
- System.out.println("Message from server : "+i);
! System.out.println(strArray[i]);
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.123.85.7
1F:推 wheredo:非常感谢<(_ _)> 09/12 10:30
2F:推 LaPass:顶问前面的! - + 是什麽意思?这是什麽用法啊? 09/12 17:00
3F:→ darkk6:+:增加这行 -:移除这行 !:有修改过 09/12 17:04
4F:→ darkk6:之前在改BBS原始码的时候,给读者看比较的标注方式而已XD 09/12 17:06
5F:→ laiis:diff ? 09/12 17:07
6F:推 LaPass:了解了 感谢解说 09/12 17:14