2017-09-25 85 views
-3

爲什麼java顯示這個輸出爲每個端口連接是否需要像java基本框架或包以外的其他東西這些。我在基本的服務器客戶端程序工作第一是客戶端和第二是server.I嘗試基本代碼只有connection.but它顯示了這個輸出爲什麼它告訴我連接沒有建立?

import java.io.*; 
      import java.net.*; 
      class DateClient 
      { 
       public static void main(String args[]) throws Exception 
       { 
        Socket soc=new Socket(InetAddress.getLocalHost(),5217);   
        BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream() )); 
        System.out.println(in.readLine()); 
       }  
      } 




import java.net.*; 
      import java.io.*; 
      import java.util.*; 
      class DateServer 
      { 
       public static void main(String args[]) throws Exception 
       { 
        ServerSocket s=new ServerSocket(5217); 
        while(true) 
        { 
         System.out.println("Waiting For Connection ..."); 
         Socket soc=s.accept(); 
         DataOutputStream out=new DataOutputStream(soc.getOutputStream()); 
         out.writeBytes("Server Date: " + (new Date()).toString() + "\n"); 
         out.close(); 
         soc.close(); 
        } 
       } 
      } 


     THIS IS OUTPUT 

     output:= 
       Exception in thread "main" java.net.ConnectException: Connection timed out: conn 
       ect 
         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 DateClient.main(DateClient.java:8) 

回答

0

您正在使用的DNS解析,以獲得本地主機每一次,你會遇到連接到它時,超時,所以:

  • 要麼你DNS解析器配置爲關閉或被防火牆過濾的主機,

  • 或DNS解析程序將本地主機名解析爲不是您的服務器之一併且已關閉或由防火牆過濾的IP。

這些是你可能有這樣的超時,而不是主機不可達或端口不可達錯誤的兩種主要情況。這個一個

Socket soc = new Socket(InetAddress.getLocalHost(),5217); 

所以,更換線路

Socket soc = new Socket(InetAddress.getLoopbackAddress(), 5217); 

解決您的問題。

相關問題