有关网络接收与发送数据的问题?!接收到的数据与发送端不同?!

baddy 2001-12-04 06:41:43
var
login_type:byte;
login_name,login_password:shortstring;
str:string;
mstream:tmemorystream;
R,i:longword;
ch_byte:array [1..100] of byte;
temp_byte:array [1..4] of byte absolute R;
begin
mstream:=tmemorystream.create;
fillchar(ch_byte,sizeof(ch_byte),0);
fillchar(temp_byte,sizeof(temp_byte),0);
try
R:=htonl(61);//longword type
for i:=1 to 4 do
ch_byte[i]:=temp_byte[i];
R:=htonl(1);
for i:=1 to 4 do
ch_byte[i+4]:=temp_byte[i];
R:=htonl(source_no);
for i:=1 to 4 do
ch_byte[i+8]:=temp_byte[i];
str:=FormatDateTime('mmddhhnnss',now());
str_time:=str;
R:=htonl(strtoint(str));
for i:=1 to 4 do
ch_byte[i+12]:=temp_byte[i];
command_no:=command_no+1;
R:=htonl(command_no);
for i:=1 to 4 do
ch_byte[i+16]:=temp_byte[i];//完成消息头!
login_type:=1;
R:=htonl(login_type);
ch_byte[21]:=temp_byte[4];
mstream.write(ch_byte,4);

login_name:=chatform.edit2.text;//strtemp:=#0+#0+#0+#0+#0+#0+#0+#0+#0+#0+#0+#0+#0;
login_password:=chatform.edit3.text;
if length(login_name)<16 then
login_name:=login_name+copy(strtemp,1,16-length(login_name));
if length(login_password)<16 then
login_password:=login_password+copy(strtemp,1,16-length(login_password));
str1:=login_name+login_password+reserve;
mstream.write(str1[1],40); //这样处理能不能达到使字符串与前面的整数消息头连接,写入服务器端???
chatform.ClientSocket.Socket.sendbuf(mstream,61);
finally
mstream.Free;
end;

//服务器端的接收是这样处理的,可是接收到的数据与客户端不一致有很多问题???

socket.receivebuf(ch_byte,61);
for i:=1 to 4 do
temp_byte[i]:=ch_byte[i];
mes_len:=ntohl(R);// 与客户端的数据不符?!
for i:=1 to 4 do
temp_byte[i]:=ch_byte[i+4];
source_no:=ntohl(R);// 与客户端的数据不符?!
另接收到的字符串如果分离好些?!
请大家出出主意?
谢谢!
...全文
106 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
baddy 2001-12-05
  • 打赏
  • 举报
回复
不会吧,一点机会也不给啊?!
baddy 2001-12-05
  • 打赏
  • 举报
回复
大家出出主意啊?!
baddy 2001-12-04
  • 打赏
  • 举报
回复
to zswang:
你好!
我在这方面比较差劲:因为网关已经做死了,消息关是20个字节的整数,用文本处理,可以吗?
另:能给一段示列源码吗,因任务时间较紧!
谢谢!
王集鹄 2001-12-04
  • 打赏
  • 举报
回复
//建议用文本参数
发vStringList.Text
vStringList.Values['变量名1'] := '值1';
vStringList.Values['变量名2'] := '值2';
vStringList.Values['变量名3'] := '值3';

收vStringList.Text
'值1' := vStringList.Values['变量名1'];
'值2' := vStringList.Values['变量名2'];
'值3' := vStringList.Values['变量名3'];

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧