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

socket链接问题!!急急。。。
我运行一个简单的whois程序,就是从whois网站查询输入的地址是否被注册过,但是为什么运行后总是出现超时的问题呢,这个现象是指已经链接上了吗?还是根本就没有链接上?那个网站我试过,是可以使用的网站,我觉得很奇怪啊,很简单的为什么不通呢,帮帮我吧,谢谢了

import   java.net.*;
import   java.io.*;

public   class   whois
{
public   final   static   int   port   =   43;
public   final   static   String   hostname   =   "218.30.103.154 ";
public   static   void   main(String[]   args)
{
Socket   theSocket;
DataInputStream   theWhoisStream;
PrintStream   ps;

//检查命令行参数
if   (args.length   <1)
{
System.out.println( "\nUsage:   java   whois   <command> ");
System.out.println( "Parameters: ");
System.out.println(
"\tcommand   =   one   or   more   Domain   name,   or   other   command. ");
System.out.println( "Example: ");
System.out.println( "\tjava   whois   sohu.com ");
System.out.println( "\tjava   whois   help ");

System.exit(1);   //退出
}
while(true)
{
try   {

//在TCP服务端口43(十进制)连接SRI-NIC服务主机
theSocket   =   new   Socket( "218.30.103.154 ",   port);
System.out.println( "Socket   connected! ");
ps   =   new   PrintStream(theSocket.getOutputStream());
//发送用户提供的一个或多个命令
for   (int   i   =   0;   i   <   args.length;   i++)
ps.print(args[i]   +   "   ");
//以回车和换行( <CRLF> )结尾
ps.print( "\r\n ");

//接受相应命令的返回信息
theWhoisStream   =   new   DataInputStream(theSocket.getInputStream());
String   s;
while   ((s   =   theWhoisStream.readLine())   !=   null)   {
System.out.println(s);
}

//关闭DataInputStream和PrintWriter
theWhoisStream.close();
ps.close();
//关闭socket
theSocket.close();
break;
}
catch   (IOException   e)   {
System.err.println(e);
continue;
}
}
}
}


------解决方案--------------------
说明一个问题,你说如的IP和PORT你不能访问
------解决方案--------------------
端口有没有被占用,或者别的原因呢
------解决方案--------------------
theSocket都没有accept(),怎么可能连上
------解决方案--------------------
支持楼上的,accept,监听连接请求