Re: [問題] Client接收String的問題
基本上很難知道收幾次可以收完,一般來說應該是不會要求到這個部分
就算這樣用:
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
推
09/12 10:30, , 1F
09/12 10:30, 1F
推
09/12 17:00, , 2F
09/12 17:00, 2F
→
09/12 17:04, , 3F
09/12 17:04, 3F
→
09/12 17:06, , 4F
09/12 17:06, 4F
→
09/12 17:07, , 5F
09/12 17:07, 5F
推
09/12 17:14, , 6F
09/12 17:14, 6F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):