好,看看了,谢谢:
Type
TCommDataFormat=packed record
Head:Byte;
SerNumber:Byte;
StreamLen:array[0..1] of byte;
stream:array [0..512] of char;
end;
procedure TLogin_frm.BitBtn1Click(Sender: TObject);
var
MyStream: TMemoryStream;
Str:string;
TmpStr:TCommDataFormat;
buffer:char;
m,i,j:integer;
begin
str:=Login_name.text;
for m:=1 to 10-Length(str) do
str:='0'+str;//STR补足共10个CHAR
str:=str+Login_pw.text;
m:=length(str);
with TmpStr do
begin
Head:=$10;
SerNumber:=1;
streamlen[0]:=0;
StreamLen[1]:=Length(str);
for i:=0 to m-1 do
Stream:=(str[i+1]);
end;//给第一次通讯的包写数据,以16为命令头。
WriteLog(now,'用户号码为:'+Login_name.text+'发送登陆密码校验!');
MyStream:=TMemoryStream.Create;
try
MyStream.Write(TmpStr, sizeof(TCommDataFormat));
senddata(Mystream);//为后面定义的函数。
IfSend:=True;
finally
MyStream.Free;
end;
end;
procedure SendData(content: TMemoryStream);
var
len:integer;
begin
FSockAddrIn.SIn_Addr.S_addr := inet_addr(PChar(Hostname));
FSockAddrIn.SIn_Port := htons(strtoint(port));
len := sendto(sock, content, sizeof(content), 0, FSockAddrIn, sizeof(FSockAddrIn));
if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then
begin
MessageBox(handle,'SOCKET错误,代码:','提示',mb_ok);
WriteLog(now,'SOCKET错误,代码:'+inttostr(WSAGetLastError()));
exit;
end;
if len = SOCKET_ERROR then
begin
MessageBox(handle,'数据发送失败!','提示',mb_ok);
WriteLog(now,'数据发送失败!');
exit;
end;
if len<>sizeof(content) then
begin
MessageBox(handle,'数据发送不完整!','提示',mb_ok);
end;
end;
发送后收到的数据完全不正确。