挣你点分,真不容易。好吧,好人作到底了。
数据库结构设计,这个俺不了解你的需求,是无能为力的,只有靠你自己了,要切记必须
能够保证每天的数据都能单独提出生成报文,这是远程报帐的关键。
1、文件流压缩函数,引用zlib.
procedure compresstext(var compressedstream:tmemorystream;const compressionlevel:integer);
var
sourcestream:tcompressionstream;deststream:tmemorystream;count:integer;
begin
count:=compressedstream.Size;
try deststream:=tmemorystream.Create;
case compressionlevel of
0:sourcestream:=tcompressionstream.Create(clnone,deststream);
1:sourcestream:=tcompressionstream.Create(clfastest,deststream);
2:sourcestream:=tcompressionstream.Create(cldefault,deststream);
3:sourcestream:=tcompressionstream.Create(clmax,deststream);
end;
compressedstream.SaveToStream(sourcestream);sourcestream.Free;
compressedstream.Clear;compressedstream.WriteBuffer(count,sizeof(count));
compressedstream.CopyFrom(deststream,0);
finally deststream.Free;end;
end;
procedure uncompresstext(compressedstream:tmemorystream;destfn:string);
var sourcestream:tdecompressionstream;deststream:tmemorystream;buffer
char;count:integer;
begin
compressedstream.ReadBuffer(count,sizeof(count));getmem(buffer,count);
try deststream:=tmemorystream.Create;
sourcestream:=tdecompressionstream.Create(compressedstream);
sourcestream.ReadBuffer(buffer^,count);
deststream.WriteBuffer(buffer^,count);deststream.Position:=0;
deststream.SaveToFile(destfn);
finally freemem(buffer);deststream.Free;sourcestream.free;end;
end;
2、转贴唐晓峰的帖子,这个SOCKET传输程序是有效的,如果还是不能理解,那么用ftp吧,
简单省事,除了容易遭受FTP攻击外,其他倒没有什么大麻烦(一般对于普通应用,不会
有人这么吃饱没事干的)。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls;
type
TCon = record
FileName : String;
TotalSize : Integer;
Status : Integer;
end;
PCON = ^TCON;
TForm1 = class(TForm)
SS: TServerSocket;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure SSClientConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure SSClientRead(Sender: TObject; Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
SS.Port := 9000;
SS.Active := True;
end;
procedure TForm1.SSClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var c : pcon;
begin
c :=new(pcon);
c.FileName := '';
c.TotalSize := 0 ;
c.Status := 0;
Socket.Data := c;
Socket.SendText('已经连接,请输入UPLOAD FILENAME SIZE'#13#10);
end;
procedure TForm1.SSClientRead(Sender: TObject; Socket: TCustomWinSocket);
var C : PCON;
cmd:String;
Buffer : pointer;
nRetr : integer;
fs : TFileStream;
const bufferSize = 1024 ;
begin
C:= Socket.Data ;
case c.Status of
0 :
begin
cmd := trim(Socket.ReceiveText) ;
if Pos('UPLOAD ',uppercase(cmd)) > 0 then
begin
c.FileName := trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
c.TotalSize := StrToInt(Copy(c.FileName,Pos(' ',c.FileName)+1,Length(c.FileName)));
c.FileName := trim(Copy(c.FileName,1,Pos(' ',c.FileName)));
c.Status := 1;
Socket.Data := C;
Socket.SendText('you can send File !'#13#10);
end;
end;
1 : begin
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);
if not FIleExists('c:/'+c.FileName) then
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmCreate or fmShareDenyNone);
fs.Seek(0,soFromBeginning);
end
else
begin
fs :=TFileStream.Create('c:/'+c.FileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;
fs.WriteBuffer(Buffer^,nRetr);
fs.Destroy;
FreeMem(Buffer);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.Show;
end;
end.
--------------------------------------------------------------------------------
来自:唐晓锋 时间:99-11-30 01:17:19 ID:162654
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;
type
TForm2 = class(TForm)
CS: TClientSocket;
OpenDialog1: TOpenDialog;
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
SendCommand: TButton;
Label1: TLabel;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure SendCommandClick(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure CSRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
function GetFileSize(const FileName: string):integer;
var f : TFileStream;
begin
f := TFileStream.Create(FileName,fmOpenRead or fmShareDenyNone);
Result :=f.Size;
F.Free;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
with OpenDialog1 do
begin
Execute;
if FileName <> '' then
begin
Edit1.Text := 'UPLOAD '+ ExtractFileName(FileName) +' '+Inttostr(GetFileSize(FileName));
Label1.Caption := FileName;
cs.Socket.SendText(edit1.Text);
end;
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
CS.Active := True;
end;
procedure TForm2.SendCommandClick(Sender: TObject);
var fs : TFileStream;
Buf : pointer;
begin
//CS.Socket.SendText(Edit1.Text+#13#10);
//Memo1.Lines.Add();
fs := TFileStream.Create(Label1.Caption ,fmOpenRead or fmShareDenyNone);
GetMem(Buf,fs.Size);
fs.Seek(0,soFromBeginning);
fs.ReadBuffer(Buf^,fs.Size);
memo1.Lines.Add('has send : '+inttostr(Cs.Socket.SendBuf(Buf^,fs.Size)));
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
cs.Close;
end;
procedure TForm2.CSRead(Sender: TObject; Socket: TCustomWinSocket);
begin
Memo1.Lines.add(socket.receiveText);
end;
end.
3、FTP和NMSTRM都是控件,使用起来十分简单(虽然有很多不如意的地方),随便找本
入门书例程多多,不用让我麻烦了吧。
4、一种简单的报文格式:给你一个文本格式吧。例如,客户名称AAA,地址BBB,代码CCC,
其他DDD,AAA订购的商品有2种,分别是1商品和2商品,各自要素分别为价格12、23,单位
箱、桶,等等。报文格式可以这样:
UNT //表示报文开始,可以放置报文类型标志。
KHM AAA //KHM 表示这是一个客户的开始,一直到KHE为止都是这个客户的资料,包括他的
商品信息
KHD BBB
KHC CCC
SMC 1 //到此表示开始记录AAA的某种商品,这是商品名称,一直到SME为止都是AAA的
1商品的资料
。。。
SME
SMC 2 //第二种AAA的商品2,从此开始。
。。。
SME
KHE
KHM //第二个客户从此开始。
。。。
KHE
HHH 789 //表示该报文生成时,所记录的商品额总计。
HHD 5 //表示所涉及到的客户数量。这两项可以在拆分时用于解决数据校验的问题,当然
可以更多。
UNE //报文结束。
其中KHM 表示报文头,用来区分该行表示的内容,以对应相应的数据结构。
生成和拆分文本可以参看帮助中的TEXTFILE。