Solved

Connect several clients to one server

Posted on 2001-08-30
8
318 Views
Last Modified: 2010-04-06
Hello

I want to make a program that will be able to exchange data between one server and several clients using TCP/IP protocol. Now I have a program that use ServerSocket and ClientSocket to connect only 2 computers. I want to connect to server at least 5 computers, but it is impossible to use more than one ServerSocket in one program. Is somebody know how to connect several clients to one server ?

Thanks
0
Comment
Question by:Alexei
8 Comments
 
LVL 1

Expert Comment

by:SenDog
ID: 6443692
Try using the ICS Components from Francois Piette which are available at: http://overbyte.alexid.fr/frame_index.html

It has more functionality and allows multiple connections to TWsocketServer.
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 6443902
Hi Alexei,

The following is a short explanation i have produced some time ago.

Hope this may help you !!

Use TClientSocket and TServerSocket - NonBlocking of cource.

The Client :

var
SendString : String:
Body : string;
MyErrorCode : integer;
....
SendString:='Something to send';
....
procedure xxx
begin
TClientSocket1.Open
Repeat
Application.Proccesmessage; //or run it in its own thread !!
until ContinueAfterPost:=true; //or something like this (e.g. a timeout)
end
...
procedure TFormClient.ClientWrite(Sender: TObject;
Socket: TCustomWinSocket);
begin
Socket.SendText(SendString);
end;
...
procedure TFormClient.ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Body:=Body+Socket.ReceiveText;
//
....
end;
...
procedure TFormClient.ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
MyErrorCode:=ErrorCode;
ErrorCode:=0;
ContinueAfterPost:=true;
end;
...
procedure TFormClient.ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
ContinueAfterPost:=true;
if MyErrorCode>0 then
exit; //There was an error
//
......
end;

The Server :

procedure TFormServer.ServerAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
You should create some kind of a record structure to save information about
this connection (all connections).
Socket has a data property (a pointer) in where you could save the record
pointer. You should also save the pointer in a TList.

It is then possible to run through the list to see whether a connection
should be timed out.
end;

procedure TFormServer.ServerClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Check the body (received data) for valid data - or if there has been a
error.
end;

procedure TFormServer.ServerClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
MyRecord : ^TMyRecord;
begin
MyRecord:=Socket.Data;
MyRecord^.Body:=MyRecord^.Body+Socket.ReceiveText;
end;

Reply to client.
MyRecord^.SocketObject.SendText('a reply');

If your client is behind a proxy-server you must include a HTTP-header in
your stream.
This is a very very short explanation, but it's a beginning.


Regards
Peter
0
 

Expert Comment

by:grini
ID: 6444087
Hi,Alexei.
So,when a client connects to Server this generates
an OnclientConnect event and creates a new socket,
what you can use later:
Serversocket.Socket.connections[i]
The number of connections:
Serversocket.Socket.Activeconnections
0
 
LVL 1

Expert Comment

by:Dennis9
ID: 6448583
Hi.

You could put this on server sockets on read:
//
if ServerSocket1.Active then
  begin
    if ServerSocket1.Socket.ActiveConnections > 0 then
      for i := 0 to pred(ServerSocket1.Socket.ActiveConnections) do
        ServerSocket1.Socket.Connections[i].SendText('Text to send to all');
        edit6.clear;
end;
//

//Then it would look like this:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  i: integer;
  str: string;
begin
  str := Socket.ReceiveText;
  if ServerSocket1.Active then
  begin
    if ServerSocket1.Socket.ActiveConnections > 0 then
      for i := 0 to pred(ServerSocket1.Socket.ActiveConnections) do
        ServerSocket1.Socket.Connections[i].SendText(str);
  end
end;



Hope it helped.

Dennis
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 1

Expert Comment

by:venks
ID: 6466479
MIDAS is the  best answer for this.But  you require a license at each client
Venks
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 6485629
A ServerSocket can handle multiple connections at the same time. No need for Midas or other components, except if these provide other functionality which is essential to your application.
0
 

Expert Comment

by:loticom
ID: 7716115
I just cant understand why you need 2 ServerSockets ,1 would accomodate more than a single connection.

Or we didn't get your point?.
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8280637
All,
I am unlocking this question in preparation for cleanup.  I will return in 7 days to finalize this question.  Please leave any recommendations for the final state of this question, I will take all recommendations into consideration.  Failing any feedback, I may decide in 7 days to delete or PAQ this question with no refund.  Thanks.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

16 Experts available now in Live!

Get 1:1 Help Now