我使用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这两个单元
使用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这两个单元
解决方案 »
- 商场pos机的LED
- 如何使用installshield制作带SQL2000数据库的安装程序
- 关系动态建立FRAME时的问题
- 能用中文给我举例说明一下virtual和override的意思吗?
- 请教,读流发生错误
- 关于Rect的问题.时刻在捆饶着我.分数全散.
- 请问要如何计算利息
- 做了BMP to GIF的共享软件会不会引起GIF格式的版权纠纷?
- 牛虻呼叫老鱼,牛虻呼叫老鱼,牛虻呼叫老鱼,牛虻呼叫老鱼,牛虻呼叫老鱼,
- 从局域网通过代理上网要设置DNS,何为DNS?
- 头一次遇到这么奇怪的事,百思不得其解!
- 请教大家关于DBGridToExcel控件的使用方法??????????????????????????
先接收成byte array 然后使用 copyMemory转换
TUser=record
UserName,UserID:String;
IP:String;
Port:Integer;
Msg:String;
Arr:Array[1..9] of shortString;
flag:Boolean;
Cmd:String;
end;
这样定义是有问题的,里面的String会导致包大小计算不对
我改动一个,
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的应用协议