日期:2014-05-20  浏览次数:20641 次

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(),接收客户端发送的消息,可是我在你的代码中没有看到这个过程。