为什么此代码不发送或接收?
我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的服务器需要致电 和 Code> 在调用
ttcpblocksocket.bind()
之后,然后使用tsocket
accept()
returns retally read/nest数据。Your server needs to call
TTCPBlockSocket.Listen()
andTTCPBlockSocket.Accept()
after callingTTCPBlockSocket.Bind()
, and then read/send data using theTSocket
thatAccept()
returns.