0

I'm trying to write a simple proxy server, and I found this code online and I just want to try to run it see if it works, but when it creates a new socket, it got an ConnectException that says Connect refused. I'm using 'localhost' as the host and I tried several different ports but nothing works. What's the problem here, is it the code or my machine?

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) {
            }
        }
    }

2 Answers2

0

'Connection refused' means exactly one thing: nothing was listening at the IP:port you tried to connect to. So it was wrong. The solution is to get it right, or to start the server if it hadn't been started.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I'm using localhost, so I don't know what you mean by 'start the server if it hadn't been started'. What exactly do I need to do? – Shawn Zhong Jan 31 '16 at 17:37
  • How would I know? It's your server. I don't find anything unclear about the text you quoted. – user207421 Feb 01 '16 at 05:36
-1

You need to set timeout while reading data from socket, there is an example available here

Community
  • 1
  • 1
Abhijeet
  • 8,561
  • 5
  • 70
  • 76
  • Rubbish. You can't even set a timeout, let alone read anything from a socket, let alone get a timeout exception, until you have a socket, and OP doesn't have a socket: he has 'connection refused'. The answer you cite is (a) about UDP and (b) wrong. – user207421 Feb 01 '16 at 08:59