• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Server Sockets

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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...

Regards
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. There will be single server socket running one thread per one request.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 webserver...how 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??

Regards
 
Bartender
Posts: 9625
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 webserver...how 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.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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???
Regards
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic