Hi all,
I try to write a HTTP proxy using java.net.ServerSocket class, but I
do not understand the behaviour of the accept call.
Here is the part of the code where the ServerSocket is created and
accept is called.
-->
ServerSocket s = new ServerSocket (localport);
while (true) {
Socket c = null;
try {
display("listening...");
c = s.accept();
if (c != null) {
byte r_in[] = new byte[256];
String request = new String();
InputStream in = c.getInputStream();
-->
I am testing the code with an opera web browser that has configured my
own proxy as web proxy. What happens no is a little bit confusing. The
server starts correctly and "listening..." is pronted on the console
but when I type in an URL in the address bar and press enter nothing
happens. After terminating the request by pressing the X button in the
navigation bar the accept returns and the URL is retrieved by the
proxy but not transmitted to the web browser since there is no
connection any more after the X button has been pressed.
Is there anybody who can help me with this behaviour? As far as I
suppose the accept should return immediately after I typed in the URL
and pressed the load or go button.
I am using windows XP SP2 as operating system and java 1.507 as jdk
thanks in advance Christian
Gordon Beaton - 26 Mar 2007 09:46 GMT
> I am testing the code with an opera web browser that has configured my
> own proxy as web proxy. What happens no is a little bit confusing. The
[quoted text clipped - 4 lines]
> proxy but not transmitted to the web browser since there is no
> connection any more after the X button has been pressed.
Most likely your problem is not in accept() but in the part of the
code you didn't post, where you actually read the request from the
InputStream. Presumably you're reading to EOF instead of just to the
end of the request header, or something along those lines.
Try connecting with telnet instead of a web browser, and typing the
request manually so you have better control over the situation.
Try displaying each line as it's read by your server to see what's
happening.
/gordon
--