首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Exceptionjava.net.ConnectException: Connection refused: connect解决思路

2012-01-08 
Exceptionjava.net.ConnectException: Connection refused: connect我的程序如下:客户端:importjava.io.*

Exceptionjava.net.ConnectException: Connection refused: connect
我的程序如下:
客户端:
import   java.io.*;
import   java.net.*;

public   class   Client  
{
        PrintStream   streamToServer;
        BufferedReader   streamFromServer;
        Socket   toServer;
        public       Client()
        {
        connectToServer();
        }
        private   void   connectToServer()
        {
        try
        {
        String   name;
        toServer   =   new   Socket( "localHost ",1001);
        streamFromServer   =   new   BufferedReader(new   InputStreamReader

((toServer.getInputStream())));
        streamToServer   =   new   PrintStream(toServer.getOutputStream());
        System.out.print( "Enter   connection   name: ");
        BufferedReader   reader   =   new   BufferedReader(new   InputStreamReader

(System.in));
        name   =   reader.readLine();
        streamToServer.println(name);
        String   str   =   streamFromServer.readLine();
        System.out.println( "The   Server   says: "+str);
        }
        catch(Exception   e)
{
System.out.println( "Exception "+e);
      }
        }
       

public   static   void   main(String[]   args)  
{

new   Client();

}

}

服务器端:
import   java.io.*;
import   java.net.*;
public   class   Server   implements   Runnable  
{
        ServerSocket   serverSocket;
        PrintStream     streamToClient;
        BufferedReader   streamFromClient;
        Socket   fromClient;
        static   int   count   =   0;
        Thread   thread;
        public   void   Server()
        {
        try
        {
        serverSocket   =   new   ServerSocket(1001);
        }
        catch   (Exception   e)
        {
        System.out.println( "Socket   could   not   be   created "+e);
        }
        thread   =   new   Thread(this);
        thread.start();
        }
        public   void   run()
        {
        try
        {


        while(true)
        {
        fromClient   =   serverSocket.accept();
        count++;
        System.out.println( "Client   connection   no: "+count);
        streamFromClient   =   new   BufferedReader(new   InputStreamReader

((fromClient.getInputStream())));
        streamToClient   =   new   PrintStream(fromClient.getOutputStream());
        String   str   =   streamFromClient.readLine();
        System.out.println( "Client   connection   name: "+str);
        streamToClient.println( "Welcome "+str);
       
        }
                }
        catch(Exception   e)
        {
        System.out.println( "Exception "+e);
        }
        finally
        {
        try
        {
        fromClient.close();
        }
        catch(Exception   e1)
        {
        System.out.println( "Could   not   close   connection "+e1);
              }
        }
        }

public   static   void   main(String[]   args)   {
//   TODO   自动生成方法存根
                    new   Server();
}

}
执行时显示上面的结果请问是什么原因?
是因为端口不对吗?
我不知道我的默认端口应该怎么查找

[解决办法]
public void Server() --> public Server()

热点排行