[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

Connect several clients to one server

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
Alexei
Asked:
Alexei
1 Solution
 
SenDogCommented:
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
 
PeterLarsenCommented:
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
 
griniCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Dennis9Commented:
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
 
venksCommented:
MIDAS is the  best answer for this.But  you require a license at each client
Venks
0
 
AvonWyssCommented:
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
 
loticomCommented:
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
 
SpideyModCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now