Why is this code not sending or receiving?

167 views Asked by At

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.

1

There are 1 answers

3
Remy Lebeau On

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.