这个问题真的很着急,请大家看看
框架上决定了我只能用一个客户端socket来连server
当我把server 关掉以后,socket 的 read 就出错了,我抓到了错误于是我就 connect(地址),变态的它竟然报错 socket is already connected;
于我就先close(),再connect,它竟然报错 socket is closed;
      while (true) {
        char[] ret = new char[10000];
        try {
          switch(m_ReadMode)
          {
              case CProvConstant.READMODE_GENERAL:
              GenerRead(ret,rd);
              break;
              case CProvConstant.READMODE_SMSC://CCM(SMSC)
              SMSCRead(ret,datais,rd);
          }        }
        catch (SocketTimeoutException e) {
          //String msg = e.getMessage();
          //  ProvisioningMediator.Logger.Warnning(e.getMessage());
        }
        catch(IOException ex)
        {
           m_socket.m_bConnectd=false;
           m_socket.isLogin=false;            //m_socket.close(); //这里加close,后面那段代码连接时报isclosed 不加,后面报 is already connected 
           throw ex;
        }======================另一段代码==================                        ProvisioningMediator.Logger.Debug("EnsureConnection");
                        if (socket.isConnected()== true) //
                        {
                                ProvisioningMediator.Logger.Debug("socket is connect");
                                return ;
                        }
                        //else if(socket.isClosed() ){  // close
                        // socket.isLogin = false;
                        // return ;
                        //}
                        else{ // no connected
                            Object[] params = new Object[]{ new String(m_sIP), new Integer(m_sPORT)};
                            String msg = MessageFormat.format("Connecting to: {0}, {1}", params);
                                ProvisioningMediator.Logger.Info(msg);
                                InetAddress addr = InetAddress.getByName(m_sIP);
                                SocketAddress sockaddr = new InetSocketAddress(addr, m_sPORT);
                                socket.connect(sockaddr);  //nnd就是这里报错
                                ProvisioningMediator.Logger.Debug("Connected OK");
                                if(ListenFirst)
                                {
                                        ProvisioningMediator.Logger.Debug("Try to listen first");
                                        ReadSocketData(socket);
                                }
                                socket.m_bConnectd=true;
                                socket.isLogin = false;
                                return ;
                        }

解决方案 »

  1.   

    当我把server 关掉以后,socket 的 read 就出错了,我抓到了错误.
    ------------------
    我理解的意思不知道是不是对的.你把server端的socket close???
    关了以后,客户端怎么可能继续连接哦???服务器都关了,你还能访问?
    另外关于socket,你close后,就不能重新连接了,socket这个实例就已经作废.
    close
    public void close()
               throws IOExceptionCloses this socket. 
    Any thread currently blocked in an I/O operation upon this socket will throw a SocketException. Once a socket has been closed, it is not available for further networking use 
    ------------------------------------------------------------------------------
    (i.e. can't be reconnected or rebound). A new socket needs to be created. If this socket has an associated channel then the channel is closed as well. 
    Throws: 
    IOException - if an I/O error occurs when closing this socket.
    See Also:
    isClosed()
      

  2.   

    socket close后,就不能再使用了,你需要重新构造一个 socket
      

  3.   

    如果我不close,为什么它还是不行呢?