[英]Java Mail Error : java.net.ConnectException: Connection refused: connect
[英]Java Networking Problem: java.net.ConnectException: Connection refused: connect
當我運行簡單的網絡程序時,我無法在Java中進行任何聯網我無法獲得連接,即使在使用本地主機時,錯誤:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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 EchoClient.main(EchoClient.java:8)
這是該計划:
import java.io.*;
import java.net.*;
import java.util.*;
public class EchoClient{
public static void main(String[] args) {
try{
Socket client = new Socket(InetAddress.getLocalHost(), 1234);
InputStream clientIn = client.getInputStream();
OutputStream clientOut = client.getOutputStream();
PrintWriter pw = new PrintWriter(clientOut);
BufferedReader br = new BufferedReader(new InputStreamReader(clientIn));
Scanner stdIn = new Scanner(System.in);
System.out.println("Input Message:");
pw.println(stdIn.nextLine());
System.out.println("Recieved Message:");
System.out.println(br.readLine());
pw.close();
br.close();
client.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
我使用Windows 7,我已經轉向Windows防火牆,我沒有反病毒。
正如我在評論中所寫,檢查服務器(類似EchoServer?)是否正常運行。
但是,當您成功連接時還有其他問題。 pw.println(stdIn.nextLine());
可能不會將內容發送到服務器,你需要做一個pw.flush();
要真正發送內容,您可以使用autoflushing創建PrintWriter
:
pw = new PrintWriter(clientOut, true);
如果你需要一個EchoServer
我剛剛寫了一個與你的客戶端一起工作的,如果你添加我上面描述的刷新 :
public class EchoServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1234);
while (true) {
// accept the connection
Socket s = ss.accept();
try {
Scanner in = new Scanner(s.getInputStream());
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
// read a line from the client and echo it back
String line;
while ((line = in.nextLine()) != null)
out.println(line);
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}
}
您可以使用telnet localhost 1234
進行嘗試。
仔細檢查您的本地EchoServer是否已在端口1234上啟動並運行。如果該端口上沒有服務器正在運行,您將無法連接。
我在Windows 7中的解決方案是打開“簡單TCP / IP服務”。 它在這里描述: http : //www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.