我在dll中建了一个form,其上放了clientsocket控件,写了接收事件,在dll中有如下过程: procedure SendCommand(Ip: string;Port: integer;S:string); var frmsoc:Tfrmsoc; begin frmsoc:=Tfrmsoc.Create(frmsoc); frmsoc.InitiateAction; frmsoc.ClientSocket1.Address:=Ip; frmsoc.ClientSocket1.Port:=Port; if not frmsoc.ClientSocket1.Active then frmsoc.ClientSocket1.Active:=true; frmsoc.ClientSocket1.Socket.SendText(S); frmsoc.ClientSocket1.Active:=false; frmsoc.ClientSocket1.Close; frmsoc.Free;end; 为什么服务端接收不到字符串s? 又怎么将服务端的返回值接收到并传给调用者?
procedure SendCommand(Ip: string;Port: integer;S:string);
var frmsoc:Tfrmsoc;
begin
frmsoc:=Tfrmsoc.Create(frmsoc);
frmsoc.InitiateAction;
frmsoc.ClientSocket1.Address:=Ip;
frmsoc.ClientSocket1.Port:=Port;
if not frmsoc.ClientSocket1.Active then
frmsoc.ClientSocket1.Active:=true;
frmsoc.ClientSocket1.Socket.SendText(S); frmsoc.ClientSocket1.Active:=false;
frmsoc.ClientSocket1.Close;
frmsoc.Free;end;
为什么服务端接收不到字符串s?
又怎么将服务端的返回值接收到并传给调用者?
functoin xxxx(AHandle: THandle; ACaption: String): LongInt;
var
DLLForm: TDLLForm;
begin
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
Result := LongInt(DLLForm);
DLLForm.Caption := ACaption;
DLLForm.Show;
end;
var
SocketReadEvt :TEvent;type
TSocketForm = class(TWinControl)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
public
RedText:String;
procedure OnClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket); end;constructor TSocketForm.Create(AOwner: TComponent);
begin
inherited ;
end;destructor TSocketForm.Destroy;
begin
Inherited;
end;
//接收返回值
procedure TSocketForm.OnClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
RedText:= Socket.ReceiveText;
SocketReadEvt.SetEvent();
end;//发送命令
Function SendCommand(Ip: string;Port: integer;S:string):String;
var
AClientSocket:TClientSocket;
frm:TSocketForm;
buf:Array of char;
begin
Result := '';
SocketReadEvt :=TEvent.Create(nil,False,False,'');
AClientSocket := TClientSocket.Create(Application);
frm := TSocketForm.Create(Application);
try
AClientSocket.Address := IP;
AClientSocket.Port := port;
AClientSocket.Active := True;
AClientSocket.OnRead := frm.OnClientSocketRead; while not (AClientSocket.Active = True) do
Application.ProcessMessages; setLength(Buf,Length(s)+2);
Buf[0] := char($03);
StrCopy(@Buf[1],PCHAR(s));
Buf[Length(Buf)-1] := Char($0D);
AClientSocket.Socket.SendBuf(Buf[0],Length(Buf));
//AClientSocket.Socket.SendText(S); while not (SocketReadEvt.WaitFor(0) = wrSignaled) do
Application.ProcessMessages; SetLength(Result,Length(frm.RedText));
Result := frm.RedText;
finally
AClientSocket.Free;
SocketReadEvt.Free ;
frm.Free();
end;
end;