delphi之socket通讯

使用了2个组建:

TServerSocket

TClientSocket

------------------TServerSocket---------------------------

//开启连接

ServerSocket1.Port := 9000;
ServerSocket1.Active := true;

 

//接收客户端发送的消息。对应事件:OnClientRead

Socket.ReceiveText

 

//发送消息给客户端。

ServerSocket1.Socket.Connections[0].SendText(Edit1.Text);

 

----------------------TClientSocket---------------------------------------

//连接服务端

ClientSocket1.Host := '127.0.0.1';
ClientSocket1.Port := 9000;

ClientSocket1.Active := true;

 

//接受服务端发送的消息 。对应事件:OnRead

str := Socket.ReceiveText

 

//发送消息给服务端。

ClientSocket1.Socket.SendText(Edit1.Text);

 

 

下面为完整代码:

 

复制代码
unit Client;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, StdCtrls, ExtCtrls, Sockets;

type
  TFrmClient = class(TForm)
    ClientSocket1: TClientSocket;
    Memo1: TMemo;
    Panel1: TPanel;
    Label2: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    Button1: TButton;
    Edit1: TEdit;
    Label3: TLabel;
    Edit3: TEdit;

    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmClient: TFrmClient;

implementation

{$R *.dfm}

procedure TFrmClient.Button2Click(Sender: TObject);
begin
  if (ClientSocket1.Active = false) then
  begin
    ClientSocket1.Host := Edit3.Text;
    ClientSocket1.Port := strtoint(Edit2.Text);

    ClientSocket1.Active := true;
    Self.Caption := '连接socket服务器成功!';
  end;
end;

procedure TFrmClient.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  // Memo1.Lines.Add('服务器说:' + Socket.ReceiveText);
  if (Memo1.Lines.Count > 50) then
    Memo1.Lines.Clear;

  Memo1.Lines.Add('服务器:' + ' Ip=' + Socket.RemoteAddress + '  ' + datetimetostr
      (now));

  // Socket.RemoteHost +
  Memo1.Lines.Add(Socket.ReceiveText);
  Memo1.Lines.Add('');
end;

procedure TFrmClient.Button1Click(Sender: TObject);
begin
  // ClientSocket1.Socket.SendText(Edit1.Text);
  with ClientSocket1 do
  begin
    if Edit1.Text <> '' then
    begin
      Memo1.Lines.Add('客户端:' + '  ' + datetimetostr(now));
      Socket.SendText(Edit1.Text);
      Memo1.Lines.Add(Edit1.Text);
      Memo1.Lines.Add('');
    end;
  end;
end;

end.
复制代码
复制代码
unit server;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ScktComp;

type
  TFrmMain = class(TForm)
    ServerSocket1: TServerSocket;
    Memo1: TMemo;
    Panel1: TPanel;
    Edit2: TEdit;
    Label2: TLabel;
    Button2: TButton;
    Panel2: TPanel;
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    CheckBox1: TCheckBox;
    procedure ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientError(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure Button2Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmMain: TFrmMain;

implementation

{$R *.dfm}

procedure TFrmMain.Button1Click(Sender: TObject);
begin
  with ServerSocket1 do
  begin
    if Edit1.Text <> '' then
    begin
      Memo1.Lines.Add('服务器:' + '  ' + datetimetostr(now));
      Socket.Connections[0].SendText(Edit1.Text);
      // Socket.Connections[0].RemoteAddress;//IP
      Memo1.Lines.Add(Edit1.Text);
      Memo1.Lines.Add('');
    end;
  end;
end;

procedure TFrmMain.Button2Click(Sender: TObject);
begin
  if Edit2.Text <> '' then
  begin
    try
      ServerSocket1.Port := strtoint(Edit2.Text);
      ServerSocket1.Active := true;
      Self.Caption := 'Socket服务器待命中,端口:' + Edit2.Text;
      Button2.Enabled := False;
    except
      Self.Caption := 'Socket服务器没有准备好';
    end;
  end;
end;

procedure TFrmMain.Edit2Change(Sender: TObject);
begin
  Button2.Enabled := true;
end;

procedure TFrmMain.FormCreate(Sender: TObject);
begin
  Self.Caption := 'Socket服务器';
end;

procedure TFrmMain.ServerSocket1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Memo1.Lines.Add('-----客户端:' + Socket.RemoteAddress + ' 发消息来连接了--');
end;

procedure TFrmMain.ServerSocket1ClientError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0;
end;

procedure TFrmMain.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  stemp: string;
begin
  try
    if (Memo1.Lines.Count > 50) then
      Memo1.Lines.Clear;

    Memo1.Lines.Add('客户端:' + ' Ip=' + Socket.RemoteAddress + '  ' +
        datetimetostr(now));

    // Socket.RemoteHost +
    Memo1.Lines.Add(Socket.ReceiveText);
    Memo1.Lines.Add('');

    if (CheckBox1.Checked = true) then
    begin
      Button1.Click;
      // Socket.SendText(Edit1.Text);
    end;

  except
    Memo1.Lines.Add(datetimetostr(now) + ' :程序异常');
  end;
end;

end.
posted @ 2017-07-28 15:19  陈财明博客  阅读(2037)  评论(0编辑  收藏  举报