clientsocket的clienttype问题(100分)

  • 主题发起人 主题发起人 hdf001
  • 开始时间 开始时间
H

hdf001

Unregistered / Unconfirmed
GUEST, unregistred user!
语句如下:
clientsocket1.Active:=false;
clientsocket1.Address:='202.204.59.*';
clientsocket1.Port:=1024;
clientsocket1.Active:=true;
clientsocket1.Socket.SendText('123');
当设定clientsocket1的clienttype属性为ctBlocking时,以上语句可以正常运行;
当设定clientsocket1的clienttype属性为ctNonBlocking时,以上语句不能正常运行,clientsocket1的active无法设为true,请问是为什么?

 
Server端与客户端设置的不一致。如果为Blocking 都为Blocking;修改服务器端的属性。
 
client设为ctblocking,server设为stnonblocking是正常的
 
ctBlocking和ctNonBlocking可以理解为同步和异步,同步是堵塞的,就是说这个操作如果
不完成,就一直等待,异步则是提交了这个过程,有没有结果都往下走了。

这样就可以解释你的问题了,ctBlocking方式时,socket总是被set active后才调用
sendtext,所以是成功的,如果是ctNonBlocking,socket可能没有被active,这时是不能
调用sendtext的。如果你这样做,应该也是可以的:
clientsocket1.Active:=true;
[red]Sleep(2000);[/red] //加时间延时,等待socket状态被设置为true;
clientsocket1.Socket.SendText('123');
 
clientsocket的clienttype设置为ctNonBlocking后,
加入Sleep(2000);后,语句运行到最后一句,察看clientsocket1.active的值依然为false,同时serversocket1的onclientread时间也不触发,无法发收消息,请问为什么?

clientsocket1.Active:=false;
clientsocket1.Address:='202.204.59.*';
clientsocket1.Port:=1024;
clientsocket1.Active:=true;
Sleep(2000);
clientsocket1.Socket.SendText('123');

 
将程序的原代码发过来:xuqiang@dsideal.com
 

clientsocket设置为ctBlocking,serversocket设置为stNonBlocking时,程序正常
clientsocket设置为ctNonBlocking,serversocket仍设置为stNonBlocking时,程序不正常
clientsocket1.active总为false.

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
procedure Button1Click(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.Active:=false;
clientsocket1.Host:='hdf';//ÎҵļÆËã»úÃû
clientsocket1.Port:=1024;
clientsocket1.Active:=true;
clientsocket1.Socket.SendText(memo1.Text);
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
memo2.Lines.Add(socket.ReceiveText);
end;
end.
 
问题已解决,邮件发出,请接收。。
 
unit CoolTools;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Button2: TButton;
procedure Button1Click(Sender: TObject);

procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);

private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.Address :='210.47.16.133';//我的计算机名
clientsocket1.Active:=true;
end;




procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
Text:String;
begin
Text := Socket.ReceiveText;
memo2.Lines.Add(Text);
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Socket.SendText(Memo1.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Active := true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
clientsocket1.Socket.SendText(Memo1.Text);
end;

end.
 
谢谢goeasy,关键是把clientsocket1.socket.sendtext('内容')转移到"ClientSocket1Connect"事件中执行
 
后退
顶部