求助java中Socket问题,急。。。。!

没事眯一会 2012-08-10 12:59:39
和另一台机子做数据交换。代码如下
	public String send(String SendMessage,String clientIP,int clientPort) throws UnknownHostException, IOException{
System.out.println("------------clientIP="+clientIP+"-------------clientPort="+clientPort);
Socket s = new Socket(clientIP,clientPort);
System.out.println("s.getOutputStream()"+s.getOutputStream());

PrintStream out = new PrintStream(s.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
InputStream is = s.getInputStream();
System.out.println("SendMessage:"+SendMessage);
out.print(SendMessage);

int b = 0;
while ((b = is.read()) != -1) {
System.out.println(b);
bos.write(b);
System.out.println(b);
}
byte[] bytes = bos.toByteArray();*/

int len = -1;
while((len=is.available()) <=8) {
len = is.available();
System.out.println("len:"+len);
}

byte[] bytes = new byte[8192];
is.read(bytes, 0 , bytes.length);
String ReceiveMessage = StringUtil.trim(new String( bytes));

System.out.println("ReceiveMessage:"+ReceiveMessage);
out.close();
s.close();
is.close();
return ReceiveMessage;
}



为什么老是在while((len=is.available()) <=8) {
len = is.available();
System.out.println("len:"+len);
}
中循环,后天总是打印len=0;端口 ip都正确,求解!
...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr_sunboy 2012-08-10
  • 打赏
  • 举报
回复
返回此输入流方法的下一个调用方可以不受阻塞地从此输入流读取(或跳过)的字节数。
gyt_cw 2012-08-10
  • 打赏
  • 举报
回复
api文档是这么写的:
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream。
brightyq 2012-08-10
  • 打赏
  • 举报
回复
类 InputStream 的 available 方法总是返回 0。

详细api文档。

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧