PDA

View Full Version : درباره SocketServer و SocketClient



Dracula
دوشنبه 26 خرداد 1382, 11:07 صبح
با سلام خدمت همه دوستان عزیز

در صورت امکان توضیحاتی درباره این 2 کامپوننت بدهید . متشکرم .

درباره نحوه کار کردن و در صورت امکان یک مثال .

پیشاپیش از همه دوستان متشکرم .

مهدی کرامتی
دوشنبه 26 خرداد 1382, 23:28 عصر
<div dir=ltr>
Question/Problem/Abstract:

A Cookbook or step-by-step instructions for building a MIDAS application.
Answer:


First do the Delphi Update-Packs !!

Start the Borland SOCKET SERVER (in the BIN folder).


Create the SERVER:

1. Add a RemoteDataModule (RDM) to the SERVER-project
2. Drop TTable or TQuery.
3. Drop a TProvider and connect it with the TTable (or TQuery). Choose 'Export from Datamodule' via the right-mouse-button.
4. Save and run the SERVER for registration

http://www.barnamenevis.org/forum/temp/RDM_plus_Provider_plus_ExportMnu.gif

Create the client:

1. Drop TSOCKETConnection in a form
2. Enter the machine-name at the host-property
3. Pay attention at the port (SOCKET SERVER settings)

4. There will be a list of RDMs at the SERVERName-property
5. Drop a TClientDataset (CDS) and connect it to TSOCKETConnection
via RemoteSERVER. Select a Provider (ProviderName-property)
6. Set Active to true and connect the DB-controls (e.g. DBGrid)

That's it !

HINT: For more useful information on MIDAS and DCOM look at Dan Miser's website:
www.distribucon.com/
</div>

مهدی کرامتی
دوشنبه 26 خرداد 1382, 23:32 عصر
<div dir=ltr>Question/Problem/Abstract:

The Delphi documentation on the TServerSocket's multithreading capabilities can appear a little sparse for the untrained eye. I will try and shed a little more light on the subject.
Answer:


Actually it's pretty easy to make a multithreaded SERVER that listens for messages on a SOCKET. Delphi has a component for that: the TSERVERSOCKET.

But you need a little bit of knowledge to use it.

In order to structure your work, you should:

- Add a TSERVERSOCKET to your main form.
- Set the SERVERtype property to stThreadBlocking
- Create a new unit (shown below) containing the SERVER thread.


Make the following code on the OnSOCKETGetThread

procedure TfrmMain.fSOCKETGetThread(Sender: TObject;
ClientSOCKET: TSERVERClientWinSOCKET;
var SOCKETThread: TSERVERClientThread);
begin
// This creates the TSERVERThread object I have shown
// in the code below. A new object is created each time
// A new connection is established.
SOCKETThread := TSERVERThread.Create( FALSE, ClientSOCKET );
end;

The TSERVERThread is an object I have created myself. The object inheits from TSERVERClientThread and contains the code that actually are reading and writing from the SOCKET.

The unit I created contains at least the following code:

unit SERVERthread;

interface

uses
windows, scktcomp, SysUtils, Classes, Forms;

type
ESERVERThread = class( Exception );
// The SERVERthread is a descendant of the
// TSERVERClientThread
TSERVERThread = class( TSERVERClientThread )
private
fSOCKETStream : TWinSOCKETStream;
public
procedure ClientExecute; override;
// The ClientExecute overrides the
// TSERVERClientThread.ClientExecute
// and contains the actual code that is
// executed when the thread is started
end;

implementation

procedure TSERVERThread.ClientExecute;
begin
inherited FreeOnTerminate := TRUE;
try
fSOCKETStream := TWinSOCKETStream.Create( ClientSOCKET,
100000 );
// 100000 is the timeout in miliseconds.
try
while ( not Terminated ) and ( ClientSOCKET.Connected ) do
try
// This is where you will do the actual
// Waiting for input, Reading and writing
// The examples below shows what you can
// put in here.
except on e:exception do
begin
// An error has occurred, close and exit
ClientSOCKET.Close;
Terminate;
end;
end;
finally
fSOCKETStream.Free;
end;
except on e:exception do
begin
// An error has occurred, close and exit
ClientSOCKET.Close;
Terminate;
end;
end;
end;

When the connection is established, the thread needs to wait for incoming data. You can use this code to wait for data:

if ( not Terminated ) and
( not fSOCKETStream.WaitForData( 1000000 ) ) then
begin
// Handle the timeout
end;
// There are incoming data on the SOCKET!

To read data, you should have a buffer to store the data in. Usually the buffer is a PByteArray or a array of chars. In this example I have a buffer called fRequest which is a array of chars. Furthermore I am expecting a fixed number of bytes. My array has the size of the constant REQUESTSIZE.

var
ac, readlen : integer;
begin
FillChar( fRequest, REQUESTSIZE, 0 );
ac := 0;
repeat
readlen := fSOCKETStream.Read( fRequest[ac],
1024 );
// I read in chunks of 1024 bytes until the buffer
// is full
ac := ac+readlen;
until ( readlen = 0 ) or ( ac = REQUESTSIZE );
end;

If readlen is 0 then I do not receive any more data. The Read function times out after 100000 miliseconds as stated in the TWinSOCKETStream.Create(). If you do not know how much data to expect, you should set this timeout fairly small. 30 seconds should be a maximum in most situations.

When sending a reply, you should be aware of your clients behavior. Many clients only waits for one package of reply, others expects many packages.
In this example, I have a client that only expects one package, so I have to send my data back in one chunk:

fSOCKETStream.WriteBuffer( fRep, fReplySize );

The fRep is the reply buffer, and fReplySize is the size of the replybuffer. </div>

مهدی کرامتی
دوشنبه 26 خرداد 1382, 23:34 عصر
کافیه؟ یا بازم از Delphi3000 برات اینجا مقاله Paste کنم؟ :wink:

Dracula
سه شنبه 27 خرداد 1382, 08:54 صبح
آقا دستت درد نکنه :wink:

ممنونم .

Dracula
سه شنبه 27 خرداد 1382, 09:03 صبح
آقای دلفی اسیستنت دستت درد نکنه

من یه sample هم پیدا کردم که از آدرس http://www.delphi3000.com/redirect.asp?Link=../article/2364/Chat.zip میتونید دانلود کنید .

موفق باشید .