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.

