L
Leo
Hi!
I am doing an assignment that requires something like a "Proxy" that
sits between client and server. However, before I can start
programming functions of the "Proxy", I encounter the problem that the
middleman ("Proxy") can not receive response from the web server. Here
is the simple test program that just do the job of receiving request
from client, sending the request to the web server, reading the
response from the web server and finally sending the response to the
client.
import java.net.*;
import java.io.*;
public final class RequestDiffTest
{
final static String CRLF = "\r\n";
public static void main(String argv[]) throws Exception
{
int port;
// Set the port number.
if (argv.length == 0) {
port = 6789; // If no port number is specified, use the default
port 6789.
System.out.println("Server Port Number: " + port);
} else {
port = Integer.parseInt(argv[0]); // Otherwise, use the port
number specified by the user
System.out.println("Server Port Number: " + port);
}
// Establish the listen socket.
ServerSocket listenSocket = new ServerSocket(port);
// Process HTTP service requests in an infinite loop.
while (true) {
// Listen for a TCP connection request.
Socket connectionSocket = listenSocket.accept();
// Establish a TCP connection to the server.
Socket serverConnectionSocket = new Socket("localhost", 8000);
// Read the request message from client.
BufferedReader inputStreamFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
String requestMessage = "";
String messageLine = "";
while ((messageLine = inputStreamFromClient.readLine()).length() !=
0) {
requestMessage += messageLine + CRLF;
}
requestMessage += CRLF;
System.out.println("1. Read Client Request Message:\n" +
requestMessage);
// Send the request message to server.
DataOutputStream outputStreamToServer = new
DataOutputStream(serverConnectionSocket.getOutputStream());
outputStreamToServer.writeBytes(requestMessage);
System.out.println("2. Request Message Sent.");
// SOMETHING WRONG HERE: CAN NOT READ RESPONSE.
// Read the response from the server and send
it to the client.
InputStream inputStreamFromServer =
serverConnectionSocket.getInputStream();
OutputStream outputStreamToClient =
connectionSocket.getOutputStream();
byte[] buff = new byte[1024];
int n = 0;
System.out.println("3. Reading Response From Server.");
while ((n = inputStreamFromServer.read(buff)) != -1) {
System.out.write(buff, 0, n);
outputStreamToClient.write(buff, 0, n);
}
System.out.println("\n4. Successfully Handle The Request.");
inputStreamFromClient.close();
outputStreamToServer.close();
inputStreamFromServer.close();
outputStreamToServer.close();
serverConnectionSocket.close();
connectionSocket.close();
}
}
}
When the program tries to receive response from the web server, the
JVM always say that "Software causes connection to abort: recv fail".
I am sure the web server has already sent the response and I don't
know why the program can't receive response from the server. Could
anyone kindly help me to solve this problem. Thanks in advance!
I am doing an assignment that requires something like a "Proxy" that
sits between client and server. However, before I can start
programming functions of the "Proxy", I encounter the problem that the
middleman ("Proxy") can not receive response from the web server. Here
is the simple test program that just do the job of receiving request
from client, sending the request to the web server, reading the
response from the web server and finally sending the response to the
client.
import java.net.*;
import java.io.*;
public final class RequestDiffTest
{
final static String CRLF = "\r\n";
public static void main(String argv[]) throws Exception
{
int port;
// Set the port number.
if (argv.length == 0) {
port = 6789; // If no port number is specified, use the default
port 6789.
System.out.println("Server Port Number: " + port);
} else {
port = Integer.parseInt(argv[0]); // Otherwise, use the port
number specified by the user
System.out.println("Server Port Number: " + port);
}
// Establish the listen socket.
ServerSocket listenSocket = new ServerSocket(port);
// Process HTTP service requests in an infinite loop.
while (true) {
// Listen for a TCP connection request.
Socket connectionSocket = listenSocket.accept();
// Establish a TCP connection to the server.
Socket serverConnectionSocket = new Socket("localhost", 8000);
// Read the request message from client.
BufferedReader inputStreamFromClient = new BufferedReader(new
InputStreamReader(connectionSocket.getInputStream()));
String requestMessage = "";
String messageLine = "";
while ((messageLine = inputStreamFromClient.readLine()).length() !=
0) {
requestMessage += messageLine + CRLF;
}
requestMessage += CRLF;
System.out.println("1. Read Client Request Message:\n" +
requestMessage);
// Send the request message to server.
DataOutputStream outputStreamToServer = new
DataOutputStream(serverConnectionSocket.getOutputStream());
outputStreamToServer.writeBytes(requestMessage);
System.out.println("2. Request Message Sent.");
// SOMETHING WRONG HERE: CAN NOT READ RESPONSE.
// Read the response from the server and send
it to the client.
InputStream inputStreamFromServer =
serverConnectionSocket.getInputStream();
OutputStream outputStreamToClient =
connectionSocket.getOutputStream();
byte[] buff = new byte[1024];
int n = 0;
System.out.println("3. Reading Response From Server.");
while ((n = inputStreamFromServer.read(buff)) != -1) {
System.out.write(buff, 0, n);
outputStreamToClient.write(buff, 0, n);
}
System.out.println("\n4. Successfully Handle The Request.");
inputStreamFromClient.close();
outputStreamToServer.close();
inputStreamFromServer.close();
outputStreamToServer.close();
serverConnectionSocket.close();
connectionSocket.close();
}
}
}
When the program tries to receive response from the web server, the
JVM always say that "Software causes connection to abort: recv fail".
I am sure the web server has already sent the response and I don't
know why the program can't receive response from the server. Could
anyone kindly help me to solve this problem. Thanks in advance!