• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2192
  • Last Modified:

TCP Socket server handling multiple requests

Morning,

I have a basic socket server up and running - it listens on port x, when it receives an external request it does some stuff with that connection, then it resets to handle the next request.

I want to make the socket server so that it can handle multiple requests at the same sometime.

Could I get some guidance on what techniques will be required for this.  In my mind, the following would seem relevent:
1. Creating a new tread for each incoming connection
2. Somehow passing off each new connection to another port (random) so that port x is freed up to await the next connection
3. Ensuring that the working code that does stuff with the external request is thread safe.

Is there anything else I should consider.

Thanks
SF
0
sheepfarmer
Asked:
sheepfarmer
  • 5
  • 4
  • 2
4 Solutions
 
MerijnBSr. Software EngineerCommented:
1 & 3: This depends on if you are going to use blocking or non-blocking sockets. Which is the smarter choice depends on what kind of data (protocol) you are going to use, can you tell something about this?

2: This is done for you most of the time.

Most important thing to consider is the answer for 1 & 3, what toolkit / component / setup are you going to use, don't take this lightly, it can mean the difference between a breeze and a nightmare!
0
 
sheepfarmerAuthor Commented:
What is the primary difference between blocking and non-blocking?

The application is relatively straightforward:
1. It listens on a port
2. On connection, it talks to the client doing some 'stuff' for a period of time (could be up to 30 mins)
3. In the meantime, it needs to listen and handle other incoming connections
4. The 'stuff' it does in the background is threadsafe (i.e. no conflict).

Currently just using the build in TSocketServer component in non-blocking mode.  
Are there better componenets for this sort of thing?  Delphi 2009 BTW.

Thanks
SF

0
 
MerijnBSr. Software EngineerCommented:
> What is the primary difference between blocking and non-blocking?

When you use a blocking mechanism it means that everything you do (send data, wait for data) halts the rest of your thread. This means that for each connection you'll have to create a separate thread, which brings in complexity. Advantage is that (with the right data), it's easier to parse the protocol because you always know what you are doing if data comes in. In other words, you don't have to keep track of where in the parsing process you are.

In non blocking mode it all feels much more like 'normal' development. Things work with events and you can (in most cases) keep doing everything from the main thread. So you just get an event if one of the clients sends data, in stead of waiting for it. It makes development easier, but some data is harder to parse (depends on the protocol).

I'd always suggest to go for non-blocking because that way you don't have to do anything with threads. Looking at your 4 points, the choice mostly depends on what kind of data is sent / received.

> Are there better componenets for this sort of thing?  Delphi 2009 BTW.

I never used the build in TSocketServer much, because of some limitations I found long ago (they might be gone now, don't know). I've used ICS for many years now when doing non-blocking tcp/ip development and it works great: http://www.overbyte.be/frame_index.html?redirTo=/products/ics.html
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sheepfarmerAuthor Commented:
The ICS looks good, in particular its support for SSL - have you any experience with SSL in that library - I assume that if both ends of the link (client and server) use Delphi and ICS, a secure connection could be established.

On the blocking side of things, could you set a timeout so that if no response is received, you get back control of the thread?


0
 
MerijnBSr. Software EngineerCommented:
> Have you any experience with SSL in that library

No I don't, but there is good support from the author and experienced users who can help you with this without doubt.

> On the blocking side of things, could you set a timeout so that if no response is received, you get back control of the thread?

Yes you can, _but_ I'd like to know why you ask this, because this is not a reason to do blocking in your mean thread!
0
 
sheepfarmerAuthor Commented:
On the blocking issue, I have another project where an application talks to an external device via TCP.  The communication is such that the application makes a request, then waits for a response.  This would appear more like the blocking model since if no response is received, this is an error.

With the non-blocking model, you would have to put in an external 'timer' or some such to generate a timeout if no response is received in a specified period of time.
0
 
TheRealLokiSenior DeveloperCommented:
I've got some demos up that do most of what you want
using Indy or SocketServer
I do have some ICS ones, but you might find the indy ones easier to understand

http://sourceforge.net/projects/internetdemos

0
 
MerijnBSr. Software EngineerCommented:
> I do have some ICS ones, but you might find the indy ones easier to understand.

Always nice to see different opinions through different approaches, I feel the other way around :)
0
 
sheepfarmerAuthor Commented:
I would like to see the ICS ones too if available.

Thanks
SF
0
 
TheRealLokiSenior DeveloperCommented:
> Always nice to see different opinions through different approaches, I feel the other way around :
My personal preference is for ICS or native TSocketServer ;-)

0
 
sheepfarmerAuthor Commented:
Ok, before this gets into an opinion war, I'll close the question - thanks for your help all.

SF
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now