حالة الموضوع:
مغلق
  1. .:: RSS ::.

    .:: RSS ::. عضوية آلية

    الأنتساب:
    ‏9 سبتمبر 2011
    المشاركات:
    14,005
    الإعجابات المتلقاة:
    0
    نقاط الجائزة:
    36
    الإقامة:
    IQ-T34M
    :43:Hello :12:
    CREATING A CLIENT/SERVER APPLICATION IN DELPHI

    Start two instances of Delphi. We’ll use one for the Client and one
    for the Server. Tab to your first copy of Delphi and change the
    Form’s caption to ‘
    Client’. Now tab to the second copy of Delphi and
    change the Form’s caption to ‘
    Server’. .
    If you take a look under the
    Internet tab of your Component Palette –
    you will see a
    ClientSocket and ServerSocket components
    on your
    Client Form, and the ServerSocket on your Server Form. Their
    .
    :43:
    THE CLIENT
    Place 2 Edit boxes, 2 Buttons, and a Label on your Form. Change the
    Text property of your Edit1 to
    ‘127.0.0.1’ (your local IP). Change
    Button1’s Caption property to ‘&Connect’ (the ‘&’ sign will create a
    shortcut to that Button of the first letter). Change Button2’s
    Caption to ‘&Disconnect’ and change your Label’s caption to ‘Not
    Connected’. Delete the text in your Edit2’s Text property.
    Now, click on your ClientSocket component and change the port
    property to ‘
    55555’ (could be any port you wish as long as it doesn’t
    conflict with other port numbers, I.E –
    port 80 is used for HTTP so
    don’t use that port etc). The OnClick event for you Connect Button:13::
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ClientSocket1.Address :=
    Edit1.Text;
    ClientSocket1.Active :=
    True;
    end;
    Here we are setting our ClientSocket’s Address property with the Text
    in our Edit1. The Address property is where the IP you wish to
    connect to would go. Secondly we are setting the Active property toTrue – which means it will try to connect to the Port and Address
    properties we have specified.
    We know when we have established a connection because the OnConnect
    ClientSocket event is fired. Create the event and put this in:
    procedure TForm1.ClientSocket1Connect(Sender: TObject;
    Socket: TCustomWinSocket);
    begin
    Label1.Caption :=
    'Connected!';
    Button1.Enabled :=
    False;
    Button2.Enabled :=
    True;
    end;
    The Enabled property disables or enables the component accordingly
    (you’ll see at runtime). Our Disconnect Button:
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    ClientSocket1.Active := False;
    end;
    That will disconnect from the connection. ClientSocket has an event
    called OnDisconnect which unsurprisingly is fired when we disconnect:
    procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
    Socket: TCustomWinSocket);
    begin
    Label1.Caption := 'Disconnected';
    Button1.Enabled := True;
    Button2.Enabled := False;
    end;
    Now for our Edit2. Create an OnKeyDown event for it and do:
    procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
    Shift: TShiftState);
    begin
    if (Key = VK_RETURN) then
    begin
    ClientSocket1.Socket.SendText(Edit2.Text);
    end;
    end;
    This event will be fired each time you press a key while you’re
    cursor is in the Edit2 box, and each time it will check this
    condition: if (Key = VK_RETURN) - That will only execute what’s
    between its begin and end; if the key is VK_RETURN – which is a
    Virtual Key Code for the Enter Key. So we’re saying if you press
    enter while your cursor is in the Edit2 box, then send the text in
    the Edit2 box to the host we’re connected to.
    ClientSocket1.Socket.SendText() does this.
    :43:THE SERVER:
    Place a Button and a Label on your Form. Set the ServerSocket’s Port
    property to ‘55555’ – it needs to be the same port as your Client’s.
    Change your Label’s Caption to: ‘Not serving at the moment’, and
    change your Button’s Caption to: ‘&Listen for connections’.
    OK, now create the OnClick event handler for your Button and do:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ServerSocket1.Active := True;
    Label1.Caption := 'Listening for connections.';
    end;
    Here we are setting the ServerSocket’s Active property to True which
    opens the Port you specified as listening. This is opposite to our
    ClientSocket’s Active property which will try to connect to a host.
    Create an OnClientRead event for your ServerSocket which will be
    fired when something is read from the client – when it receives som
    data from the client. Make it look like this:
    procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
    Socket: TCustomWinSocket);
    begin
    Label1.Caption := Socket.ReceiveText;
    end;
    We are just assigning the Label’s Caption with Socket.ReceiveText,
    which is a method which reads text received from the connection.
    Right, now save your client in a separate folder (E.G: c:\client) and
    the server in another folder (E.G: c:\server). Compile each, and
    tests them out.
    You will not be able to connect to the server unless it is listening,
    which you enable by clicking its Button. Now you can connect to it –
    whether it is on your computer or on a networked computer, just put
    in the IP of the machine it is on and click Connect. Type some text
    into the blank Edit box (our Edit2) and press enter. This will send
    it to the server which will display it using the Label Caption.
    :43:MAIN SECTION
    In this section I will explain new things as they crop up, but topics
    covered in the beginner’s section will not be covered in great
    detail. Each subheading will contain new functionality you can add to
    your application. Remember, I’m just giving you the information here
    of a way of how to do things. By all means expand on that, add your
    own ideas and program it to suit your own needs.
    :43:pROGRAMMING OUR REMOTE ADMINISTRATION TOOL
    Open two copies of Delphi. We will use one for work on the Client
    side of our application and the other for work on our Server. Tab to
    your first copy of Delphi and change the Form’s caption to ‘Client –
    No Connection’. Now tab to the second copy of Delphi and change the
    Form’s caption to ‘Server’. When I refer to your Client, I will be
    talking about the copy of Delphi you have open with which you are
    working on the Client Form, and vice-versa.
    Drop a ClientSocket component on your Client Form and drop a
    ServerSocket component on your Server Form. Set the Port of both
    sockets to the port you would like your application to run on. At
    this point, save both parts of your application in two separate
    folders.
    :43:THE CLIENT:
    Place an Edit, a Memo, and two Buttons on your Form. Change the name
    of your Edit to ‘IPBox’ and you could change its Caption to the IP of
    the machine you will be connecting to in testing (maybe: 127.0.0.1).
    Your first Button’s Caption should be changed to ‘&Connect’ and the
    second: ‘&Disconnect’.
    Put this in your Forms OnCreate Event:
    Memo1.Clear;
    That’s because by default our Memo will have ‘Memo1’ written in it.
    We don’t need that displayed so we’ll remove it when our application
    starts.
    Lets make our Connect Button do something. We want to use exception
    handling here with the try..except, because if the user puts in
    something that is not an IP, Windows will display an error. So put
    this in your OnClick event:
    try
    ClientSocket1.Address := IPBox.Text;
    ClientSocket1.Active := True;
    except
    Memo1.Lines.Add('** Error Connecting.');
    end;
    I like the idea of changing for Form’s caption when you connect like
    this (OnConnect event):
    Form1.Caption := 'Client - Connection Established';
    And for the OnDisconnect: Form1.Caption := 'Client - No Connection';
    You could also add a line to your Memo if you like (in the same way
    as Tron does).
    Really, you just need a way of knowing when you’ve connected and
    disconnected. You want your Disconnect Button to actually disconnect
    from the host by using this code: ClientSocket1.Active := False;
    If you like, you could add a line to the Memo saying ‘Connecting..’
    or something. Just do this in the ClientSocket OnConnecting event.
    Now if your Client can not connect to the server for whatever reason,
    the OnError ClientSocket event will be fired. Now if you haven’t
    created this event you’ll get the old Windows message box which we
    want to avoid really. If you do this in the OnError event..:
    Memo1.Lines.Add('** Error: ' + IntToStr(ErrorCode));
    ErrorCode := 0;
    ..Then what’s called an ErrorCode will be added to the Memo instead.
    An ErrorCode is just a generated code from which the code number
    indicates the type of error which occurred. Setting the ErrorCode to
    0 as we have done on line two, stops the Windows error message popup.
    Another idea is to disable your Disconnect Button (Enabled property
    set to False) at design-time and then enable it when you connect. You
    can find an example of this in the Client section of: CREATING A
    CLIENT/SERVER APPLICATION (BEGINNERS SECTION).
    :43:THE SERVER
    Remember to set your Server Port to the same as your Client Port. Now
    if you change the ServerSocket Active property to True here, at
    runtime your server will start serving (listening for connections)
    the minute you run it. So do that and then compile each side of your
    application and test. OK it won’t do much, but it allows you to make
    sure you’ve coded it correctly. To follow are a list of features you
    can include in your application. Work through them in their order
    :24:
    HOW TO DOWNLOAD A FILE FROM THE SERVER -
    THE CLIENT SIDE:
    Place two Edit boxes, a Button, and a ProgressBar on your Form. You
    can find a ProgressBar under the Win32 tab of the Component Palette.
    Call (change the Name property) your first Edit ‘ToDownload’ and call
    your second Edit ‘DownloadAs’. Clear each Text property.
    You might want to put a couple of Labels on your Form to indicate
    which Edit box is which. Change the ProgressBar’s name to PBar and
    change your Button’s caption to ‘D&ownload’ (& before the o, because
    we already have &D). The Download Button’s OnClick code:
    if ClientSocket1.Active then { If we’re connected to a host }
    begin
    RemoteFile := ToDownload.Text;
    StoredLocalAs := DownloadAs.Text;
    ClientSocket1.Socket.SendText('' + RemoteFile);
    end;
    You will need to declare RemoteFile and StoredLocalAs, as global
    string variables. Global, because later on we will be using them in
    other procedures. The SendText line will be sending something like
    this to the server: c:\file.txt – I’ve used c:\file.txt as
    an example, but it should be plain to see that it is whatever
    RemoteFile has been assigned that will be sent.
    :43:
    THE SERVER SIDE:
    Now you can’t just use ReceiveText here in the OnClientRead event of
    your ServerSocket. That only has the capability of receiving Text
    (strings) - we need to be able to receive other types of data aswell.
    If you make your ServerSocket’s OnClientRead event look like this:
    procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
    Socket: TCustomWinSocket);
    var
    Buf : string;
    MsgLen, {The comma is just a way of declaring two integers }
    LenReceived: integer; {EG: int1,int2,int3: integer; }
    begin
    {1} MsgLen := Socket.ReceiveLength;
    {2} SetLength( Buf, MsgLen );
    {3} LenReceived := Socket.ReceiveBuf( Buf[1], MsgLen );
    {4} Buf := Copy( Buf, 1, LenReceived );
    end;
    I’ll go through each line at a time corresponding to the numbered
    comments I’ve put in.
    1. We are assigning MsgLen with the amount of bytes ready to be sent
    over the connection. We do this using the ReceiveLength function.
    Note that ReceiveLength is only approximate – we might receive less
    data than ReceiveLength returned.
    2. Will are going to need a buffer (place in memory to store the
    incoming data) big enough to store the incoming data. We do this by
    setting the string length of our Buf by the size of the data it is
    about to receive.
    3. Here we are using the ReceiveBuf function to read up to the second
    parameter amount of bytes into Buf. The second parameter (MsgLen)
    indicates how big the Buf is. This function also returns how many
    bytes were actually read, so LenReceived will equal this amount.
    4. This will make sure the Buf string is only as big as necessary.
    Say ReceiveLength returned 80 bytes about to be sent - then the Buf
    length would be set to 80 through point 2 there. We might only have
    received 60 bytes here in one go, and our LenReceived will hold an
    integer value of this amount we have received. We just need to trim
    off any excess Buf length with Copy(), copying from 1 (beginning of
    Buf) to the length of the data actually received (LenReceived).
    ‘Buf’ will contain the data that has arrived. In this case Buf will
    equal: c:\file.txt – if there was a ‘c:\file.txt’ typed
    into our Client’s ToDownload Edit and our Download Button clicked.
    All we need to do is run a check on that Buf to see if there’s our
    in there. I’ve used that as like a tag so we can see what
    the Client wants us to do. Here’s how we could check:
    if Pos('', Buf) = 1 then
    begin
    Delete(Buf, 1, 11); {Delete the tag: }
    RequestedFile := Buf;
    SendClientFile;
    end;
    :13:
    I HOPE YOU Enjoy
    SEE YOU:12:
    لاي استفسار انا موجود

     
حالة الموضوع:
مغلق

مشاركة هذه الصفحة