本帖最后由 answer76 于 2010-06-05 02:56:13 编辑

解决方案 »

  1.   

    同意楼上,Socket和ServerSocket都应该close掉
      

  2.   

    尝试了一下,当send()方法用out.close()的时候,把socket关闭了,继续往下执行到message=in.readLine()的时候,由于socket已经关闭了,得不到输入流,就报错了,楼主把finally这段代码注释掉,message=in.readLine()在这里就能阻塞了,如果想关闭socket的话,可以事先约定一个结束标志,然后关闭socket~俺也是新手,不知道对不对,就是不太明白,关闭流是不是也会关闭socket这一点~
      

  3.   

    都 要关闭的, 例:
    try {  ServerSocket server = new ServerSocket(5776);  while (true) {    Socket connection = server.accept( );    try {      Writer out        = new OutputStreamWriter(connection.getOutputStream( ));      out.write("You've connected to this server. Bye-bye now.\r\n");       out.flush( );             connection.close( );   }   catch (IOException ex) {     // This tends to be a transitory error for this one connection;     // e.g. the client broke the connection early. Consequently,     // you don't want to break the loop or print an error message.     // However, you might choose to log this exception in an error log.   }   finally {     // Guarantee that sockets are closed when complete.      try {       if (connection != null) connection.close( );     }     catch (IOException ex) {}   }}catch (IOException ex) {  System.err.println(ex);}
      

  4.   


    问题是,比如client端,发送数据完成,并接收到数据,这个时候再关闭socket。那我再次输入的时候不是会出问题的么?这点还是没弄清楚,哪位帮我看看要关闭的地方,具体给我指出来行么。public void run()
    {
    in = getInputStream(s);
    out = getOutputStream(s);
    while(true)
    {
    String message = getMessage();
    try
    {
    System.out.println("send message : " + message);
    out.write(message);
    out.newLine();
    out.flush();
    String str = in.readLine();
    System.out.println("return : " + str);
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    try
    {
    s.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }
    }我把那个close加在这里,Server端还是报错。
    java.net.SocketException: socket closed
      

  5.   

    现在老问题解决了,新问题又来了。
    我现在启动了两个客户端,分别发送了一个消息给服务器端,服务器端接收到了,但是A客户端只接收到A自己发出的消息,B也只收到自己发的。我要做的应该是A发的A,B都接收的到,同理B发的任何一个接连上的客户端应该接收到。
    我觉得Server端写的应该没问题,可能是Client端接收的地方写的有点小毛病。private void send(String message)
    {
    for(Socket s: list)
    {
    BufferedWriter out = getOutputStream(s);
    try
    {
    out.write(message);
    out.newLine();
    out.flush();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    /*try
    {
    out.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }*/
    }
    }
    }send方法是向每一个正在接连上的Client发回信息。
      

  6.   

    String str = in.readLine();阻塞式方法  在包裹他的catch语句中做相应的操作,就好了
    第二个问题:看看你服务器端有没有把每个客户端都保存在arraylist容器中
    发送时有没有给每个客户端发送数据?ps:下次发代码 用工具栏那个带#的按键 这样发出来有缩进,方面看
      

  7.   


    public void run()
    {
    in = getInputStream(s);
    out = getOutputStream(s);
    while (true)
    {
    String message = getMessage();
    try
    {
    //这个函数里,先执行getMessage()从控制台输入,然后输入消息M 到out中.这个时候执行到in.readLine()
    //客户端阻塞在这等待,而server接到M后转发,in.ReadLine读到消息输出消息M,进入下一个While循环
    //在getMessage()处等待控制台输入
    //所以客户端是在getMessage()等待,只能接收控制台输入,是没办法接收到server端发的消息的.
    System.out.println("send message : " + message);
    out.write(message);
    out.newLine();
    out.flush();
    String str = in.readLine();
    System.out.println("return : " + str);
    } catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }
      

  8.   

    所以LZ要想客户端即接受控制台,又见诶手server端消息,必须再在客户端开一个接受线程.
      

  9.   

    代码太长了,给给例子参考吧, 
    http://mybeautiful.javaeye.com/blog/659317
      

  10.   


    基本同意这个观点,read, write要用两个不同的线程处理!