温
温柔的邂逅
Unregistered / Unconfirmed
GUEST, unregistred user!
把tmemorystream保存到文件用m.SaveToFile(ss)// ss为文件名 可以吗
我的代码
var
Form1: TForm1;
c:longint;
m:tmemorystream;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
clientsocket1.Close;
clientsocket1.Host:=edit1.text;
clientsocket1.Open; //连接服务端
except
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage(edit1.text+'通讯成功!');
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
caption:='连接'+edit1.text+'失败';
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
errorcode:=0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
buffer:array [0..10000] of byte; //设置接收缓冲区
len:integer;
ll,ss,hao:string;
begin
ss := 'D:/standby/as.exe';
if c=0 then //C为服务端发送的字节数,如果为0表示为尚未开始接收
begin
ll:=socket.ReceiveText;
c:=strtoint(ll); //设置需接收的字节数
clientsocket1.Socket.SendText('okok');
begin
len:=socket.ReceiveLength; //读出包长度
label1.Caption:=inttostr(c);
socket.ReceiveBuf(buffer,len); //接收数据包并读入缓冲区内
m.Write(buffer,len); //追加入流M中
if m.Size>=c then //如果流长度大于需接收的字节数,则接收完毕
begin
m.Position:=0;
try
m.SaveToFile(ss);
finally //以下为清除工作
clientsocket1.Active:=false;
clientsocket1.Active:=true;
m.Clear;
c:=0;
end;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m:=TMemorystream.Create; //建立内存流
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
m.free;
ClientSocket1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
clientsocket1.Socket.SendText('gets'); //发送申请,通知服务端
end;
end;
此为客户端的代码 我接受很长时间也不见完成
我的代码
var
Form1: TForm1;
c:longint;
m:tmemorystream;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
clientsocket1.Close;
clientsocket1.Host:=edit1.text;
clientsocket1.Open; //连接服务端
except
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
end;
end;
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
showmessage(edit1.text+'通讯成功!');
end;
procedure TForm1.ClientSocket1Error(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
caption:='连接'+edit1.text+'失败';
showmessage(edit1.text+#13#10+'未开机或未安装服务程序');
errorcode:=0;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
buffer:array [0..10000] of byte; //设置接收缓冲区
len:integer;
ll,ss,hao:string;
begin
ss := 'D:/standby/as.exe';
if c=0 then //C为服务端发送的字节数,如果为0表示为尚未开始接收
begin
ll:=socket.ReceiveText;
c:=strtoint(ll); //设置需接收的字节数
clientsocket1.Socket.SendText('okok');
begin
len:=socket.ReceiveLength; //读出包长度
label1.Caption:=inttostr(c);
socket.ReceiveBuf(buffer,len); //接收数据包并读入缓冲区内
m.Write(buffer,len); //追加入流M中
if m.Size>=c then //如果流长度大于需接收的字节数,则接收完毕
begin
m.Position:=0;
try
m.SaveToFile(ss);
finally //以下为清除工作
clientsocket1.Active:=false;
clientsocket1.Active:=true;
m.Clear;
c:=0;
end;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
m:=TMemorystream.Create; //建立内存流
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
m.free;
ClientSocket1.Close;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
clientsocket1.Socket.SendText('gets'); //发送申请,通知服务端
end;
end;
此为客户端的代码 我接受很长时间也不见完成