Indy10发送与接收问题(记录)
2008-10-28 09:54
295 查看
Indy10发送与接收问题(记录) Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiNetwork/html/delphi_20061214163900127.html
我使用TIDTcpClient及TIdTcpServer,
使用ReadLn及WriteLn正常.
但是我想发送记录类型,如何做呢?我的实现如下,但没有成功:
------------------------------------------------------------
type
TUser=record
UserName,UserID:String;
IP:String;
Port:Integer;
Msg:String;
Arr:Array[1..9] of shortString;
flag:Boolean;
Cmd:String;
end;
------------------------------------------------------------
实现如下:
------------------------------------------------------------
1.使用TidBytes类型:
客户:
procedure TForm1.Button3Click(Sender: TObject);
var
User:TUser;
sby:TidBytes;
begin
with user do
begin
UserName:='Wyatt';
UserID:='7895421';
Ip:='192.168.1.188';
Port:=9999;
Msg:=Edit3.Text;
cmd:='Quit';
end;
sBy:=RawTOBytes(user,sizeof(user));
ic1.IOHandler.Write(sBy);
end;
服务器:
procedure TFrmServer.is1Execute(AContext: TIdContext);
var
user:TUser;
buf:TidBytes;
begin
Acontext.Connection.IOHandler.ReadBytes(buf,sizeof(user));
BytesToRaw(buf,user,sizeof(user));
with user,memo1.Lines do
begin
Add(userName);
add(userID);
add(ip);
add(inttostr(port));
end;
end;
----------------------------------------------------------------
2.使用TStream方法:
客户:
procedure TForm1.Button3Click(Sender: TObject);
var
User:TUser;
Mon:TMemoryStream;
begin
with user do
begin
UserName:='Wyatt';
UserID:='85171659';
Ip:='192.168.1.188';
Port:=9999;
Msg:=Edit3.Text;
cmd:='Quit';
end;
Mon:=TMemoryStream.Create;
try
Mon.WriteBuffer(user,sizeof(user));
ic1.IOHandler.Write(Mon);
finally
Mon.Free;
end;
end;
服务器:
procedure TFrmServer.is1Execute(AContext: TIdContext);
var
user:TUser;
Mon:TMemoryStream;
begin
Mon:=TMemoryStream.Create;
try
AContext.Connection.IOHandler.ReadStream(Mon);
Mon.ReadBuffer(user,Sizeof(user));
with user,memo1.Lines do
begin
Add(userName);
add(userID);
add(ip);
add(inttostr(port));
end;
finally
Mon.Free;
end;
end;
以上都不能实现,请高手帮忙解决一下.
以上是在delphi2006
注意一定要Indy10,因为和Indy9不同
其中要注意Use IdContext,IdGlobal这两个单元
一个想法
先接收成byte array 然后使用 copyMemory转换
type
TUser=record
UserName,UserID:String;
IP:String;
Port:Integer;
Msg:String;
Arr:Array[1..9] of shortString;
flag:Boolean;
Cmd:String;
end;
这样定义是有问题的,里面的String会导致包大小计算不对
楼上分析准确,就是这个原因
在Record内不要使用String类型,就算要用,也要用String[254]这种或Array of Char
呵,谢谢
我改动一个,
type
TUser=record
UserName,UserID:String[20];
IP:String[16];
Port:Integer;
Msg:String[100];
Arr:Array[1..9] of String[20];
flag:Boolean;
Cmd:String[20];
end;
------------------------
使用RawtoBytes及BytesToRaw可以。
为何使用Stream收不到呢?
另,如果要传送像文件类似的东西,如何定义呢?
自己定义应用协议,数据格式,传输规范
例如FTP,HTTP等都是TCP的应用协议
再求,为何使用Stream收不到呢?
http://www.delphi2007.net/DelphiNetwork/html/delphi_20061214163900127.html
我使用TIDTcpClient及TIdTcpServer,
使用ReadLn及WriteLn正常.
但是我想发送记录类型,如何做呢?我的实现如下,但没有成功:
------------------------------------------------------------
type
TUser=record
UserName,UserID:String;
IP:String;
Port:Integer;
Msg:String;
Arr:Array[1..9] of shortString;
flag:Boolean;
Cmd:String;
end;
------------------------------------------------------------
实现如下:
------------------------------------------------------------
1.使用TidBytes类型:
客户:
procedure TForm1.Button3Click(Sender: TObject);
var
User:TUser;
sby:TidBytes;
begin
with user do
begin
UserName:='Wyatt';
UserID:='7895421';
Ip:='192.168.1.188';
Port:=9999;
Msg:=Edit3.Text;
cmd:='Quit';
end;
sBy:=RawTOBytes(user,sizeof(user));
ic1.IOHandler.Write(sBy);
end;
服务器:
procedure TFrmServer.is1Execute(AContext: TIdContext);
var
user:TUser;
buf:TidBytes;
begin
Acontext.Connection.IOHandler.ReadBytes(buf,sizeof(user));
BytesToRaw(buf,user,sizeof(user));
with user,memo1.Lines do
begin
Add(userName);
add(userID);
add(ip);
add(inttostr(port));
end;
end;
----------------------------------------------------------------
2.使用TStream方法:
客户:
procedure TForm1.Button3Click(Sender: TObject);
var
User:TUser;
Mon:TMemoryStream;
begin
with user do
begin
UserName:='Wyatt';
UserID:='85171659';
Ip:='192.168.1.188';
Port:=9999;
Msg:=Edit3.Text;
cmd:='Quit';
end;
Mon:=TMemoryStream.Create;
try
Mon.WriteBuffer(user,sizeof(user));
ic1.IOHandler.Write(Mon);
finally
Mon.Free;
end;
end;
服务器:
procedure TFrmServer.is1Execute(AContext: TIdContext);
var
user:TUser;
Mon:TMemoryStream;
begin
Mon:=TMemoryStream.Create;
try
AContext.Connection.IOHandler.ReadStream(Mon);
Mon.ReadBuffer(user,Sizeof(user));
with user,memo1.Lines do
begin
Add(userName);
add(userID);
add(ip);
add(inttostr(port));
end;
finally
Mon.Free;
end;
end;
以上都不能实现,请高手帮忙解决一下.
以上是在delphi2006
注意一定要Indy10,因为和Indy9不同
其中要注意Use IdContext,IdGlobal这两个单元
一个想法
先接收成byte array 然后使用 copyMemory转换
type
TUser=record
UserName,UserID:String;
IP:String;
Port:Integer;
Msg:String;
Arr:Array[1..9] of shortString;
flag:Boolean;
Cmd:String;
end;
这样定义是有问题的,里面的String会导致包大小计算不对
楼上分析准确,就是这个原因
在Record内不要使用String类型,就算要用,也要用String[254]这种或Array of Char
呵,谢谢
我改动一个,
type
TUser=record
UserName,UserID:String[20];
IP:String[16];
Port:Integer;
Msg:String[100];
Arr:Array[1..9] of String[20];
flag:Boolean;
Cmd:String[20];
end;
------------------------
使用RawtoBytes及BytesToRaw可以。
为何使用Stream收不到呢?
另,如果要传送像文件类似的东西,如何定义呢?
自己定义应用协议,数据格式,传输规范
例如FTP,HTTP等都是TCP的应用协议
再求,为何使用Stream收不到呢?
相关文章推荐
- indy10中idtcpclient的使用问题[和大华电子称数据交换]
- odoo10企业版邮件发送问题
- 关于linux下的udp/tcp通信设置发送sendto/接收recvfrom信息超时的参数。解决通道堵塞问题。
- 关于百度云、极光的推送的问题,为什么控制台发送消息成功,而手机端接收不到消息
- android平台通过socket向C++服务器端发送数据和接收数据的相关问题
- Jmail的一个bug - 接收邮件时邮件的发送时间不对问题
- Oracle 10g在 WIN 10上 安装过程中遇到的问题记录
- CGAL window 10安装、Demo使用步骤以及问题解决记录
- yii2.0发送邮件发不出去问题记录
- 记录关于android广播(无序)接收不到的问题解决思路
- msmq在Internet上访问的问题(只能发送,不能接收)
- Java发起GET/POST请求,接收或发送URL中的数据的问题
- android安全问题(七) 抢先接收广播 - 内因篇之广播发送流程
- INDY发送html邮件带附件邮件内容不能正确显示的问题
- 利用MSComm控件实现字符发送与接收(串口)的工程设置问题
- 再论Tcp Udp 发送、接收 0字节问题
- Python中使用socket发送HTTP请求数据接收不完整问题解决方法
- 使用Outlook发送邮件至移动设备导致移动设备接收winmail.dat问题解决方法
- indy10发送网页
- PHP后端接收不到AngularJs中$http.post发送的数据的问题