【问题标题】:Java new socket connection refusedJava 新的套接字连接被拒绝
【发布时间】:2016-05-08 18:31:28
【问题描述】:

我正在尝试编写一个简单的代理服务器,我在网上找到了这段代码,我只是想尝试运行它看看它是否有效,但是当它创建一个新套接字时,它得到一个 ConnectException 说 Connect denied .我使用'localhost'作为主机,我尝试了几个不同的端口,但没有任何效果。这里有什么问题,是代码还是我的机器?

public static void runServer(String host, int remotePort, int localPort) throws IOException {

    // Create the socket for listening connections
    ServerSocket mySocket = new ServerSocket(localPort);

    final byte[] request = new byte[1024];
    byte[] reply = new byte[4096];

    while (true) {
        Socket client = null, server = null;

        try {
            client = mySocket.accept();

            final InputStream streamFromClient = client.getInputStream();
            final OutputStream streamToClient = client.getOutputStream();

            try {
                server = new Socket(host, remotePort);
            } catch (IOException e) {
                PrintWriter out = new PrintWriter(streamToClient);
                out.print("Proxy server cannot connect to " + host + ":"
                + remotePort + ":\n" + e + "\n");
                out.flush();
                client.close();
                continue;
            }

            final InputStream streamFromServer = server.getInputStream();
            final OutputStream streamToServer = server.getOutputStream();

            Thread t = new Thread() {
                public void run() {
                    int bytesRead;
                    try {
                        while ((bytesRead = streamFromClient.read(request)) != -1) {
                            streamToServer.write(request, 0, bytesRead);
                            streamToServer.flush();
                        }
                    } catch (IOException e) {
                    }
                }
            };

            t.start();

            int bytesRead;
            try {
                while ((bytesRead = streamFromServer.read(reply)) != -1) {
                    streamToClient.write(reply, 0, bytesRead);
                    streamToClient.flush();
                }
            } catch (IOException e) {
            }
            streamToClient.close();

        } catch (IOException e) {
            System.err.println(e);
        } finally {
            try {
                if (server != null)
                    server.close();
                if (client != null)
                    client.close();
            } catch (IOException e) {
            }
        }
    }

【问题讨论】:

    标签: java sockets


    【解决方案1】:

    “连接被拒绝”只意味着一件事:在您尝试连接的 IP:port 上没有任何东西在监听。所以这是错误的。解决办法是把它弄好,或者如果服务器没有启动就启动它。

    【讨论】:

    • 我使用的是本地主机,所以我不知道你所说的“如果服务器没有启动就启动它”是什么意思。我到底需要做什么?
    • 我怎么知道?是你的服务器。我没有发现您引用的文字有任何不清楚的地方。
    【解决方案2】:

    从socket读取数据需要设置超时时间,有一个例子here

    【讨论】:

    • 垃圾。您甚至无法设置超时,更不用说从套接字读取任何内容,更不用说超时异常,直到您有一个套接字,并且 OP 没有套接字:他有“连接被拒绝”。您引用的答案是(a)关于 UDP 和(b)错误。
    猜你喜欢
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多