Solved

ServerSocket Questions

Posted on 2001-09-06
16
1,051 Views
Last Modified: 2007-11-27
I have several Questions. Perhaps they can all be answered with a small server example with some good comments !

-Is there a maximum number of clients that can connect concurrently to a TServerSocket?
-Can the ServerSocket listen on multiple ports or should I use multiple ServerSocket components in the application?
(If multiple ports I could use some code)
-What considerations do I need to make in order to have 100+ connections (NonBlocking, ThreadBlocking, etc)?
-How do I keep track of what connection belongs to each client so I may send specific messages back to each one? (I could really use an example of this)
-Are the special considerations for the client side (NonBlocking or ThreadBlocking)?

I will put 200 points for this since there are quite a few questions.

Thanks
0
Comment
Question by:Romans
  • 4
  • 4
  • 3
  • +5
16 Comments
 

Expert Comment

by:eddie_kho
ID: 6462758
property 'port' of TServerSocket can be change on run-time, never tried it before but i think you better use multiple component....

on the ClientRead event, there is a parameter named 'socket', you can do something like this to get the sender information:

......
Var sTemp : String;
....
   sTemp := Socket.RemoteAddress;
....
....
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 6463690
If you need to listen on multiple port, you should add a component for each port.

Use NonBlocking !!!

To keep track of multiple connections you could do something similar to this :

Record type :

TMyConnection = record
   InUse : Boolean;
   ClientHeader : string;
   HeaderStrings : Tstringlist;
   HeaderOK : boolean;
   TotalBytes : integer;
   ClientIp : string;
   Disconnected : boolean;
   ReplyHeader : String;
   ReplyBody : string;
   SocketHandle : integer;
   Body : string;
   BodyStrings : tstringlist;
   TimerOn : boolean;
   TimerTime : Tdatetime;
   SocketObject : TcustomWinSocket;
   SaveLog : boolean;
//and/or what-ever you need
end;

var
 ConnectionList : TList;

procedure TFormServer.ServerAccept(Sender: TObject;
  Socket: TCustomWinSocket);
var
 MC : ^TMyConnection;
begin

//check wheather you want this connection or not
//Socket.RemoteAddress / Socket.close; etc.

new(mc);
ConnectionList.add(MC);

MC^.ClientIP:=Socket.RemoteAddress;
TimerOn:=true;
TimerTime:=now; //reset time out value
 //Use the timer value to see whether a connection
 //should be timed out.

Socket.Data:=MC; //**********
MC^.SocketObject:=socket;

etc. etc.
end;

procedure TFormServer.ServerClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
 MC : ^TMyConnection;
begin
MC:=Socket.Data. //now you have the connection information
MC^.Body:=MC^.Body+Socket.ReceiveText;

etc. etc.

Good luck
Peter
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 200 total points
ID: 6464044
> -Is there a maximum number of clients that can connect concurrently to a TServerSocket?

Delphi does not limit the number of connections, but the operating system does. Windows 9x/ME is said to have a limit of 64 sockets (I have not verified this), while the maximum number of sockets on Windows NT/2000/XP is limited by the size of the nonpageable memory pool (which if sized depending on the actual real memory which has been installed in the machine). Some hundreds or a few thousand connects however shoule be no big problem with current memory sizes.

> -Can the ServerSocket listen on multiple ports or should I use multiple ServerSocket components in the
application?

No, it can listen on multiple IP addresses but on one port only. This limitation is given by the underlying sockets interface.

> (If multiple ports I could use some code)

Yes, if you do it right. All events have a Sender or Socket parameter which will give you the information needed to have one method handle events from multiple serversockets.

-What considerations do I need to make in order to have 100+ connections (NonBlocking, ThreadBlocking,
etc)?

Blocking means that every connect has its own thread. This is OK for small numbers of connects, but your application should not have too many threads (I'd say that about 10-20 should be the limit for single-CPU systems; with SMP, you can about multiply this value by the number of processors). So NonBlocking would be the way to go. Be aware, however, that nonblocking means that only one threads handles all requests by default; make sure to have small and fast event code so that you don't accumulate events and finally get stuck by it. A good method (but only for programmers experienced with threads) is to have a pool of threads and to dynamically assign tasks to these threads; this has the advantage to avoid having as many threads as connect but allows you to do more lengthy operations without locking up your application.

-How do I keep track of what connection belongs to each client so I may send specific messages back
to each one? (I could really use an example of this)

You can do so by creating a list of open connections. Use the OnConnect and OnDisconnect events to add or remove clients from the list.

-Are the special considerations for the client side (NonBlocking or ThreadBlocking)?

Not really. The client may be better of in blocking mode since it is sometimes easier to deal with. But it's no neccesity.
0
 
LVL 3

Expert Comment

by:rondi
ID: 6464069
Hi, I haven't used TServerSocket - I use Francois Piette's
ICS package (http://www.rtfm.be/fpiette), but the same
principles should apply:


-Is there a maximum number of clients that can connect concurrently to a TServerSocket?
    -> Theoretically, no. Every connection instantiates a new client socket object in the server.

-Can the ServerSocket listen on multiple ports or should I use multiple ServerSocket components in the
application? (If multiple ports I could use some code)
    -> Nah, each server sock. can only listen on one port.
You could point their OnAccept events to the same
procedure, though.
 
-What considerations do I need to make in order to have 100+ connections (NonBlocking, ThreadBlocking, etc)?
    ->The major consideration is speed. You probably wanna
use the asynchronous or Non-Blocking approach.

-How do I keep track of what connection belongs to each client so I may send specific messages back to each one? (I could really use an example of this)
    ->You could, as Peter suggested, use a custom record and attach a record to each Client Socket; or you could
maintain a TStringList that maps client remoteaddresses
to client socket objects. I'd recommend the former.

-Are the special considerations for the client side (NonBlocking or ThreadBlocking)?
    ->AFAIK, this should have NO effect on the server.

rondi
0
 

Expert Comment

by:lsae
ID: 6465494
listening...
0
 
LVL 3

Expert Comment

by:karouri
ID: 6468552
listening..
0
 

Author Comment

by:Romans
ID: 6469284
I have been researching and I found a website that answered my first question - max number of socket connections.

http://www.coders.sk/manualy/win/wskfaq/advanced.html#q16

I tested a small a small app on Win 98 - the max number of connections I could achieve was 70 ..
0
 

Author Comment

by:Romans
ID: 6469286
Thanks AvonWyss - you were right on the max number of connections. Still looking for solutions to the others ..
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 2

Expert Comment

by:PeterLarsen
ID: 6469946
If you'r using NT you won't have that connection limitation.
But your major problem will probably be the speed, as mentioned by Rondi.
Do you have to keep so many connections a live ??

Btw. OnAccept is called before OnConnect - so this is here you should hook your program up !!
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6470110
Romans, I believe that I have answered pretty much everything you asked. So, what solutions are you looking for?
0
 

Author Comment

by:Romans
ID: 6478126
I still don't have an answer to my 100+ connections problem. I'd like to be able to accomodate well over 100 users. If there is a problem maintaining a connection for each one, or if the max is 70 connections (Win98), or if there is a speed problem, how would I accomplish. Take any IRC application - how do they accomplish this?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6478215
An IRC application makes one (1) connect to a server, which in turn has hundereds or thousands of connected users. The server will take care of routing the messages to the appropriate users connected. Note that no-one would take Windows 9x as server OS for a larger IRC server, thus the 70 socket limit is no problem (and even then, it can be raised using a registry hack).

So, if you effectively want to handle 100+ connection, do the reg hack or get yourself a more decent OS than Windows 9x/ME.
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 6478662
If you want to use more than 100 socket connections, you should install the latest winsock.
There are no limitations in that version.
0
 

Author Comment

by:Romans
ID: 6503401
Thanks for all the help !
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6504282
Were my comments really THIS poor, that you only gave me a C grade? The Q&A page says:

"How Do I Know What Grade to Give?

Although we use an A-D scale here at Experts Exchange, it works differently than, say, school grades. If one or more Experts' proposals are accepted as answers, they should usually be given an A or B grade, since they have taken the time to provide you with a working solution. If a possible solution is incomplete - ask for clarification or details before accepting the answer and grading it. People should not be given lower grades because of incorrect grammar or because you just accepted their answer or comment to close the question. Keep in mind, your question and any follow-up comments should be focused so that there can be a specific answer. The following is a good guideline to follow when grading:

A: The Expert(s) either provided you with a thorough answer or they provided you with a link to information that thoroughly answered your question. An "A" can also be given to any answer that you found informative or enlightening beyond the direct question that you asked.

B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution, that you were able to use, although you may have needed a bit more information to complete the task.

C: Because Experts' reliability are often judged by their grading records, many Experts would like the opportunity to clarify if you have questions about their solutions. If you have given the Expert(s) ample time to respond to your clarification posts and you have responded to each of their posts providing requested information; or if the answers, after clarification, lack finality or do not completely address the issue presented, then a "C" grade is an option. You also have the option here of just asking Community Support to delete the question.

Remember, the Expert helping you today is probably going to be helping you next time you post a question. Give them a fair chance to earn an 'Excellent!' grade and they'll provide you with some amazing support."

Please explain why you gave the C grade. Thanks you.
0
 

Expert Comment

by:suyu
ID: 8804588
Hi,

i happened to have an application in which i need to handle two tasks (continuous data exchanging between server and client) simutanously, what i have done is using two client sockets on the client machine(with different ports) and two server sockets on the server machine (corresponding to the ports set up with client sockets, so, they have two different posts as well). when the applicaion runs, both two client sockets send requests to the server machine, i was trying to use one method to handle events from the two different sever/client sockets, similar to AvonWyss's suggestion in his Answer dataed 09/07/2001 06:26AM PDT, the problem is when the second task connection is formed, the server application give message, Windows socket error (10022) on API 'rev', then, only one task carries on exchanging data and the other task is dropped,  i am a new registeree on the site, but think it would be an A grade for any solution?

thanks  
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now