aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Server Sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Server Sockets" Watch "Server Sockets" New topic

Server Sockets

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979

When multiple requests are hitting a server.....say port 80 http service

then how will the server actually be able to service all the requests....

Will the server create many sockets.....a socket for each request...

Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

No. There will be single server socket running one thread per one request.

Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Suppose there is an web server..that takes http requests and in the application we have to connect to a database...

If there are 100 requests....hitting the webserver and all 100 requests should do some kind of database access..

the database resides in different server machine...(of course only port exists at that server to listen for requests)

But from the will these 100 requests ask for database connectivity...

From the webservers point of view....will it open say 'x' no of sockets that connect to a single point of contact of database server??

Joe Ess

Joined: Oct 29, 2001
Posts: 8927

I think Prabhu was too simple in his answer to your original question. There is one socket listening, the socket that the service is bound to (in a web server, usually 80/443 for HTTP/HTTPS), but the server socket creates a new socket for each incoming connection. This way the incoming connection can be serviced while the server socket can continue to listen. It is also common practice to use threads to allow simultaneous servicing of existing connections and listening for new ones.

Originally posted by A Kumar:

But from the will these 100 requests ask for database connectivity...
From the webservers point of view....will it open say 'x' no of sockets that connect to a single point of contact of database server??

Since database connections are non-trivial to create, it is common to use a pool of connections rather than creating one for each request. And again, the database will work like the web server described above: bound to a single socket, creating a socket for each simultaneous connection.
Have a look at The Java Tutorial: All About Sockets for some simple code.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Nathan Leniz
Ranch Hand

Joined: Nov 26, 2006
Posts: 132
I also remember reading from my PHP days that most databases are single write/multi read. If I'm remembering correctly, you can have a lot of connections to your database (which is bad, connection pooling is much better as it creates less strain on the DB server), but the first thread coming in to write gets priority, then goes down in a queue.

Can anyone verify if this is still (or ever was) correct? I'm learning Java to build an online application that may have a lot of people trying to access a database at any given time.

The very existence of flamethrowers proves that at some time, some where, some place, someone once said to themselves "I'd really like to set those people on fire over there, but I just can't get close enough".
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979

thanks for the reply...

Actually we have a requirement of creating a pool of sockets in the webserver

so that any req that hits should use a socket from this pool and connect to the CRM server and once it gets the response...release the socket to the pool..

How to design such a pool that can be scalable to withstand 1000's of simultaneous requests???
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42289
Thousands of simultaneous requests? Wow! That's a serious number. Apart from everything else I'd say that multiple servers will be necessary, if every request involves a further access of a second server.

Ping & DNS - my free Android networking tools app
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Servers are designed in a multithreaded way.
The listening socket is one , but as soon as the request comes ,the server creates another socket and a worker thread to deal with the request and the main thread keeps on listening to that socket for request.If another request comes ,the same procedure is followed.
Apart from the main binding port , for every request the server will create a socket at some available port.

Rahul Bhattacharjee
LinkedIn - Blog
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a quick example from a real server of what Rahul just described:

When requests come in faster than they are handled the thread pool expands to make more threads. This particular server does not put any limit on requests and threads because it only has a handful of users. One could use a thread pool with a max size or keep count of active requests. I guess you could send a "server busy" message back to the user at some point.

Any database access would be in the MessageHandler and is not really related to being in an HTTP server. At high loads, some kind of connection pooling will be critical for your throughput.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
It is sorta covered in the JavaRanch Style Guide.
subject: Server Sockets