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

socket连接问题
请问大侠们,我在网上找了一个whois的简单程序,但是运行起来总是报错是java.net.ConnectException:   Connection   timed   out:   connect
源程序如下:
import   java.net.*;
import   java.io.*;

public   class   whois
{
public   final   static   int   port   =   43;
public   final   static   String   hostname   =   "10.0.0.51 ";

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( "10.0.0.51 ",   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;
}
}
}
}
根本没有显示System.out.println( "Socket   connected! ");的结果,请大家帮帮忙,是怎么回事啊?不是一个很简单的程序吗??


------解决方案--------------------
10.0.0.51没有这个IP吧?.......不能连接啊....
------解决方案--------------------
java.net.ConnectException: Connection timed out: connect
连接超时
1.确认服务器端正常运行了,并且43端口已经被打开
2.确认IP地址,10开头地址一般都是不用的?你怎么会用到这个地址的??
本机访问127.0.0.1或者LOCALHOST就行