N
norton
Unregistered / Unconfirmed
GUEST, unregistred user!
我要在局域网内,传输文件(可以看作二进制文件,比如:数据库的表文件),现用以下程序段实现及其出现的问题:
1。用nmstrm,和nmstrmserv 及 nmuuprocessor mime编码实现。
//发送端:
procedure TForm1.Button1Click(Sender: TObject);
var
input:TFileStream;
output:TFileStream;
send:TFilestream;
temp:string;
begin
opendialog1.Execute;
input:=TFileStream.Create(opendialog1.FileName,fmOpenRead);
output:=TFileStream.Create('c:/temp/send.tmp',fmCreate);
nmuuprocessor1.InputStream:=input;
nmuuprocessor1.OutputStream:=output;
nmuuprocessor1.Encode;
output.Seek(sofrombeginning,0);
input.Free;
output.Free;
send:=TFileStream.Create('c:/temp/send.tmp',fmOpenRead);
temp:=nmstrm1.PostIt(send);
send.Free;
showmessage(temp);
end;
//接收端:
procedure TForm1.NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
strm: TStream);
var
input:TFileStream;
output:TFileStream;
receive:TFileStream;
begin
receive:=TFileStream.Create('c:/temp/receive.tmp',fmCreate);
receive.CopyFrom(strm,strm.size);
receive.Free;
input:=TFileStream.Create('c:/temp/receive.tmp',fmopenread);
savedialog1.Execute;
output:=TFilestream.Create(savedialog1.FileName,fmCreate);
nmuuprocessor1.InputStream:=input;
nmuuprocessor1.OutputStream:=output;
nmuuprocessor1.Decode;
input.Free;
output.Free;
end;
出现的问题:我先发送一WORD文件(700K左右,用uuprocessor 编码后,有
1M左右,但发送没有问题,但经不起多次发送。发送一可执行文件(400K),
经uuprocessor编码后,有600K左右,点击发送,没有反应,postit也不返回
‘ok’。
2.用tserversocket 和 tclientsocket实现。
//两者在同一程序段内》
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.SimplePanel:=True;
statusbar1.SimpleText:='connected!';
end;
//监听按钮
procedure TForm1.Button1Click(Sender: TObject);
begin
serversocket1.Active:=True;
end;
//发送按钮
procedure TForm1.sendClick(Sender: TObject);
var
send:TFilestream;
begin
opendialog1.Execute;
send:=TFileStream.Create(opendialog1.filename,fmopenread);
serversocket1.Socket.Connections[0].sendstream(send);
serversocket1.socket.Connections[0].sendtext('ok');
send.free;
end;
//客户段接收
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
len:integer;
msg:string;
b:array [1..10000] of byte;
begin
with clientsocket1 do
begin
len:=Socket.ReceiveLength;
if len=2 then
begin
msg:=socket.ReceiveText;
if comparestr(msg,'ok')=0 then
begin
receive.Seek(sofrombeginning,0);
receive.Free;
showmessage('ok');
end;
end
else
begin
socket.ReceiveBuf(b,len);
receive.Write(b,len);
end;
end;
end;
//客户段连接按钮
procedure TForm1.Button2Click(Sender: TObject);
begin
clientsocket1.Open;
receive:=TFileStream.Create('c:/temp/receive',fmcreate);
end;
当发送先前WORD文档时,多次激活clientsocketread事件,看来是要进行
数据拼接 ,但经过两次后,出现‘access violation at adress xxxx'
错误。
3。用nmudp广播,本地地址:192.100.100.1,广播地址:192.100.100.255
但从某本书上得知:nmudp每个数据包只能为2k,要多次拼接。只好做罢。
有那位大虾,有兴趣可补上,我也写了,但也调不通,出现问题与2程序段
出现问题差不多。
以上是,问题,程序段中某些考虑不周全的地方,请见凉,因为我项先实现
功能。请诸位大虾,指点一二。送上150分,因为I'm poor!我是非常急!
因为我不能用ftp服务器,所以nmftp作罢。
1。用nmstrm,和nmstrmserv 及 nmuuprocessor mime编码实现。
//发送端:
procedure TForm1.Button1Click(Sender: TObject);
var
input:TFileStream;
output:TFileStream;
send:TFilestream;
temp:string;
begin
opendialog1.Execute;
input:=TFileStream.Create(opendialog1.FileName,fmOpenRead);
output:=TFileStream.Create('c:/temp/send.tmp',fmCreate);
nmuuprocessor1.InputStream:=input;
nmuuprocessor1.OutputStream:=output;
nmuuprocessor1.Encode;
output.Seek(sofrombeginning,0);
input.Free;
output.Free;
send:=TFileStream.Create('c:/temp/send.tmp',fmOpenRead);
temp:=nmstrm1.PostIt(send);
send.Free;
showmessage(temp);
end;
//接收端:
procedure TForm1.NMStrmServ1MSG(Sender: TComponent; const sFrom: String;
strm: TStream);
var
input:TFileStream;
output:TFileStream;
receive:TFileStream;
begin
receive:=TFileStream.Create('c:/temp/receive.tmp',fmCreate);
receive.CopyFrom(strm,strm.size);
receive.Free;
input:=TFileStream.Create('c:/temp/receive.tmp',fmopenread);
savedialog1.Execute;
output:=TFilestream.Create(savedialog1.FileName,fmCreate);
nmuuprocessor1.InputStream:=input;
nmuuprocessor1.OutputStream:=output;
nmuuprocessor1.Decode;
input.Free;
output.Free;
end;
出现的问题:我先发送一WORD文件(700K左右,用uuprocessor 编码后,有
1M左右,但发送没有问题,但经不起多次发送。发送一可执行文件(400K),
经uuprocessor编码后,有600K左右,点击发送,没有反应,postit也不返回
‘ok’。
2.用tserversocket 和 tclientsocket实现。
//两者在同一程序段内》
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
statusbar1.SimplePanel:=True;
statusbar1.SimpleText:='connected!';
end;
//监听按钮
procedure TForm1.Button1Click(Sender: TObject);
begin
serversocket1.Active:=True;
end;
//发送按钮
procedure TForm1.sendClick(Sender: TObject);
var
send:TFilestream;
begin
opendialog1.Execute;
send:=TFileStream.Create(opendialog1.filename,fmopenread);
serversocket1.Socket.Connections[0].sendstream(send);
serversocket1.socket.Connections[0].sendtext('ok');
send.free;
end;
//客户段接收
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
len:integer;
msg:string;
b:array [1..10000] of byte;
begin
with clientsocket1 do
begin
len:=Socket.ReceiveLength;
if len=2 then
begin
msg:=socket.ReceiveText;
if comparestr(msg,'ok')=0 then
begin
receive.Seek(sofrombeginning,0);
receive.Free;
showmessage('ok');
end;
end
else
begin
socket.ReceiveBuf(b,len);
receive.Write(b,len);
end;
end;
end;
//客户段连接按钮
procedure TForm1.Button2Click(Sender: TObject);
begin
clientsocket1.Open;
receive:=TFileStream.Create('c:/temp/receive',fmcreate);
end;
当发送先前WORD文档时,多次激活clientsocketread事件,看来是要进行
数据拼接 ,但经过两次后,出现‘access violation at adress xxxx'
错误。
3。用nmudp广播,本地地址:192.100.100.1,广播地址:192.100.100.255
但从某本书上得知:nmudp每个数据包只能为2k,要多次拼接。只好做罢。
有那位大虾,有兴趣可补上,我也写了,但也调不通,出现问题与2程序段
出现问题差不多。
以上是,问题,程序段中某些考虑不周全的地方,请见凉,因为我项先实现
功能。请诸位大虾,指点一二。送上150分,因为I'm poor!我是非常急!
因为我不能用ftp服务器,所以nmftp作罢。