2012-10-05 175 views
0

我用一個簡單的程序來連接到服務器:連接拒絕與Java套接字?

這裏是:

public static void main(String[] args) throws Exception { 
     String sentence; 
     String modsent; 

     //input Stream 
     BufferedReader fromclient = new BufferedReader(new InputStreamReader(System.in)); 
     //client Socket 
     Socket clientsock = new Socket("stockholm.serverdhse2.se", 9000); 
     //output Stream attached to socket 
     DataOutputStream serverout = new DataOutputStream(clientsock.getOutputStream()); 
     //input from Server 
     BufferedReader fromserver = new BufferedReader(new InputStreamReader(clientsock.getInputStream())); 

     sentence = fromclient.readLine(); 

     serverout.writeBytes(sentence + "\n"); 

     modsent = fromserver.readLine(); 

     System.out.println("From Server: " + modsent); 

     clientsock.close(); 
    } 

} 

但現在我得到異常:

Exception in thread "main" java.net.ConnectException: Connection refused: connect 
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) 
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) 
    at java.net.PlainSocketImpl.connect(Unknown Source) 
    at java.net.SocksSocketImpl.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.connect(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at java.net.Socket.<init>(Unknown Source) 
    at register.main(register.java:22) 

可能是什麼問題呢?

+1

你有服務器在'「stockholm.serverdhse2.se」,9000' –

+0

我在想同樣的路線。這臺服務器是否活躍? – Less

+0

是的!該服務器肯定存在,並已上漲!我的另一個問題是:如何通過套接字發送字符串到服務器? – maximus

回答

1

也許它只是形式到達網絡中的一個範圍內的DNS服務器,使用nmap檢查:

的nmap -A stockholm.serverdhse2.se -p 9000

至極給我:

失敗解決給定的主機名/ IP:stockholm.serverdhse2.se。

爲了確保它的DNS問題,嘗試與IP。 如果連接成功與IP你得到它:「dns問題」