繁体   English   中英

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM