procedure Tpost.Execute;
begin
{ Place thread code here }
clientsocket1:=Tclientsocket.Create(nil); //创建clien
clientsocket1.OnConnect:=ClientSocket1Connect; //连接事件
clientsocket1.OnConnecting:=ClientSocket1Connecting; //连接事件
if (form1.Edit1.text<>'') and (form1.Edit2.text<>'') then
begin
if form1.clientsocket1.active=true then
form1.clientsocket1.Close;
headbuff:=form1.memo1.Text; //取内容
contentbuff:=form1.memo2.Text; //取内容
clientsocket1.host:=form1.Edit1.Text;
clientsocket1.Port:=strtoint(form1.Edit2.text);
clientsocket1.Open;
end;
end;
为什么我的OnConnect和OnConnecting无法激活呢?
procedure Tpost.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
form1.statusbar1.SimpleText:='与服务器'+clientsocket1.Address+'成功建立连接';
Socket.SendText(headbuff);
Socket.sendtext(contentbuff);
end;procedure Tpost.ClientSocket1Connecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
form1.statusbar1.SimpleText:='正在与服务器'+clientsocket1.Address+'建立连接';
end;
是不是需要消息循环,但我看了一些文章无法理解其中.希望有人能帮我解答一下.
clientsocket1.OnConnect:=ClientSocket1Connect; //连接事件
clientsocket1.OnConnecting:=ClientSocket1Connecting; //连接事件
这两处。另外,一定要动态建立Tclientsocket吗?
begin
{ Place thread code here }
clientsocket1:=Tclientsocket.Create(nil); //创建clien
clientsocket1.OnConnect:=ClientSocket1Connect; //连接事件
clientsocket1.OnConnecting:=ClientSocket1Connecting; //连接事件
if (form1.Edit1.text<>'') and (form1.Edit2.text<>'') then
begin
if form1.clientsocket1.active=true then
form1.clientsocket1.Close;
headbuff:=form1.memo1.Text; //取内容
contentbuff:=form1.memo2.Text; //取内容
clientsocket1.host:=form1.Edit1.Text;
clientsocket1.Port:=strtoint(form1.Edit2.text);
clientsocket1.Open;
while not Terminated do
begin
{加入你要处理的代码}
Sleep(1);
end;
end;
end;
一下子想不通
while not Terminated do
begin
{加入你要处理的代码}
Sleep(1);
end;
中间的作用域是用来做什么的
begin
{ Place thread code here }
clientsocket1:=Tclientsocket.Create(nil); //创建clien
clientsocket1.OnConnect:=ClientSocket1Connect; //连接事件
clientsocket1.OnConnecting:=ClientSocket1Connecting; //连接事件
if (form1.Edit1.text<>'') and (form1.Edit2.text<>'') then
begin
if form1.clientsocket1.active=true then
form1.clientsocket1.Close;
headbuff:=form1.memo1.Text; //取内容
contentbuff:=form1.memo2.Text; //取内容
clientsocket1.host:=form1.Edit1.Text;
clientsocket1.Port:=strtoint(form1.Edit2.text);
clientsocket1.Open;
//这里添加一个MessageLoop
//例如:
while GetMessage(MsgRec, 0, 0, 0) do begin
TranslateMessage(MsgRec);
DispatchMessage(MsgRec)
end;
Terminate;
end;
end;
while not Terminated do
begin
{加入你要处理的代码}
if form1.Memo3.Text<>'' then
break;
Sleep(1);
end;
end;
form1.Memo3.Lines.Add('终止');