如何将clientsocket组件加在动态库中实现与服务端的通信

zzbinsky 2003-12-18 10:48:09
各位大虾:

小弟想要做个动态库,在其中加载clientsocket控件,使得能够发送指令给server端和接收server端返回的执行结果。望各位大虾多多指教!小弟不胜感激!分不够再加,最好能给出代码
...全文
14 点赞 收藏 7
写回复
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzbinsky 2003-12-20
感谢大家的支持,我的问题已解决,代码如下:
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;
回复
CaoChengLu 2003-12-18
在DLL中创建Form,应该将应用程序的句柄传递给DLL的TApplication对象。而且,Form的拥有者应该是Application。下面是《Delphi 4 开发大全》上的示例:
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;
回复
starcbh 2003-12-18
太简单了
回复
sunhans 2003-12-18
我在开发DLL时采用的是Socket API,直接用Send和Recv向服务器端发送和接收,控制比较简单,适合在一个事务内完成。
回复
wolf2005 2003-12-18
你检查一下有没有连上你的服务端?
回复
zzbinsky 2003-12-18
高人都那里去了???
回复
zzbinsky 2003-12-18
我在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?
又怎么将服务端的返回值接收到并传给调用者?
回复
发动态
发帖子
网络通信/分布式开发
创建于2007-08-02

1565

社区成员

Delphi 网络通信/分布式开发
申请成为版主
社区公告
暂无公告