H
huadao
Unregistered / Unconfirmed
GUEST, unregistred user!
源程序如下,我将ttcpserver控件和tclientserver都设为bmnonblocking
可在client端connect总是失败,请问是怎么回事
unit UnitServer;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, Sockets, StdCtrls;
type
TForm1 = class(TForm)
TcpServer1: TTcpServer;
Button1: TButton;
TcpClient1: TTcpClient;
Memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure FormActivate(Sender: TObject);
procedure TcpServer1Listening(Sender: TObject);
procedure TcpClient1Connect(Sender: TObject);
procedure TcpClient1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
procedure TcpClient1Send(Sender: TObject; Buf: PChar;
var DataLen: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
pbufStrChar;
s,s2:string;
i:integer;
begin
TcpClient1.Active:= not TcpClient1.Active;
TcpClient1.RemoteHost:='10.0.5.254';
TcpClient1.RemotePort:='5000';
s:=trim(memo1.Text);
i:=length(s);
SetLength(s2,i);
System.Move(s[1],s2[1] ,i);
TcpClient1.Active:=True;
if not TcpClient1.connected then
begin
TcpClient1.Open;
TcpClient1.Connect;
end;
TcpClient1.SendBuf(pbufstr,i);
{ try
if TcpClient1.Connect then
TcpClient1.SendBuf(pbufstr,i);
finally
TcpClient1.Disconnect;
end;
}
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var tmpStr:array[0..100] of Char;
begin
ClientSocket.ReceiveBuf(tmpStr,10);
Memo2.Text:=tmpStr;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
TcpServer1.LocalPort:='5000';
TcpServer1.Active:=True;
end;
procedure TForm1.TcpServer1Listening(Sender: TObject);
begin
end;
procedure TForm1.TcpClient1Connect(Sender: TObject);
begin
end;
procedure TForm1.TcpClient1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
begin
end;
procedure TForm1.TcpClient1Send(Sender: TObject; Buf: PChar;
var DataLen: Integer);
var tstr:string;
i:integer;
begin
end;
end.
可在client端connect总是失败,请问是怎么回事
unit UnitServer;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, Sockets, StdCtrls;
type
TForm1 = class(TForm)
TcpServer1: TTcpServer;
Button1: TButton;
TcpClient1: TTcpClient;
Memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
procedure FormActivate(Sender: TObject);
procedure TcpServer1Listening(Sender: TObject);
procedure TcpClient1Connect(Sender: TObject);
procedure TcpClient1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
procedure TcpClient1Send(Sender: TObject; Buf: PChar;
var DataLen: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
pbufStrChar;
s,s2:string;
i:integer;
begin
TcpClient1.Active:= not TcpClient1.Active;
TcpClient1.RemoteHost:='10.0.5.254';
TcpClient1.RemotePort:='5000';
s:=trim(memo1.Text);
i:=length(s);
SetLength(s2,i);
System.Move(s[1],s2[1] ,i);
TcpClient1.Active:=True;
if not TcpClient1.connected then
begin
TcpClient1.Open;
TcpClient1.Connect;
end;
TcpClient1.SendBuf(pbufstr,i);
{ try
if TcpClient1.Connect then
TcpClient1.SendBuf(pbufstr,i);
finally
TcpClient1.Disconnect;
end;
}
end;
procedure TForm1.TcpServer1Accept(Sender: TObject;
ClientSocket: TCustomIpClient);
var tmpStr:array[0..100] of Char;
begin
ClientSocket.ReceiveBuf(tmpStr,10);
Memo2.Text:=tmpStr;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
TcpServer1.LocalPort:='5000';
TcpServer1.Active:=True;
end;
procedure TForm1.TcpServer1Listening(Sender: TObject);
begin
end;
procedure TForm1.TcpClient1Connect(Sender: TObject);
begin
end;
procedure TForm1.TcpClient1Receive(Sender: TObject; Buf: PChar;
var DataLen: Integer);
begin
end;
procedure TForm1.TcpClient1Send(Sender: TObject; Buf: PChar;
var DataLen: Integer);
var tstr:string;
i:integer;
begin
end;
end.