先发参数在传文件;
如果用结构如:
type
//数据类型 字符串 下载文件 上传文件 记录
TDataType = (dtString, dtGetFile, dtUpFile, dtRecord);
//发送数据记录
TSendData = packed record //记大小4096
FDataType: TDataType; // 数据类型
FSockFlage: Integer; // Server/Client Flage
FDataSize: Integer; // 数据大小
FDataAllLength: Integer; //数据总长度
FCmdStr: array[1..255] of Char;// COMMAND string
FData: array[1..1024] of Char;// 数据
end;
在接收时用结构收就行
根据数据大小和总长度确定文件是否接收完成.
procedure OnWrite(Hwnd: HWND; var S: TSocket);
var
SendBuf: TSendData;
lpText: PChar;
//Buf: array[0..1024] of Char;
begin
if S <> INVALID_SOCKET then
begin
//填充SendBuf
FillChar(SendBuf, SizeOf(SendBuf), 0);
//取得Command str
XWinSock.GetWndText(hCmdEdt, lpText);
//填充SendBuf.CmdStr
lstrcpy(SendBuf.CmdStr, lpText);
//删除lpText
FreeMem(lpText);
//服务标识
SendBuf.FSockFlage := SF_CLIENT;
//数据类型
SendBuf.FDataType := TDataType(SendMessage(hCmdList, CB_GETCURSEL, 0, 0));
if SendBuf.FDataType = dtUpFile then
begin
SendFile(S, SendBuf);
end else begin
WinSock.send(S, SendBuf, SizeOf(SendBuf), 0);
end;
procedure OnRead(Hwnd: HWND; var S: TSocket);
var
ReadBuf: TSendData;
Len: Integer;
begin
if S <> INVALID_SOCKET then
begin
FillChar(ReadBuf, SizeOf(ReadBuf), 0);
Len := WinSock.recv(S, ReadBuf, SizeOf(ReadBuf), 0);
if Len > 0 then begin
case ReadBuf.FDataType of
dtString: ReadString(S, ReadBuf);
dtUpFile:;
dtGetFile: ReadFileBuffer(S, ReadBuf);
dtRecord:;
end;
//OnWrite(Hwnd, S, ReadData);
end
else begin
ReadBuf.FDataType := dtString;
ReadBuf.FSockFlage := SF_SERVER;
ReadBuf.CmdStr := '读取数为空!';
FormatChar(StatusText, 'Server Host: [%s], ReadBuf.CmdStr: [%s]',
[Integer(GetRemoteHost(S)), Integer(@ReadBuf.CmdStr)]);
MemoAdd(hMemo, StatusText);
end;
end;