谁帮我看看我的client的代码, 用的ICS的WSocket
怎么不发送数据到服务器?
unit SktThr;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,WSocket;
type
TSktThread = class(TThread)
private
FProgress : String;
procedure UpdateStatus;
procedure ShowProgress;
procedure Progress(Msg : String);
procedure DataAvailable(Sender: TObject; Error: Word); //接收到数据
published
procedure Setup(i: Integer);
procedure Execute; override;
public
FAddr : String;
FPort : String;
FThreadNumber : Integer;
FWSocket : TWSocket;
Success : Boolean;
end;
implementation
uses Unit1;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSktThread.Setup(i: Integer);
begin
FThreadNumber := i;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSktThread.UpdateStatus;
begin
SktThreadForm.ProcessResults(FThreadNumber, Success);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSktThread.ShowProgress;
begin
SktThreadForm.ProgressListBox.lines.Add(FProgress);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSktThread.Progress(Msg : String);
begin
FProgress := Msg;
SynChronize(ShowProgress);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSktThread.Execute;
begin
FWSocket := TWSocket.Create(Nil);
FWSocket.MultiThreaded := TRUE;
FWSocket.OnDataAvailable := DataAvailable;
while not Terminated do begin
Progress(IntToStr(FThreadNumber) + ' Start send');
with FWSocket do begin
Addr := FAddr;
Port := FPort;
try
Connect; // Connect to Server
Success := TRUE;
Progress(IntToStr(FThreadNumber) + ' Connect server successfully!');
SendStr('hello,my server!');
except
Success := FALSE;
end;
end;
// while 1=1 do
// begin
// SendStr('hello,my server!');
// sleep(200);
// end;
if not Terminated then
Synchronize(UpdateStatus);
end;
FWSocket.Free;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TSktThread.DataAvailable(Sender: TObject; Error: Word);
var
Buf : PChar;
Cli : TWSocket;
Len : Integer;
Cnt : Integer;
begin
Cli := Sender as TWSocket;
Cnt := Cli.RcvdCount;
if Cnt <= 0 then
Exit;
{$IFDEF VER80}
{ Delphi 1 has 255 character limit of strings (StrPas below) }
if Cnt > 254 then
Cnt := 254;
{$ENDIF}
GetMem(Buf, Cnt + 1);
try
Len := Cli.Receive(Buf, Cnt);
if Len > 0 then begin
Buf[Cnt] := #0;
Progress('线程号:'+IntToStr(FThreadNumber)+' Received: ' + StrPas(Buf));
end;
finally
FreeMem(Buf, Cnt + 1);
end;
end;
end.