H
hglhujing
Unregistered / Unconfirmed
GUEST, unregistred user!
服务器端serversocket是这样写的: qing和ting是2个packed record,内容qing都是integer型,ting里面有一个stri是16位字符型,其他是integer型:
function TForm1.MakeMsg(var mh; var mb; size1, size2: integer; var buffer:
Pointer):
integer;
begin
buffer := allocmem(Cardinal(size1 + size2));
CopyMemory(buffer, @mh, size1);
CopyMemory(Pbyte(integer(buffer) + size1), @mb, size2);
result := size1 + size2;
end;
procedure TForm1.SendMs(stri: string);
var
mh: qing;
yaosongbuf: pointer;
th: ting;
sendsize: integer;
begin
ZeroMemory(@th, sizeof(th));
ZeroMemory(@mh, sizeof(mh));
mh.s := 0;
mh.ss := 1;
mh.p := 2;
mh.t := 3;
th.q := 4;
th.v := 5;
StrPLCopy(@th.stri, stri, length(stri));
sendsize := makemsg(mh, th, sizeof(mh), sizeof(th), yaosongbuf);
ServerSocket1.Socket.Connections[0].SendBuf(yaosongbuf^, sendsize);
FreeMemory(yaosongbuf);
end;
clientsocket的主要部分如下:记录对应上边的设定
procedure TForm1.disconnect1Click(Sender: TObject);
begin
ClientSocket1.Active := false;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
mh: qing;
recsize: integer;
th: ting;
begin
while (true) do
begin
recsize := Socket.ReceiveBuf(mh, SIZEof(mh));
if (recsize < SizeOf(mh)) then
begin
exit;
end
else
begin
Memo1.Lines.Append('收包成功!!');
Memo1.Lines.Append(th.stri);
Edit1.Text := IntToStr(recsize);
end;
end;
end;
希望有高手指教!!
function TForm1.MakeMsg(var mh; var mb; size1, size2: integer; var buffer:
Pointer):
integer;
begin
buffer := allocmem(Cardinal(size1 + size2));
CopyMemory(buffer, @mh, size1);
CopyMemory(Pbyte(integer(buffer) + size1), @mb, size2);
result := size1 + size2;
end;
procedure TForm1.SendMs(stri: string);
var
mh: qing;
yaosongbuf: pointer;
th: ting;
sendsize: integer;
begin
ZeroMemory(@th, sizeof(th));
ZeroMemory(@mh, sizeof(mh));
mh.s := 0;
mh.ss := 1;
mh.p := 2;
mh.t := 3;
th.q := 4;
th.v := 5;
StrPLCopy(@th.stri, stri, length(stri));
sendsize := makemsg(mh, th, sizeof(mh), sizeof(th), yaosongbuf);
ServerSocket1.Socket.Connections[0].SendBuf(yaosongbuf^, sendsize);
FreeMemory(yaosongbuf);
end;
clientsocket的主要部分如下:记录对应上边的设定
procedure TForm1.disconnect1Click(Sender: TObject);
begin
ClientSocket1.Active := false;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
var
mh: qing;
recsize: integer;
th: ting;
begin
while (true) do
begin
recsize := Socket.ReceiveBuf(mh, SIZEof(mh));
if (recsize < SizeOf(mh)) then
begin
exit;
end
else
begin
Memo1.Lines.Append('收包成功!!');
Memo1.Lines.Append(th.stri);
Edit1.Text := IntToStr(recsize);
end;
end;
end;
希望有高手指教!!