首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

socket服务器端收发来的字符串时有的收不到.为什么?解决思路

2012-01-23 
socket服务器端收发来的字符串时有的收不到.为什么?BufferedReaderinBuffernulltry{//从客户端读取数据w

socket服务器端收发来的字符串时有的收不到.为什么?
BufferedReader   inBuffer=null;
try   {
//从客户端读取数据
while   (FActive)   {
    inBuffer   =   new   BufferedReader(new   InputStreamReader(FSocket
                    .getInputStream()));

    System.out.print(FSocket.getInetAddress()+ ": "+FSocket.getPort()+ "     ");
    System.out.println(inBuffer.readLine());

}
inBuffer.close();
}   catch   (IOException   e)   {
    e.printStackTrace();
    System.out.println(e);
}
}

客户端发送数据
while   (true)   {
    line   =   new   BufferedReader(new   InputStreamReader(System.in));
    if   (null   !=   line.readLine())   {
          out.println(line.readLine());
        if   (line.readLine().equals( "quit "))   {
System.out.println( "退出 ");
break;
      }
}
}

本来应该是客户端输入,然后后回车,服务器端就收到,可现在是客户端输入多次,多次回车后服务器端才能收到一个。

为什么?????


[解决办法]
不合适的地方很多,指出一处错误:
if (null != line.readLine()) {
out.println(line.readLine());
if (line.readLine().equals( "quit ")) {
应该这样:
String str = line.readLine();
out.println(str);
if (str.equals( "quit ")) {
[解决办法]
FActive,这个什么用处?

服务器端socket对象应该一直处于阻塞状态,accept(),接收客户端发送的消息,可是我在你的代码中没有看到这个过程。

热点排行
Bad Request.