你哪里还不明白啊?!把程序都做给你算了。
客户端:
//---------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
Dialogs, StdCtrls, ScktComp, WinSock;
type
TForm1 = class(TForm)
Button1: TButton;
ClientSocket1: TClientSocket;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
studPACKETHEAD = packed record
packettype: LongWord;
service_type: Byte;
length: LongWord;
end;
SMPACKETCONTENT = packed record
//原来的C语言结构定义看起来是用于存字符串的,所以改成下面的定义
dest_address: array [0..20] of Char;
source_address: array [0..20] of Char;
stud_type: LongWord;
test_time: array [0..16] of Char;
valid_time: array [0..16] of Char;
sm_length: Byte;
studmessage: array [0..179] of Char;
cw_type: array [0..1] of Char;
cw_code: array [0..5] of Char;
end;
MyPacket = packed record
Head: studPACKETHEAD;
Content: SMPACKETCONTENT;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var ST: MyPacket;
begin
ZeroMemory(@ST, SizeOf(ST));
ST.Head.packettype := 12345;
ST.Head.service_type := 251;
ST.Head.length := SizeOf(ST.Head);
StrCopy(ST.Content.dest_address, PChar(ClientSocket1.Address));
ST.Content.source_address := '127.0.0.1';
ST.Content.stud_type := $FFFFFFFF;
StrCopy(ST.Content.test_time, PChar(DateTimeToStr(Now)));
ClientSocket1.Open;
send(ClientSocket1.Socket.SocketHandle, ST, SizeOf(ST), 0);
ClientSocket1.Close;
end;
end.
服务器端:
//---------------------------------------------------------------------------
//注:需要定义与客户端一样的结构
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var ST: MyPacket;
begin
ZeroMemory(@ST, SizeOf(ST));
Socket.ReceiveBuf(ST, SizeOf(ST));
Memo1.Clear;
Memo1.Lines.Add(IntToStr(ST.Head.packettype));
Memo1.Lines.Add(IntToStr(ST.Head.service_type));
Memo1.Lines.Add(IntToStr(ST.Head.length));
Memo1.Lines.Add(ST.Content.dest_address);
Memo1.Lines.Add(ST.Content.source_address);
Memo1.Lines.Add(IntToStr(ST.Content.stud_type));
Memo1.Lines.Add(ST.Content.test_time);
end;