我正在學習使用Java通過套接字進行的客戶端服務器通信。 首先,我使用以下代碼檢索自己機器的IP地址。簡單客戶端服務器通信
InetAddress ownIP=InetAddress.getLocalHost();
//the result being 192.168.56.1
現在我寫使用上述地址的簡單的客戶端服務器應用程序如下
public class SimpleClientServer {
public static void main(String[] args)
{
//sending "Hello World" to the server
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
clientSocket = new Socket("192.168.56.1", 16000);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
out.println("Hello World");
out.close();
in.close();
clientSocket.close();
}
catch(IOException e)
{
System.err.println("Error occured " + e);
}
}
}
結果豪爾讀取的後續。
Error occured java.net.ConnectException: Connection refused: connect
這是什麼原因。它只是錯誤的主機地址?
那麼你期望在端口16000上收聽*? – 2012-02-06 11:29:42
首先在同一臺機器上的端口16000上設置服務器套接字,然後運行相同的代碼 – Johnydep 2012-02-06 11:35:37
1)請告訴我們發生錯誤的線路 2)請顯示服務器代碼。 – 2012-02-06 11:36:15