ServerSocket.accept() problem

  • Thread starter christian.kleegrewe
  • Start date
C

christian.kleegrewe

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
 
G

Gordon Beaton

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.

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

--
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top