为什么此代码不发送或接收?

发布于 2025-01-19 16:34:52 字数 4079 浏览 0 评论 0原文

我正在尝试使用ttcpblocksocket在GUI程序中发送和接收消息。

我的程序编译,但没有收到或发送任何消息。

也许我的ttcpblocksocket对象在我使用它之前会被销毁吗?

这是客户端:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls,blcksock, Graphics, Dialogs, StdCtrls;

type

  { TCPThread }

  TCPThread = class (TThread)
    protected
       socket:TTCPBlockSocket;
    public
      procedure sendmymsg(msg:string);
      procedure pconnect(ip:string;port:string);
      constructor Create;virtual;
      destructor Destroy; override;
  end;

  { TcpClient }

  TcpClient = class(TCPThread)

    protected
      procedure Execute;override;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  Client:TCPClient;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Client:=TCPClient.Create;
  Client.pconnect('127.0.0.1','5555');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  client.sendmymsg(edit1.text);
  memo1.Lines.add(edit1.Text);
  edit1.clear;
end;


{ TcpClient }

procedure TcpClient.Execute;
var
msg:string;
begin
  Repeat
  msg:='';
  if socket.LastError=0 then
     if socket.CanRead(100) then
        begin
          msg:=socket.RecvString(1);
          form1.memo1.lines.add('server : ' + msg)
        end;
   until msg='exit';
end;

{ TCPThread }

procedure TCPThread.sendmymsg(msg: string);
begin
  socket.SendString(msg);
end;

procedure TCPThread.pconnect(ip: string; port: string);
begin
 socket.Connect(ip,port);
end;


constructor TCPThread.Create;
begin
    inherited Create(false);
    FreeOnTerminate:=true;
    socket:=TTCPBlockSocket.Create;
end;

destructor TCPThread.Destroy;
begin
  FreeAndNil(socket);
  inherited Destroy;
end;

end.

这是服务器端:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls,blcksock, Graphics, Dialogs, StdCtrls;

type

  { TCPThread }

  TCPThread = class (TThread)

    private

    protected
       socket:TTCPBlockSocket;
    public
      procedure sendmymsg(msg:string);
      procedure bindme(ip:string;port:string);
      constructor Create;virtual;
      destructor destroy;override;

  end;

  { TCPServer }

  TCPServer = class(TCPThread)

    public
      procedure Execute;override;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  Server:TCPServer;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
  try
  Server:=TCPServer.Create;
  sleep(100);
  Server.bindme('0.0.0.0','5555');
  except
  memo1.Lines.add('an Erorr occured')
  end;
  finally
     memo1.Lines.add('connection established');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Server.sendmymsg(edit1.text);
  memo1.Lines.add(edit1.Text);
  edit1.clear;
end;

{ TcpClient }

procedure TCPServer.Execute;
var
msg:string;
begin
  Repeat
  msg:='';
  if socket.LastError=0 then
     if socket.CanRead(100) then
        begin
          msg:=socket.RecvString(1);
          form1.memo1.lines.add('server : ' + msg)
        end;
   until msg='exit';
end;

{ TCPThread }

procedure TCPThread.sendmymsg(msg: string);
begin
  socket.SendString(msg);
end;

procedure TCPThread.bindme(ip: string; port: string);
begin
 socket.Bind(ip,port);
end;

constructor TCPThread.Create;
begin
    inherited Create(false);
    FreeOnTerminate:=true;
    socket:=TTCPBlockSocket.Create;
end;

destructor TCPThread.destroy;
begin
  FreeAndNil(socket);
  inherited destroy;
end;

end. 

您可以看到我是基于服务器和客户端以相同的原理为基础。

I am trying to use TTCPBlockSocket to send and receive messages in a GUI program.

My program compiles, but no messages are being received or sent.

Maybe my TTCPBlockSocket object is destroyed before I use it?

This is the Client side:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls,blcksock, Graphics, Dialogs, StdCtrls;

type

  { TCPThread }

  TCPThread = class (TThread)
    protected
       socket:TTCPBlockSocket;
    public
      procedure sendmymsg(msg:string);
      procedure pconnect(ip:string;port:string);
      constructor Create;virtual;
      destructor Destroy; override;
  end;

  { TcpClient }

  TcpClient = class(TCPThread)

    protected
      procedure Execute;override;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  Client:TCPClient;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Client:=TCPClient.Create;
  Client.pconnect('127.0.0.1','5555');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  client.sendmymsg(edit1.text);
  memo1.Lines.add(edit1.Text);
  edit1.clear;
end;


{ TcpClient }

procedure TcpClient.Execute;
var
msg:string;
begin
  Repeat
  msg:='';
  if socket.LastError=0 then
     if socket.CanRead(100) then
        begin
          msg:=socket.RecvString(1);
          form1.memo1.lines.add('server : ' + msg)
        end;
   until msg='exit';
end;

{ TCPThread }

procedure TCPThread.sendmymsg(msg: string);
begin
  socket.SendString(msg);
end;

procedure TCPThread.pconnect(ip: string; port: string);
begin
 socket.Connect(ip,port);
end;


constructor TCPThread.Create;
begin
    inherited Create(false);
    FreeOnTerminate:=true;
    socket:=TTCPBlockSocket.Create;
end;

destructor TCPThread.Destroy;
begin
  FreeAndNil(socket);
  inherited Destroy;
end;

end.

This is the Server side:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls,blcksock, Graphics, Dialogs, StdCtrls;

type

  { TCPThread }

  TCPThread = class (TThread)

    private

    protected
       socket:TTCPBlockSocket;
    public
      procedure sendmymsg(msg:string);
      procedure bindme(ip:string;port:string);
      constructor Create;virtual;
      destructor destroy;override;

  end;

  { TCPServer }

  TCPServer = class(TCPThread)

    public
      procedure Execute;override;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  Server:TCPServer;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
  try
  Server:=TCPServer.Create;
  sleep(100);
  Server.bindme('0.0.0.0','5555');
  except
  memo1.Lines.add('an Erorr occured')
  end;
  finally
     memo1.Lines.add('connection established');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Server.sendmymsg(edit1.text);
  memo1.Lines.add(edit1.Text);
  edit1.clear;
end;

{ TcpClient }

procedure TCPServer.Execute;
var
msg:string;
begin
  Repeat
  msg:='';
  if socket.LastError=0 then
     if socket.CanRead(100) then
        begin
          msg:=socket.RecvString(1);
          form1.memo1.lines.add('server : ' + msg)
        end;
   until msg='exit';
end;

{ TCPThread }

procedure TCPThread.sendmymsg(msg: string);
begin
  socket.SendString(msg);
end;

procedure TCPThread.bindme(ip: string; port: string);
begin
 socket.Bind(ip,port);
end;

constructor TCPThread.Create;
begin
    inherited Create(false);
    FreeOnTerminate:=true;
    socket:=TTCPBlockSocket.Create;
end;

destructor TCPThread.destroy;
begin
  FreeAndNil(socket);
  inherited destroy;
end;

end. 

You can see that I am basing the server and the client on the same principle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2025-01-26 16:34:52

您的服务器需要致电 Code> 在调用ttcpblocksocket.bind()之后,然后使用tsocket accept() returns retally read/nest数据。

Your server needs to call TTCPBlockSocket.Listen() and TTCPBlockSocket.Accept() after calling TTCPBlockSocket.Bind(), and then read/send data using the TSocket that Accept() returns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文