[英]Java Networking Problem: java.net.ConnectException: Connection refused: connect
I am unable to do any networking in Java when i run a simple networking program i am unable to get a connection, even when using the local host, The Error: 当我运行简单的网络程序时,我无法在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)
This is the program: 这是该计划:
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();
}
}
}
I use windows 7, i have turned of windows firewall and i dont have an anti virus. 我使用Windows 7,我已经转向Windows防火墙,我没有反病毒。
As I wrote in my comment, check that the server (something like EchoServer?) is up and running. 正如我在评论中所写,检查服务器(类似EchoServer?)是否正常运行。
But there are other problems when you succeed connecting. 但是,当您成功连接时还有其他问题。 pw.println(stdIn.nextLine());
might not send the content to the server, you need to do a pw.flush();
可能不会将内容发送到服务器,你需要做一个pw.flush();
to really send the content or you can create your PrintWriter
with autoflushing: 要真正发送内容,您可以使用autoflushing创建PrintWriter
:
pw = new PrintWriter(clientOut, true);
If you need a EchoServer
I just wrote one that works with your client if you add the flushing that I described above: 如果你需要一个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();
}
}
}
}
You can try it with telnet localhost 1234
. 您可以使用telnet localhost 1234
进行尝试。
仔细检查您的本地EchoServer是否已在端口1234上启动并运行。如果该端口上没有服务器正在运行,您将无法连接。
My solution in Windows 7 was to turn on "Simple TCP/IP Services". 我在Windows 7中的解决方案是打开“简单TCP / IP服务”。 It's described here: http://www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html 它在这里描述: http : //www.windowsnetworking.com/articles_tutorials/windows-7-simple-tcpip-services-what-how.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.