程序代码
unit _C_S_Common;
interface
uses
Windows, Classes, SysUtils, StrUtils;
type
PNet_DATA = ^TNet_DATA;
TNet_DATA = record
msgID: integer;
msg: string;
end;
Transmit_Type = (Send, Receive, Wait);
TTransmit = record
BufSize: integer;
TestBuf: Boolean;
SocketID: integer;
end;
const
FMS_TNET_DATA = 'T_NET_DATA;%d;%s';
CI_TF_TestBuffSize = 3001;
CI_TF_TestBuffSize_OK = 3002;
CI_TF_Ready = 3003;
CI_TF_End = 3004;
CI_TF_CHK_SYS = 3005;
CI_TF_CHK_SPTZ = 3006;
CI_TF_CHK_HY = 3007;
CI_TF_DownLoad_SYS = 3008;
CI_TF_Con = 3009;
var
TF: TTransmit;
function Encode_TNet_DATA(MsgID: integer; Msg: string):string;
procedure Decode_TNet_DATA(pDATA: PNet_DATA; buf: string);
implementation
function Encode_TNet_DATA(MsgID: integer; Msg: string):string;
begin
result:= format(FMS_TNET_DATA, [MsgID, Msg]);
end;
procedure Decode_TNet_DATA(pDATA: PNet_DATA; buf: string);
var
s: string;
i: integer;
begin
if leftstr(buf, 10) <> 'T_NET_DATA' then
begin
pDATA^.msgID:= 0;
pDATA^.msg:= '';
exit;
end;
s:= rightstr(buf, length(buf) - 11);
i:= pos(';', s);
pDATA^.msgID:= strtoint(leftstr(s, i-1));
pDATA^.msg:= rightstr(s, length(s)-i);
end;
end.
////////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TFrm_S.FormCreate(Sender: TObject);
begin
SSocket.Port:= 14000;
SSocket.Open;
SSocket_TF.Port:= 14001;
SSocket_TF.Open;
end;
procedure TFrm_S.SSocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('SSocketClientConnect');
TF.SocketID:= Socket.SocketHandle;
end;
procedure TFrm_S.SSocket_TFClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('SSocket_TFClientConnect');
end;
procedure TFrm_S.SSocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('SSocketClientDisconnect');
end;
procedure TFrm_S.SSocket_TFClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('SSocket_TFClientDisconnect');
end;
procedure TFrm_S.SSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
buf: string;
aTCP_DATA: TNet_DATA;
msg: string;
begin
buf:= Socket.ReceiveText;
Decode_TNet_DATA(@aTCP_DATA, buf);
case aTCP_DATA.msgID of
CI_TF_Con:
begin
m1.Lines.Add('SSocket Receive CI_TF_Con');
TF.TestBuf:= true;
TF.BufSize:= 8192;
msg:= format('%d', [TF.BufSize]);
Socket.SendText(Encode_TNet_DATA(CI_TF_TestBuffSize, msg));
m1.Lines.Add('SSocket Send CI_TF_TestBuffSize');
end;
end;
end;
procedure TFrm_S.SSocket_TFClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
pmem: Pointer;
len: integer;
aTCP_Data: TNet_DATA;
ipos: integer;
buf, msg: string;
aSocket: TCustomWinSocket;
begin
if TF.TestBuf then
begin
len:= Socket.ReceiveLength;
getmem(pmem, len);
Socket.ReceiveBuf(pmem^, len);
freemem(pmem);
m1.Lines.Add('SSocket_TF ReceiveLength' + inttostr(len));
aSocket:=TCustomWinSocket.Create(TF.SocketID);
if len = TF.BufSize then
begin
m1.Lines.Add('TEST bufsize OK ' + inttostr(TF.BufSize));
TF.TestBuf:= false;
aSocket.SendText(Encode_TNet_DATA(CI_TF_TestBuffSize_OK, ''));
end else
begin
case TF.BufSize of
8192:
begin
TF.BufSize:= 4096;
end;
4096:
begin
TF.BufSize:= 2048;
end;
2048:
begin
TF.BufSize:= 1024;
end;
end;
msg:= format('%d', [TF.BufSize]);
aSocket.SendText(Encode_TNet_DATA(CI_TF_TestBuffSize, msg));
end;
end;
end;
///////////////////////////////////////////////////////////////////////////////////////////////////
procedure TFrm_C.FormCreate(Sender: TObject);
begin
CSocket.Address:= '192.168.0.10';
CSocket.Port:= 14000;
CSocket.Open;
CSocket_TF.Address:= '192.168.0.10';
CSocket_TF.Port:= 14001;
end;
procedure TFrm_C.CSocketConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
m1.Lines.Add('CSocketConnect');
end;
procedure TFrm_C.CSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('CSocketDisconnect');
end;
procedure TFrm_C.CSocket_TFConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('CSocket_TFConnect');
CSocket.socket.SendText(Encode_TNet_DATA(CI_TF_Con, ''));
m1.Lines.Add('CSocket_TF Send CI_TF_CON');
end;
procedure TFrm_C.CSocket_TFDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
m1.Lines.Add('CSocket_TFDisconnect');
end;
procedure TFrm_C.btn_TF_OPENClick(Sender: TObject);
begin
if CSocket_TF.Socket.Connected then CSocket_TF.Close;
CSocket_TF.Open;
end;
procedure TFrm_C.CSocketRead(Sender: TObject; Socket: TCustomWinSocket);
var
buf: string;
aTCP_DATA: TNet_DATA;
msg: string;
pmem: Pointer;
begin
buf:= Socket.ReceiveText;
Decode_TNet_DATA(@aTCP_DATA, buf);
case aTCP_DATA.msgID of
CI_TF_TestBuffSize:
begin
m1.Lines.Add('CSocket Receive CI_TF_TestBuffSize');
m1.Lines.Add(' ' + buf);
TF.BufSize:= strtoint(aTCP_DATA.msg);
getmem(pmem, TF.BufSize);
ZeroMemory(pmem, TF.BufSize);
CSocket_TF.Socket.SendBuf(pmem^, TF.BufSize);
freemem(pmem);
m1.Lines.Add('CSocket_TF Send TestBuf ' + inttostr(TF.BufSize));
end;
CI_TF_TestBuffSize_OK:
begin
CSocket_TF.Close;
end;
end;
end;
/////////////////////////////////////////////////////////////////////////////////////////