• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Client/Server on TCP/IP

Greetings...
I Need a very clear example of how to send and receive (commands and strings) on a Server and a Client ?

Is it diffrent if I use Delphi Borland 2005 or Delphi Borland 2006?!?

Thank you for your time.
0
liljen_80
Asked:
liljen_80
  • 3
  • 3
2 Solutions
 
peterkiersCommented:
HI, I'm no expert, but you where looking for the same as I did.
and I found this URL:

http://www.felix-colibri.com/papers/web/socket_programming/socket_programming.html

An explenation about Winsock and at the bottom of the page
there a 2 examples:

Client_Socket.zip
Server_Socket.zip

Maybe this helps.

Peter
0
 
Scay7Commented:
heres a demo that i got it sure shows you how to do what you want... using standard delphi sockets...

//Demo by TheRealLoki
unit sockettestmain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ScktComp;
type
  TForm2 = class(TForm)
    mLog: TMemo;
    ClientSocket1: TClientSocket;
    gbServer: TGroupBox;
    rbServer: TRadioButton;
    ServerSocket1: TServerSocket;
    eServerPort: TEdit;
    Label1: TLabel;
    bListen: TButton;
    eServerSend: TEdit;
    bServerSendToAll: TButton;
    gbClient: TGroupBox;
    Label2: TLabel;
    eClientPort: TEdit;
    bConnect: TButton;
    eClientSend: TEdit;
    bClientSend: TButton;
    rbClient: TRadioButton;
    Label3: TLabel;
    eAddress: TEdit;
    bOffline: TButton;
    bDisconnect: TButton;
    procedure bListenClick(Sender: TObject);
    procedure bServerSendToAllClick(Sender: TObject);
    procedure rbServerClick(Sender: TObject);
    procedure rbClientClick(Sender: TObject);
    procedure bConnectClick(Sender: TObject);
    procedure bClientSendClick(Sender: TObject);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure bOfflineClick(Sender: TObject);
    procedure bDisconnectClick(Sender: TObject);
    procedure ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Connecting(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Disconnect(Sender: TObject;
      Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

IMPLEMENTATION

{$R *.DFM}

procedure TForm2.rbServerClick(Sender: TObject);
    begin
        gbServer.Enabled := True;
        gbClient.Enabled := False;
    end;

procedure TForm2.rbClientClick(Sender: TObject);
    begin
        gbServer.Enabled := False;
        gbClient.Enabled := True;
    end;


// ********************************     server code here
procedure TForm2.bListenClick(Sender: TObject);
    begin
        ServerSocket1.Port := StrToIntDef(eServerPort.Text, 2000);
        ServerSocket1.Active := True;
    end;

procedure TForm2.bOfflineClick(Sender: TObject);
    begin
        ServerSocket1.Active := True;
    end;

procedure TForm2.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add('connection from ' + Socket.RemoteAddress);
        mLog.Perform(EM_SCROLL,SB_LINEDOWN,0);
    end;

procedure TForm2.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add('disconnection from ' + Socket.RemoteAddress);
        mLog.Perform(EM_SCROLL,SB_LINEDOWN,0);
    end;

procedure TForm2.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add(Socket.ReceiveText);
    end;

procedure TForm2.bServerSendToAllClick(Sender: TObject);
    var
        i: integer;
    begin
        for i := 0 to pred(ServerSocket1.Socket.ActiveConnections) do
         ServerSocket1.Socket.Connections[i].SendText(eServerSend.Text);
    end;

// ********************************     client code here
procedure TForm2.bConnectClick(Sender: TObject);
    begin
        ClientSocket1.Address := eAddress.Text;
        ClientSocket1.Port := StrToIntDef(eClientPort.Text, 2000);
        ClientSocket1.Open;
    end;

procedure TForm2.bClientSendClick(Sender: TObject);
    begin
        ClientSocket1.Socket.SendText(eClientSend.Text);
    end;

procedure TForm2.bDisconnectClick(Sender: TObject);
    begin
        ClientSocket1.Close;
    end;

procedure TForm2.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add(Socket.ReceiveText);
        mLog.Perform(EM_SCROLL,SB_LINEDOWN,0);
    end;

procedure TForm2.ClientSocket1Connecting(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add('connecting to ' + ClientSocket1.Address + ':' + IntToStr(ClientSocket1.Port));
        mLog.Perform(EM_SCROLL,SB_LINEDOWN,0);
    end;

procedure TForm2.ClientSocket1Connect(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add('connected to ' + ClientSocket1.Address + ':' + IntToStr(ClientSocket1.Port));
        mLog.Perform(EM_SCROLL,SB_LINEDOWN,0);
    end;

procedure TForm2.ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
    begin
        mLog.lines.add('disconnected from ' + ClientSocket1.Address + ':' + IntToStr(ClientSocket1.Port));
        mLog.Perform(EM_SCROLL,SB_LINEDOWN,0);
    end;

end.


********************************** FORM FOLLOWS
********************************** FORM FOLLOWS


object Form2: TForm2
  Left = 345
  Top = 300
  BorderStyle = bsSingle
  Caption = 'Form2'
  ClientHeight = 323
  ClientWidth = 641
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDefault
  PixelsPerInch = 96
  TextHeight = 13
  object mLog: TMemo
    Left = 372
    Top = 48
    Width = 261
    Height = 265
    TabOrder = 0
  end
  object gbServer: TGroupBox
    Left = 16
    Top = 40
    Width = 337
    Height = 105
    Caption = 'Server'
    TabOrder = 1
    object Label1: TLabel
      Left = 16
      Top = 28
      Width = 19
      Height = 13
      Caption = 'Port'
    end
    object eServerPort: TEdit
      Left = 72
      Top = 24
      Width = 41
      Height = 21
      TabOrder = 0
      Text = '2000'
    end
    object bListen: TButton
      Left = 224
      Top = 16
      Width = 75
      Height = 25
      Caption = 'Listen'
      TabOrder = 1
      OnClick = bListenClick
    end
    object eServerSend: TEdit
      Left = 8
      Top = 72
      Width = 193
      Height = 21
      TabOrder = 2
      Text = 'Text to send from server'
    end
    object bServerSendToAll: TButton
      Left = 224
      Top = 72
      Width = 75
      Height = 25
      Caption = 'Send to all'
      TabOrder = 3
      OnClick = bServerSendToAllClick
    end
    object bOffline: TButton
      Left = 224
      Top = 40
      Width = 75
      Height = 25
      Caption = 'Offline'
      TabOrder = 4
      OnClick = bOfflineClick
    end
  end
  object rbServer: TRadioButton
    Left = 16
    Top = 8
    Width = 113
    Height = 17
    Caption = 'Server'
    TabOrder = 2
    OnClick = rbServerClick
  end
  object gbClient: TGroupBox
    Left = 16
    Top = 176
    Width = 337
    Height = 137
    Caption = 'Client'
    TabOrder = 3
    object Label2: TLabel
      Left = 16
      Top = 44
      Width = 19
      Height = 13
      Caption = 'Port'
    end
    object Label3: TLabel
      Left = 16
      Top = 20
      Width = 38
      Height = 13
      Caption = 'Address'
    end
    object eClientPort: TEdit
      Left = 72
      Top = 40
      Width = 41
      Height = 21
      TabOrder = 1
      Text = '2000'
    end
    object bConnect: TButton
      Left = 224
      Top = 16
      Width = 75
      Height = 25
      Caption = 'Connect'
      TabOrder = 2
      OnClick = bConnectClick
    end
    object eClientSend: TEdit
      Left = 8
      Top = 88
      Width = 193
      Height = 21
      TabOrder = 3
      Text = 'Text to send from server'
    end
    object bClientSend: TButton
      Left = 224
      Top = 88
      Width = 75
      Height = 25
      Caption = 'Send'
      TabOrder = 4
      OnClick = bClientSendClick
    end
    object eAddress: TEdit
      Left = 72
      Top = 16
      Width = 113
      Height = 21
      TabOrder = 0
      Text = '127.0.0.1'
    end
    object bDisconnect: TButton
      Left = 224
      Top = 40
      Width = 75
      Height = 25
      Caption = 'Disconnect'
      TabOrder = 5
      OnClick = bDisconnectClick
    end
  end
  object rbClient: TRadioButton
    Left = 16
    Top = 152
    Width = 113
    Height = 17
    Caption = 'Client'
    Checked = True
    TabOrder = 4
    TabStop = True
    OnClick = rbClientClick
  end
  object ClientSocket1: TClientSocket
    Active = False
    ClientType = ctNonBlocking
    Host = 'localhost'
    Port = 2000
    OnConnecting = ClientSocket1Connecting
    OnConnect = ClientSocket1Connect
    OnDisconnect = ClientSocket1Disconnect
    OnRead = ClientSocket1Read
    Left = 356
    Top = 164
  end
  object ServerSocket1: TServerSocket
    Active = False
    Port = 2000
    ServerType = stNonBlocking
    OnClientConnect = ServerSocket1ClientConnect
    OnClientDisconnect = ServerSocket1ClientDisconnect
    OnClientRead = ServerSocket1ClientRead
    Left = 360
    Top = 88
  end
end

Peace Scay7
0
 
liljen_80Author Commented:
I'm wondering how too deside witch port too use. In the exemple peterkiers links too it is used 5678. Is is no problems too use that port?
Which port is the ones you DONT want too use. And witch too use?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
liljen_80Author Commented:
Is it not anybody that have an example of a Server/Client that only sends and recives strings???
0
 
Scay7Commented:
Erm yes... the example i showed you...

with this mod the server receives a command 'date' then send back to client the DATE

procedure TForm2.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var com : string;
begin
com := Socket.ReceiveText;
  if com = 'date' then
  ClientSocket1.Socket.SendText(formatdatetime('hh:nn:ss dd-mm-yyyy',now));    
end;
0
 
liljen_80Author Commented:
But the problem with your eqample Scay7 is that the components TServerSocket and TClientSocket dosent exists in Borland Delphi 2005 or Borland Delphi 2006. So how too do it then?
0
 
Scay7Commented:
Oh lol sorry about that thought it would be... im using D7 so i figured that it should have it by default...
Hmm okay then try this...

dclsockets70.bpl or something similar cause when i started with Sockets i had to first install this .bpl which came standard with Delphi7 but wasnt installed yet... mabey yours isnt either ?

So look for the file   *dclsockets*.bpl*

K so here are the steps if you find it...
         
          Goto
          a. component
          b. install packages
          c. click ADD
          d. goto BIN directory in the root of delphi usually (c:\program files\borland\delphi7\bin)
          e. search for dclsockets*.bpl, click on it, and click OPEN
          f. It should have added "Borland Sockets Components" to make sure click on the components button
             and a TClientSocket & TServerSocket should be there

Peace Scay7
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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