Problem with Proxy Server Code in Java

R

roassaf

Hi,

I am trying to write a very simple Proxy Server.
When I run the code below (it's only the main part of the code.
Everything compiles)
I have a problem that the read of input stream seem to hang.
I marked the problematic line.
(while ((bytesRead = istream.read(response)) >= 0))
It never prints "inside loop" and after a few checks I found out that
the read is the problem.
Can anyone see a problem in the code?

Thanks for you help

The code:

Integer port = new Integer(_props.getProperty("proxy.port"));
ServerSocket serverSocket = new
ServerSocket(port.intValue());
Socket socket = null;
String fullRequest = "";
while(true) {

socket = serverSocket.accept();

InputStream istream = socket.getInputStream();
OutputStream ostream = socket.getOutputStream();

StringBuffer headers = new StringBuffer();
int byteRead = 0;
while (!endOfHeaders(headers) && (byteRead =
istream.read()) >= 0) {
headers.append((char) byteRead);

}


HttpRequestHeader header = new
HttpRequestHeader(headers.toString());

ostream.write(header.asciiGetBytes(false));

int bytesRead = -1;
byte[] response = new byte[4096];
System.out.println("Before loop");
//Hangs on the read right below.
The problem----->>>>> while ((bytesRead =
istream.read(response)) >= 0) {
System.out.println("Inside loop");
ostream.write(response, 0, bytesRead);
ostream.flush();
System.out.write(response, 0, bytesRead);
}
System.out.println("After loop");
socket.close();
 
S

SadRed

Hi,

I am trying to write a very simple Proxy Server.
When I run the code below (it's only the main part of the code.
Everything compiles)
I have a problem that the read of input stream seem to hang.
I marked the problematic line.
(while ((bytesRead = istream.read(response)) >= 0))
It never prints "inside loop" and after a few checks I found out that
the read is the problem.
Can anyone see a problem in the code?

Thanks for you help

The code:

Integer port = new Integer(_props.getProperty("proxy.port"));
ServerSocket serverSocket = new
ServerSocket(port.intValue());
Socket socket = null;
String fullRequest = "";
while(true) {

socket = serverSocket.accept();

InputStream istream = socket.getInputStream();
OutputStream ostream = socket.getOutputStream();

StringBuffer headers = new StringBuffer();
int byteRead = 0;
while (!endOfHeaders(headers) && (byteRead =
istream.read()) >= 0) {
headers.append((char) byteRead);

}

HttpRequestHeader header = new
HttpRequestHeader(headers.toString());

ostream.write(header.asciiGetBytes(false));

int bytesRead = -1;
byte[] response = new byte[4096];
System.out.println("Before loop");
//Hangs on the read right below.
The problem----->>>>> while ((bytesRead =
istream.read(response)) >= 0) {
System.out.println("Inside loop");
ostream.write(response, 0, bytesRead);
ostream.flush();
System.out.write(response, 0, bytesRead);
}
System.out.println("After loop");
socket.close();

We see several unknown methods here.
If you want a good and quick help, post a small demo code that is
generally compilable, runnable and could reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html

We may need to see your client code as well.
 
E

Esmond Pitt

I am trying to write a very simple Proxy Server.
When I run the code below (it's only the main part of the code.
Everything compiles)
I have a problem that the read of input stream seem to hang.

It's *supposed* to hang if there's no input. What behaviour are you
expecting?
 
R

roassaf

I am trying to write a very simple Proxy Server.
When I run the code below (it's only the main part of the code.
Everything compiles)
I have a problem that the read of input stream seem to hang.
I marked the problematic line.
(while ((bytesRead = istream.read(response)) >= 0))
It never prints "inside loop" and after a few checks I found out that
the read is the problem.
Can anyone see a problem in the code?
Thanks for you help
The code:
Integer port = new Integer(_props.getProperty("proxy.port"));
ServerSocket serverSocket = new
ServerSocket(port.intValue());
Socket socket = null;
String fullRequest = "";
while(true) {
socket = serverSocket.accept();
InputStream istream = socket.getInputStream();
OutputStream ostream = socket.getOutputStream();
StringBuffer headers = new StringBuffer();
int byteRead = 0;
while (!endOfHeaders(headers) && (byteRead =
istream.read()) >= 0) {
headers.append((char) byteRead);

HttpRequestHeader header = new
HttpRequestHeader(headers.toString());
ostream.write(header.asciiGetBytes(false));

int bytesRead = -1;
byte[] response = new byte[4096];
System.out.println("Before loop");
//Hangs on the read right below.
The problem----->>>>> while ((bytesRead =
istream.read(response)) >= 0) {
System.out.println("Inside loop");
ostream.write(response, 0, bytesRead);
ostream.flush();
System.out.write(response, 0, bytesRead);
}
System.out.println("After loop");
socket.close();

We see several unknown methods here.
If you want a good and quick help, post a small demo code that is
generally compilable, runnable and could reproduce your problem. See:http://homepage1.nifty.com/algafield/sscce.htmlandhttp://www.yoda.arachsys.com/java/newsgroups.html

We may need to see your client code as well.

This is the entire code: (it can be fully compiled)

import java.util.*;
import java.io.*;
import com.ibm.wbi.protocol.http.*;
import com.ibm.wbi.util.*;
import java.net.Socket;
import java.net.ServerSocket;

public class ProxyServer {

private WebCacheDB _cache;
private Properties _props;
/**
* Constructor.
*/
public ProxyServer() throws ProxyServerException {

try {
_props = new Properties();
_props.load(new FileInputStream("proxy.properties"));
_cache = new WebCacheDB(_props.getProperty("login"));
}
catch(Exception e) {
throw new ProxyServerException(e.getMessage());
}
}

/**
* Starts the proxy, that is, listening and serving requests.
*/
public void start() throws ProxyServerException {

try {

Integer port = new
Integer(_props.getProperty("proxy.port"));
ServerSocket serverSocket = new
ServerSocket(port.intValue());
Socket socket = null;
String fullRequest = "";
while(true) {

socket = serverSocket.accept();
/*BufferedReader reader = new BufferedReader
(new
InputStreamReader(socket.getInputStream()));
String lineRead = null;

//reads the request.
while ((lineRead = reader.readLine()) != null) {
fullRequest += lineRead;
} */

InputStream istream = socket.getInputStream();
OutputStream ostream = socket.getOutputStream();

StringBuffer headers = new StringBuffer();
int byteRead = 0;
while (!endOfHeaders(headers) && (byteRead =
istream.read()) >= 0) {
headers.append((char) byteRead);

}


HttpRequestHeader header = new
HttpRequestHeader(headers.toString());

ostream.write(header.asciiGetBytes(false));

int bytesRead = -1;
byte[] response = new byte[4096];
System.out.println("Before loop");

while ((bytesRead = istream.read(response)) >= 0)
{
System.out.println("Inside loop");
ostream.write(response, 0, bytesRead);
ostream.flush();
System.out.write(response, 0, bytesRead);
}
System.out.println("After loop");
socket.close();
//if(!serverSocket.isClosed())
// serverSocket.close();
}

}
catch(Exception e) {
throw new ProxyServerException(e.getMessage());
//System.out.println(e.getMessage());
}

}

/**
* Constructs the tables of the cache.
*/
public void constructCacheTables() throws ProxyServerException{
try {
_cache.createTables();
}
catch(Exception e) {
throw new ProxyServerException(e.getMessage());

}
}

/**
* Deletes the tables of the cache.
*/
public void deleteCacheTables()throws ProxyServerException {
try {
_cache.deleteTables();
}
catch(Exception e) {
throw new ProxyServerException(e.getMessage());
}
}

/**
* Clears the content of the cache (the tables are NOT deleted).
*/
public void clearCache() {

_cache.clearCache();
}

/**
* Termination. This method closes resources like statements,
connections,
* This method DOES NOT DELETE any table or table data.
*/
public void terminate() {

_cache.terminate();
}
public static void main(String[] args) throws Exception {

ProxyServer server = new ProxyServer();
server.start();
}
public static boolean endOfHeaders(StringBuffer headers) {
int lastIndex = headers.length() - 1;
if (lastIndex < 3 || headers.charAt(lastIndex) != '\n')
return false;
return (headers.substring(lastIndex - 3, lastIndex + 1)
.equals("\r\n\r\n"));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top