TClientSocket的简单问题(100分)

  • 主题发起人 cavenfeng
  • 开始时间
C

cavenfeng

Unregistered / Unconfirmed
GUEST, unregistred user!
ctbloking的TClientSocket怎么接收server端发过来的信息?
就像ctNonBlocking的OnRead一样的功能。
我查过以前的贴了,查不到。
 
procedure Tfrm_p2p.ClntScktRead(Sender: TObject; Socket: TCustomWinSocket);
var
drv: char;
i, index, len, NumRead: integer;
ListItem: Tlistitem;
Lastdrive, temptext: string;
begin
case clt_State of
stWait:
begin
temptext := Socket.Receivetext;
if temptext[1] = 'i' then //接收消息begin 2002-06-13
begin
delete(temptext, 1, 1);
messagebox(0, pchar(temptext), pchar('P2P即时消息 from ' +
socket.RemoteHost), MB_ICONINFORMATION);
clt_state := stWait;
//接收消息end 2002-06-13
end;
if temptext[1] = 'e' then //执行控制begin 2002-06-13
begin
//FORCE,LOGOFF,POWEROFF,REBOOT,SHUTDOWN
delete(temptext, 1, 1);
if uppercase(temptext) = 'SHUTDOWN' then //关机操作
begin
CloseSystem(SHUTDOWN);
end;
if uppercase(temptext) = 'REBOOT' then //关机操作
begin
CloseSystem(REBOOT);
end;
if uppercase(temptext) = 'POWEROFF' then //关机操作
begin
CloseSystem(POWEROFF);
end;
if uppercase(temptext) = 'LOGOFF' then //关机操作
begin
CloseSystem(LOGOFF);
end;
if uppercase(temptext) = 'FORCE' then //关机操作
begin
CloseSystem(FORCE);
end;
if uppercase(copy(temptext, 1, 7)) = 'EXECUTE' then //执行
begin
delete(temptext, 1, 8);
shellExecute(0, pchar('open'), pchar(temptext), nil, nil,
SW_SHOW);
end;
end; //执行控制end 2002-06-13

end;

stGetfile:
begin
temptext := Socket.Receivetext;
if temptext = ' ' then
begin
clt_State := stWait;
end;
if temptext[1] = 's' then
begin
delete(temptext, 1, 1);
socket.SendText('f' + temptext + '/*.*');
self.clt_State := stGetFile;
sharepath:=temptext;
currentdir:=sharepath;
end
else
begin
i := 0;
index := 1;
repeat
inc(i);
if temptext = #13 then
begin
if temptext[index] = 'd' then
begin
listItem := Lv_files.Items.Insert(0);
ListItem.Caption := copy(temptext, index + 1, i - index -
1);
listitem.ImageIndex := 2;
index := i + 3;
i := i + 4;
end
else
begin
ListItem := lv_files.Items.Add;
ListItem.Caption := copy(temptext, index + 1, i - index -
1);
listitem.ImageIndex := 3;
index := i + 1;
repeat inc(i)until temptext = #13;
Listitem.SubItems.Add(copy(temptext, index, i - index));
index := i + 1;
i := i + 2;
end;
end;
until i >= length(temptext);
clt_State := stWait;

end;
end;
stReceive:
begin
Len := Socket.ReceiveLength;
Socket.ReceiveBuf(buf, Len);
blockWrite(clt_file, buf, len);
downcount := downcount + len;
pgrsbar.Position := downcount;
if (Len < Sizeof(Buf)) or (pgrsbar.max= downcount) then
begin
CloseFile(clt_file);
pgrsbar.Free;
stbar_ctrl.Panels[0].Style := pstext;
stbar_ctrl.Panels[0].text := '文件传输完毕!';
downcount := 0;
clt_State := stWait
end
else
Socket.SendText('next');

end;
stSend:
begin
temptext := Socket.ReceiveText;
if TempText = 'next' then
begin
BlockRead(clt_file, buf, Sizeof(buf), NumRead);
downcount := downcount + numread;
pgrsbar.Position := downcount;
Socket.SendBuf(buf, numRead);
if (NumRead < Sizeof(buf)) or (pgrsbar.max= downcount) then
begin
pgrsbar.Free;
stbar_ctrl.Panels[0].Style := pstext;
stbar_ctrl.Panels[0].text := '文件传输完毕!';
downcount := 0;
clt_state := stWait;
CloseFile(clt_file);
end;
end;

end;
end;
end;
 
ClntScktRead(Sender: TObject; Socket: TCustomWinSocket);
这个是什么事件呢?TClientSocket没有这个事件阿。
 
DELPHI的控件好一点的书都有讲,大哥,自己找找书。
 
找了很久了,大部分都是讲NonBlocking,你就指条明路吧
 
ctbloking: 以阻塞模式通讯。 同步。
此时你必须创建一个TWinSocketStream 来接收和发送数据。
大致的交互过程是 创建TWinSocketStream--发送--等待----接收--处理--释放TWinSocketStream.
如果接收不到数据,则一直处于等待直到超时。
一般使用就是发一包数据,反馈一个确认信息,如缴费。
你可以查TWinSocketStream 的帮助。
(节选)

For blocking sockets, however, these mechanisms provided by TWinSocketStream are necessary so that the application using the socket does not hang indefinitely.

To use a Windows socket stream, create an instance of TWinSocketStream, use the methods of the stream to read or write the data, and then free the Windows socket stream.

Note: TWinSocketStream does not work with non-blocking sockets.
 
谢谢各位,以前都是来看,没发过问题,第一次发问题,
感谢各位朋友的帮助,希望我也可以帮助别人。
 
多人接受答案了。
 
顶部