一般来说 port 不正确的话 服务器端就不会有相应的服务 也不会返回正确的信息
如果端口没开的话就更本连不上 这时会有一个 timeout 的 例外所以你只要看能不能建立连接 之后再检查一下服务器返回的信息是否正确就行了

解决方案 »

  1.   

    telenths(_非法操作_) :1.什么时候会有一个 timeout 的 例外?
    2.下面的写法对吗?
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private String response;try {
    socket = new Socket(host, port);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
    response = in.readLine();
    if (response.startsWith("+OK")){
    //connect ok
    }
    else{
    //the host error
    }
    } catch(UnknownHostException ex) {
    //the port error
    }请教具体的实现方法.
      

  2.   

    socket = new Socket(host, port);
    之后可以加上这么一句
    socket.setSoTimeout(2000);
    //等待的时间为 2 秒 超过时间如果还没有建立好连接 这个函数就会抛出一个SocketTimeoutException     try {
          socket = new Socket("127.0.0.1", 20);
          socket.setSoTimeout(2000);
          in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
          response = in.readLine();
          if (response.startsWith("+OK")){
          //connect ok
          }
          else{
          //the host error
          }
        }catch(UnknownHostException e) {
          //the Host error
        }catch(SocketTimeoutException e){
          //the host error
        }catch(SocketException e){    }catch(IOException e){    }
      

  3.   

    更正:
        catch(SocketTimeoutException e){
          //the host error  --> port Err
        }
      

  4.   

    telenths(_非法操作_) :
    SocketTimeoutException类不存在.我用的是jdk131_03版本.
    好象这个类只在1.4版本存在?
      

  5.   

    那就 catch java.net.SocketException 吧
    一样的SocketTimeoutException 是 1.4 扩展的
    他们都继承了 java.io.IOException
      

  6.   

    实际上 setSoTimeout 是这样定义的
    setSoTimeout
    public void setSoTimeout(int timeout)
                      throws SocketException
    Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.他应该抛出一个 SocketException 不过注意 后面的说明里提到了 "a java.net.SocketTimeoutException is raised" 说明 SocketTimeoutException 也是可用的
    不过 SocketTimeoutException 确实是 1.4 新特性 在 1.3 下只好用 SocketException 了
      

  7.   

    非常感谢!好象是可以了.
    但是如果一个Server上的25端口和110端口都打开的情况下,pop3端口检查就只能检查有没有端口了,而不能知道端口是否是正确的pop3端口了.
      

  8.   

    不是吧
    还可以通过服务器的响应来判断
    如果服务器返回的是 +OK 那么我认为没有理由认为它不是 POP3
      

  9.   

    非常感谢!我现在这样做的.
    如果host和port都正确(即使port并不是pop3的端口,可能其他服务也存在的端口),但是服务器返回的不是 +OK 的话,那么错误类型就是other error ,就不用去判断是什么错了.因为有可能是端口错误,也有可能是host上根本就没有pop3服务.当然也可以理解为port错误,这只是报错误类型的问题.   try {
          socket = new Socket(host, port);
          socket.setSoTimeout(2000);
          in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
          response = in.readLine();
          if (response.startsWith("+OK")){
          //connect ok
          }
          else{
          //the other error
          }
        }catch(UnknownHostException e) {
          //the host error
        }catch(SocketException e){
          //the port error
        }catch(IOException e){
          //the other error
        }