我刚写了一个文件收发器,现公开重要的部分。。。
client
Var
fsSend: TFileStream;
fmOpen: Word;
dataend: boolean;
Const
MP_QUERY ='1';
MP_REFUSE ='2';
MP_ACCEPT ='3';
MP_NEXTWILLBEDATA='4';
MP_DATA ='5';
MP_ABORT ='6';
MP_OVER ='7';
MP_CHAT ='8';
MP_FILEPROPERTY ='9';
MP_END ='0';
iBYTEPERSEND = 256;
procedure TNetSendData_Form.btnConnectClick(Sender: TObject);
begin
inherited;
Try
cs.Address:=edtIPAddress.Text;
cs.Port:=10001;
cs.Active := True;
Except
Self.StatusBar1.Panels[1].Text := '连接税务局主机不成功!';
MessageDlg('服务器忙,请稍后再试!', mtInformation,
[mbOk], 0);
end;
end;
procedure TNetSendData_Form.btnSendFileClick(Sender: TObject);
begin
inherited;
if OpenDialog.Execute then
begin
Memo1.Lines.Add(OpenDialog.FileName);
end;
end;
procedure TNetSendData_Form.BitBtn2Click(Sender: TObject);
begin
inherited;
cs.Socket.SendText(MP_QUERY+GetFileName(Memo1.Lines.Strings[0]));//FileSize???
// ShowMessage(GetFileName(Memo1.Lines.Strings[0]));
fsSend := TFileStream.Create(Memo1.Lines.Strings[0],fmOpen);
Gauge1.MaxValue := Trunc(fsSend.
Size/iBYTEPERSEND)+1;
Memo1.Lines.Add('文件大小为:'+IntToStr(Trunc(fsSend.
Size/iByTEPERSEND)+1)+'字节');
StatusBar1.Panels[1].Text := '数据传输中,请稍候......';
dataend := False;
end;
procedure TNetSendData_Form.CSRead(Sender: TObject;
Socket: TCustomWinSocket);
var
sTemp,sRecv:string;
bufSend
ointer;
MyPosition: Integer;
begin
inherited;
sRecv:=Socket.ReceiveText;
//------------------------------------
Case sRecv[1] of
MP_REFUSE:ShowMessage('Faint,be refused!');
//----------------------------------
MP_ACCEPT:begin
//iBYTEPERSEND是个常量,每次发送包的大小。
Socket.SendText(MP_FILEPROPERTY+IntToStr(Trunc(fsSend.
Size/iBYTEPERSEND)+1));
end;
//---------------------------------------
MP_NEXTWILLBEDATA:begin
If Not DataEnd then
Socket.SendText(MP_NEXTWILLBEDATA)
else
Socket.SendText(Mp_END);
end;
//-------------------------------------------
MP_DATA:begin
try
GetMem(bufSend,iBYTEPERSEND+1);
if (fsSend.
Position+1+iBYTEPERSEND) < fsSend.
Size then
begin
fsSend.
ReadBuffer(bufSend^,iBYTEPERSEND);
Socket.SendBuf(bufSend^,iBYTEPERSEND);
end//普通的发送,大小为iBYTEPERSEND
else
begin
MyPosition := fsSend.
Size-fsSend.
Position-1;
fsSend.
Read(bufSend^,fsSend.
Size-fsSend.
Position-1);
Socket.SendBuf(bufSend^,MyPosition);
Gauge1.Progress := Gauge1.Progress + 1;
fsSend.
Free;
dataend := True;
StatusBar1.Panels[1].Text := '数据传输结束!。。。';
end;
//最后一次发送,发送剩余的数据
finally
FreeMem(bufSend,iBYTEPERSEND+1);
If Gauge1.Progress < Gauge1.MaxValue then
Gauge1.Progress := Gauge1.Progress + 1
end{of try}
end;
//---------------------------------------------------
MP_ABORT:begin
//被取消了
fsSend.
Free;
end;
end;
end;
procedure TNetSendData_Form.FormCreate(Sender: TObject);
begin
inherited;
Memo1.Lines.Text := '';
edtIPAddress.Text := '';
With IpAdoTabledo
begin
Connection := Main_DataModule.Main_ADOConnection;
TableName := 'Ip';
If Not Active then
Active := True;
While Not Eofdo
begin
EdtIpAddress.Items.Add(FieldByName('ip').AsString);
Next;
end;
end;
end;
procedure TNetSendData_Form.BitBtn1Click(Sender: TObject);
begin
inherited;
Close;
end;
procedure TNetSendData_Form.SendDataLarge;
begin
end;
procedure TNetSendData_Form.CSLookup(Sender: TObject;
Socket: TCustomWinSocket);
begin
inherited;
Self.StatusBar1.Panels[1].Text := '正在试图与税务局服务器建立连接,请稍候.....!';
end;
procedure TNetSendData_Form.CSConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
inherited;
StatusBar1.Panels[1].Text := '连接税务局主机成功,准备发送数据!';
BtnConnect.Enabled := False;
end;
procedure TNetSendData_Form.CSDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
inherited;
StatusBar1.Panels[1].Text := '与服务器断开连接。。。!';
BtnConnect.Enabled := True;
end;
procedure TNetSendData_Form.CSError(Sender: TObject;
Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
inherited;
If ErrorEvent = eeConnect then
begin
StatusBar1.Panels[1].Text := '服务器忙,重新进行连接。。。!';
Cs.Active := False;
end;
if ErrorEvent = eeAccept then
begin
StatusBar1.Panels[1].Text := '1服务器忙,重新进行连接。。。!';
end;
If ErrorEvent = eeGeneral then
begin
StatusBar1.Panels[1].Text := '2服务器忙,重新进行连接。。。!';
end;
end;
function TNetSendData_Form.GetFileName(FileName: String): String;
Var
i: Integer;
begin
For i := Length(FileName)do
wnto 0do
begin
If FileName
= '/' then
begin
Result := Copy(FileName,i+1,Length(FileName));
Break;
end;
end;
end;
end.