Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1141
  • Last Modified:

ServerSocket Questions

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
Romans
Asked:
Romans
  • 4
  • 4
  • 3
  • +5
1 Solution
 
eddie_khoCommented:
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
 
PeterLarsenCommented:
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
 
AvonWyssCommented:
> -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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rondiCommented:
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
 
lsaeCommented:
listening...
0
 
karouriCommented:
listening..
0
 
RomansAuthor Commented:
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
 
RomansAuthor Commented:
Thanks AvonWyss - you were right on the max number of connections. Still looking for solutions to the others ..
0
 
PeterLarsenCommented:
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
 
AvonWyssCommented:
Romans, I believe that I have answered pretty much everything you asked. So, what solutions are you looking for?
0
 
RomansAuthor Commented:
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
 
AvonWyssCommented:
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
 
PeterLarsenCommented:
If you want to use more than 100 socket connections, you should install the latest winsock.
There are no limitations in that version.
0
 
RomansAuthor Commented:
Thanks for all the help !
0
 
AvonWyssCommented:
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
 
suyuCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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