delphi之socket通讯

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

 

以上是关于delphi之socket通讯的主要内容,如果未能解决你的问题,请参考以下文章

golang代码片段(摘抄)

网络通讯之Socket-Tcp

delphi学习笔记2之动态库调用和串口通讯

iOS开发之即时通讯之Socket(AsyncSocket)

iOS开发之即时通讯之Socket(AsyncSocket)

Unity3D网络通讯--Socket通讯之Tcp通讯