简体   繁体   English

Java网络问题:java.net.ConnectException:连接被拒绝:连接

[英]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.

相关问题 Java Mail错误:java.net.ConnectException:连接被拒绝:connect - Java Mail Error : java.net.ConnectException: Connection refused: connect Java套接字 - java.net.ConnectException:连接被拒绝:连接 - Java sockets - java.net.ConnectException: Connection refused: connect Java 中的 ssh 异常:java.net.ConnectException:连接被拒绝:连接 - ssh Exception in Java: java.net.ConnectException: Connection refused: connect java.net.ConnectException:连接被拒绝:connect java nio - java.net.ConnectException: Connection refused: connect java nio Java java.net.ConnectException:连接被拒绝 - Java java.net.ConnectException: Connection refused java.net.ConnectException:连接被拒绝:connect openshift - java.net.ConnectException: Connection refused: connect openshift java.net.ConnectException:拒绝连接:连接HTTPS连接 - java.net.ConnectException: Connection refused: connect for HTTPS connections 获取java.net.ConnectException:连接被拒绝:在Eclipse中连接 - Getting java.net.ConnectException: Connection refused: connect in eclipse RMI(java.net.ConnectException:连接被拒绝:连接) - RMI(java.net.ConnectException: Connection refused: connect) java.net.ConnectException:连接被拒绝:用 Spring boot 连接 Solr - java.net.ConnectException: Connection refused: connect Solr with Spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM