Question

So basically I'm writing a simple proxy that takes HTTP requests in which the method names are in lowercase, and I have to capitalize them and forward them to a server. The forwarding to server part is what I'm having trouble with, though. In my code, that is the "forwardToServer" method. I've managed to normalize the request, get the IP address for the host, but when I try to send the request to the host, I get a connection timed out exception. I'm very new to socket programming so I really don't have a clue what's going on.

package proxy;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class WebProxy {

public String requestNormalizer(String badRequest) {

    return badRequest.replace(badRequest.substring(0, badRequest.indexOf(" ")), badRequest
            .substring(0, badRequest.indexOf(" ")).toUpperCase());
}

public void requestFromClient(String badRequest) throws IOException {

    ServerSocket welcome = new ServerSocket(9001);

    while (true) {

        Socket connection = welcome.accept();

        BufferedReader clientInput = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

    }
}

public void fowrardToServer(String request) throws UnknownHostException, IOException {

    Socket client;

    if (!dnsQuery(request)[1].equals(""))
        client = new Socket((InetAddress) dnsQuery(request)[0], (int) dnsQuery(request)[1]);
    else
        client = new Socket((InetAddress) dnsQuery(request)[0], 9001);

    DataOutputStream output = new DataOutputStream(client.getOutputStream());

    BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));

    output.writeBytes(request);

    System.out.println(input.readLine());
}

public Object[] dnsQuery(String request) throws UnknownHostException {

    Object[] addressPort = new Object[2];
    String hostname = request.substring(request.indexOf("host") + 6);
    hostname = hostname.substring(0, hostname.indexOf("\r"));

    if (hostname.contains(":")) {

        hostname = hostname.substring(0, hostname.indexOf(":"));
        addressPort[1] = hostname.substring(hostname.indexOf(":"));
    }
    else
        addressPort[1] = "";

    addressPort[0] = InetAddress.getByName(hostname);

    return addressPort;
}

And here's the stack trace for the exception:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(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 proxy.WebProxy.main(WebProxy.java:99)

This is the GET request I'm using for testing purposes:

GET http://www.uga.edu/ HTTP/1.1
Host: www.uga.edu
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:18.0) Gecko/20100101
Firefox/18.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate

Thanks in advance!

Edit: The code I used to test it is pretty routine, and I'm just testing the forwardToServer method

public static void main(String[] args) throws IOException {

    WebProxy wp = new WebProxy();
    String request = "get http://www.uga.edu/ HTTP/1.1\r\nhost: www.google.com\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate";

    wp.forwardToServer(wp.requestNormalizer(request));
}
Was it helpful?

Solution

The fact that you got a "connection timed out" error coming from the socket level (not the HTTP level) means that there is some issue at the networking level that is preventing the TCP connection from being established.

  • It is NOT that there is nothing listening for a connection on the IP/port you are using ... because that would result in a "connection refused" error.

  • It is (probably) NOT that you have a bogus hostname or IP address, because that would have resulted in different messages.

  • The most likely explanation is that the connection is being blocked by firewalls on the client, the server, or somewhere in between.

Try connecting to the same host / port using a web browser ... or some command line tool that allows you to open a simple TCP/IP connection. (The telnet command is often used for this on Linux ...)


Note that the stacktrace says that you are creating a Socket object in your main method of your WebProxy class ... but you haven't shown us that code. In fact, none of the code you included is directly relevant to your current problem, according to the stacktrace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top