Requests over one socket

Hi it's me again,

I tried to implement persistent connections to our project because of performance problems - as you can see some posts ago: http://forum.java.sun.com/thread.jspa?threadID=5188706&tstart=15

If a request from the client (browser) is sent to our webserver, a new connection(socket) is opened and the request is processed via servlet which finally send the response. Before implementing persistent connections for every request a connection was opened, f.ex. for one requested website 8 connections were established in this order:

Request A (Connection 1)

Request B (Connection 2)

Request C (Connection 3)

Request D (Connection 4)

Request E (Connection 5)

Request F (Connection 6)

Request G (Connection 7)

Request H (Connection 8)

No it is faster but - not as I supposed - still 2 connections were established! I thought, there must be only one. Maybe you can help me in finding my mistake.

Here is what the server does:

while (this.isRunning()){

try{

socket = serverSocket.accept();

synchronized (socket)

{

MyConnection connection =new MyConnection(this, socket);

Thread thread =new Thread(connections, connection,"DEBUG_" + counter );

counter++;

thread.setDaemon(true);

...

}

}

And here is the connection class:

try

{

while(true)

{

if (socket ==null || socket.isClosed())break;

line = in.readLine();

if (line ==null)break;

elseif (!readFirstLine && (line.trim().length() > 0))

{

// parse the first Header line

boolean ok = parseFirstHeaderLine(line);

if (!ok)break;

readFirstLine =true;

}

// read header values and add to header map

elseif (line.trim().length() > 0)

{

int colonPos = line.indexOf(":");

if (colonPos > 0)

{

String key = line.substring(0, colonPos);

String value = line.substring(colonPos + 1);

headers.put(key, value.trim());

}

}

else

{

checkForPostRequest();

// process request (search servlet and send response)

processRequest();

// request ends, a new request will start

readFirstLine =false;

headers.clear();

requestParameters.clear();

}

}

}

catch(IOException e)

{

...

}

finally

{

// close socket

}

I was wondering myself if the server accepts a new connection while the other requests aren't yet answered by the server?

If I use tcpTrace to watch the traffic I can see two connection with status "connected" and the requests are splitted in this way:

Request A (Connection 1)

Request B (Connection 1)

-- Request C (Connection 2)

Request D (Connection 1)

-- Request E (Connection 2)

Request F (Connection 1)

-- Request G (Connection 2)

Request H (Connection 1)

Do you have any ideas?

Thanks, jacquipre.

[4784 byte] By [Jacquipre79a] at [2007-11-27 9:39:40]
# 1
Meanwhile I found out that IE usually opens max. 2 persistent connection. So I suppose that I cannot influence how many connections the client (IE) will open for requests. But what I definitely know is that there will be max. 2 connections... Or is there an (logical) error in my
Jacquipre79a at 2007-7-12 23:15:29 > top of Java-index,Core,Core APIs...
# 2

If the client opens two or more connections, of course there will be two or more connections at the server, and the processing at the server will be interleaved. Nothing you can do about that.

What your implementation has done is enable the client to reuse those connections after their first use.

ejpa at 2007-7-12 23:15:29 > top of Java-index,Core,Core APIs...
# 3

> If the client opens two or more connections, of

> course there will be two or more connections at the

> server, and the processing at the server will be

> interleaved. Nothing you can do about that.

OK.

> What your implementation has done is enable the

> client to reuse those connections after their

> first use.

So that was exactly what I want to do... Seems right, isn't it?

Thanks.

Jacquipre79a at 2007-7-12 23:15:29 > top of Java-index,Core,Core APIs...