0

Hello everyone!!!!!!!!!! my proxy/server receives requests from a client in this form:

GET mhttp://proxy_ip:proxy_port/file.mhtml\n
\n

and here's my code:

import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ProxyMain {

public static void main(String argv[]) {

    int proxyPort = 55554;
    String proxyAddr = "127.0.0.1";
    ServerSocket proxySocket = null;

    try {
        proxySocket = new ServerSocket(proxyPort, 50,  InetAddress.getByName("127.0.0.1"));
    }

    catch (Exception e) {
        System.err.println("Impossible to create socket server!");
        System.out.flush();
        System.exit(1);
    }

    System.out.printf("Server active on port: %d and on address %s\n", proxyPort, proxySocket.getInetAddress());

    while (true) {
        Socket client = null;
        BufferedReader in = null;
        PrintWriter out = null;
        String request = new String();
        String tmp = new String();


        try {
            client = proxySocket.accept();
            System.out.println("Connected to: ");
            System.out.println(client.getInetAddress().toString());
            System.out.printf("On port %d\n", client.getPort());
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);

        }

        /*catch (IOException e) {
            System.err.println("Couldn't get I/O for connection accepted");
            System.exit(1);
        }*/

        catch (Exception e) {
            System.out.println("Error occurred!");
            System.exit(1);
        }

        System.out.println("Received request:");

        try{
            #####################################
                            while ((tmp = in.readLine()) != null)
            System.out.println(tmp);
            request = request + tmp;
                            #####################################
        }
        catch (IOException ioe) {
            System.err.println("Impossible to read mhttp request!");
            System.exit(1);
        }

        System.out.println(request);
    }

}

}

I have a problem in block delimited with #########. I don't know how to stop the method in.readLine(). First it reads: GET mhttp://proxy_ip:proxy_port/file.mhtml\n then it reads \n but then it blocks, still waiting to read but the request is finished. I think that the client keeps the connection alive, even after sending the request but I can't change this because it's my teacher's software. How can I solve this?

3
  • 1
    Have a look at: docs.oracle.com/javase/1.4.2/docs/api/java/io/… Commented Feb 25, 2013 at 14:00
  • 1
    Why do you read in a loop if you know you'll only ever need to read 2 lines? Just execute 2 readLine() calls...? Commented Feb 25, 2013 at 14:04
  • I was thinking it would be better to perform a general purpouse proxy...in order to accept any kind of request Commented Feb 25, 2013 at 14:18

1 Answer 1

1

don't read an entire line, read a single byte until you encounter a '\n' or perform 2 readlines as Greg suggests. in any case, since you know the exact format of the incoming message, adjust your code to read accordingly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.