一般来说 port 不正确的话 服务器端就不会有相应的服务 也不会返回正确的信息
如果端口没开的话就更本连不上 这时会有一个 timeout 的 例外所以你只要看能不能建立连接 之后再检查一下服务器返回的信息是否正确就行了
如果端口没开的话就更本连不上 这时会有一个 timeout 的 例外所以你只要看能不能建立连接 之后再检查一下服务器返回的信息是否正确就行了
解决方案 »
- icefaces问题
- hibernate3.2 Sql server 20000 sp4 报:could not initialize a collection
- 关于File的delete功能(能解决加分)
- 设备购销系统中研究解决的问题和拟采用的研究手段(途径)
- 大家帮我看看这个死锁的问题
- 急啊!hibernate问题,大家帮着看一下吧
- Tomcat部署问题,请好手指点
- 哪位仁兄对Sun Java System Application Server的群集有所研究?好像会这个的人很少
- 为什么我用eclipse的import导工程导入不近来class文件呢?
- 关于通过JNDI查找TopicConnectionFactory、Topic方式的问题?
- 有否无连接的ResultSet?
- to qqyb2000 (答案) :你把那个帖子删掉吧,和你开玩笑的,这样,你的可用分会回来的~~!!重要的是大家开心,要分干什么?
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
}请教具体的实现方法.
之后可以加上这么一句
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){ }
catch(SocketTimeoutException e){
//the host error --> port Err
}
SocketTimeoutException类不存在.我用的是jdk131_03版本.
好象这个类只在1.4版本存在?
一样的SocketTimeoutException 是 1.4 扩展的
他们都继承了 java.io.IOException
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 了
但是如果一个Server上的25端口和110端口都打开的情况下,pop3端口检查就只能检查有没有端口了,而不能知道端口是否是正确的pop3端口了.
还可以通过服务器的响应来判断
如果服务器返回的是 +OK 那么我认为没有理由认为它不是 POP3
如果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
}