Introduction to Sockets


 

Introduction

This exercise, adapted from a Delphi article on about.com, shows a simple way to send the contents of one control on one computer to a control on another computer. It is simply an introduction to sockets programming.

This exercise assumes that you have two computers and they can communicate using IP addresses.

The Client Application

 

 
  1. Start Borland C++ Builder with the default form
  2. From the Standard tab of the Component Palette, add an Edit control
  3. From the Internet tab of the Component Palette, click the ServerSocket button and click on the form:
     
  4. Double-click an empty area on the form to access its OnCreate event
  5. Implement the event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
        ServerSocket1->Port = 23;
        ServerSocket1->Active = True;
    }
    //---------------------------------------------------------------------------
  6. On the Events tab of the Object Inspector, double-click the event of the OnClose field and implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
        ServerSocket1->Active = False;
    }
    //---------------------------------------------------------------------------
  7. On the form, click the ServerSocket1 control
  8. On the Events tab of the Object Inspector, double-click the OnClientRead event
  9. Implement the event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
          TCustomWinSocket *Socket)
    {
        Edit1->Clear();
        Edit1->Text = Socket->ReceiveText();
    }
    //---------------------------------------------------------------------------
  10. Save the project as Communication1 in a new folder named Communication1
  11. To create the application so it can execute on another computer, on the main menu, click Project -> Options...
  12. Click the Linker tab
  13. Uncheck the Use Dynamic RTL check box
  14. Click the Packages tab
  15. Uncheck the Build With Runtime Packages  check box
  16. Click OK
  17. Save everything
  18. Press F9 to execute the application
  19. Distribute the application to the computer that will receive the message. It is small enough so you can copy it to a floppy disk or you can put it in a shared directory
  20. Get the IP address of the computer you will send the messages to. You can go to the computer or ask somebody to get it. You can get the IP address at the Command Prompt by typing ipconfig and pressing Enter. Write that IP address down

The Server

  1. Start a new C++ Builder with the default form
  2. Save the project in a new folder
  3. From the Standard tab of the Component Palette, add an Edit control and a Button. Change the Caption of the button to &Send
  4. From the Internet tab of the Component Palette, click the ClientSocket button and click on the form:
     
  5. Double-click an empty area on the form to access its OnCreate event
  6. Implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
        ClientSocket1->Port = 23;
        ClientSocket1->Host = "Type the Above IP Address Here";
        ClientSocket1->Active = True;
    }
    //---------------------------------------------------------------------------
  7. On the Events tab of the Object Inspector, double-click the event of the OnClose field and implement it as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
        ClientSocket1->Active = False;
    }
    //---------------------------------------------------------------------------
  8. On the form, double-click the button control and implement the event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        if( ClientSocket1->Active )
            ClientSocket1->Socket->SendText(Edit1->Text);
    }
    //---------------------------------------------------------------------------
  9. Save the project.
  10. Execute the application. Type some text and click Send

 


Copyright 2003-2007 FunctionX, Inc.