如何用clientsocket 传送一个记录类型的变量 的值给serversocket!(50分)

  • 主题发起人 主题发起人 鱼片干子
  • 开始时间 开始时间

鱼片干子

Unregistered / Unconfirmed
GUEST, unregistred user!
比如

type
arecord=record
a:integer;
b:string;
.
.
end;
myrecord:arecord;
怎么把这种数据类型传给服务器?是不是用 stream 来传?
 
可以把变量的地址和长度取出来,用SendBuf
 
ClientSocket.SendBuf(@MyRecord,sizeof(MyRecord));
 
好像不行啊!serversocket接受记录出错!不知道具体该怎么做,请高手指点!
 
ClientSocket.SendBuf(pointer(myrecord)^,sizeof(myRecord))
ServerSocket.Socket.ReceiveBuf(myrecord,sizeof(myrecord));
 
不知道大侠们是不是调试没有问题了,我怎么就是不能传送呢?
再请教!!!!!!!!!!!
 
注意serversocket的接收时候,最好先发一个标志,再发数据
 
鱼片干子:如果还想接着讨论请定期提前自己的帖子,如果不想继续讨论请结束帖子。
 
这是我自己编的一个试验程序,用sendbuf 传一个记录ChessPoint ,
If IsServer then
serversocket.Socket.Connections[0].SendBuf(ChessPoint,sizeof(chesspoint))
else
clientsocket.Socket.SendBuf(ChessPoint,sizeof(chesspoint));
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
在win98+delphi5 下通过。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ScktComp;

type
TInfo = record
x : integer ;
y : integer ;
clr : integer ;
end;

TForm1 = class(TForm)
clientsocket: TClientSocket;
serversocket: TServerSocket;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Edit3: TEdit;
Edit4: TEdit;
Button3: TButton;
Label1: TLabel;
Edit5: TEdit;
Edit6: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure clientsocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure serversocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure serversocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure serversocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure clientsocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure clientsocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure serversocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
ChessPoint : TInfo ;
IsServer : Boolean;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
IsServer := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket.Active := False;
serversocket.Active := True;
end;

procedure TForm1.Button2Click(Sender: TObject);
var server : string ;
begin
If serversocket.Active then serversocket.Active := False;
if clientsocket.active then clientsocket.active := false;
If InputQuery('Connect To','connect to',server) then
with clientsocket do
begin
host := server ;
active := True;
end;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
chesspoint.x := strtoint(edit1.text);
chesspoint.y := strtoint(edit2.text);
chesspoint.clr := strtoint(edit3.text);
If IsServer then
serversocket.Socket.Connections[0].SendBuf(ChessPoint,sizeof(chesspoint))
else
clientsocket.Socket.SendBuf(ChessPoint,sizeof(chesspoint));



end;

procedure TForm1.clientsocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption := 'success to connect to server ';
end;

procedure TForm1.serversocketAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
IsServer := True;
end;

procedure TForm1.serversocketClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption :='success to connect to client ';
end;

procedure TForm1.serversocketClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption := 'disconnect to client';
end;

procedure TForm1.clientsocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
label1.Caption :='disconnect to server ';
end;

procedure TForm1.clientsocketRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
clientsocket.Socket.ReceiveBuf(chesspoint,12);
edit4.Text := inttostr( chesspoint.x );
edit5.text := inttostr ( chesspoint.y);
edit6.text := inttostr ( chesspoint.clr);
end;

procedure TForm1.serversocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
serversocket.Socket.Connections[0].ReceiveBuf(chesspoint,12);
edit4.Text := inttostr( chesspoint.x );
edit5.text := inttostr ( chesspoint.y);
edit6.text := inttostr ( chesspoint.clr);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
serversocket.Close ;
clientsocket.Close ;
end;

end.
 
别找麻烦了,换个思路。
自己定个协议,规定好,把纪录中的每一项都转化为string
比如说每一项中间用——号隔开,如 123-aa-vv单独做成一个函数。
然后在接受端用相应的函数将record还原。
sendtext不就搞定了吗?
 
多人接受答案了。
 
还不简单!!
把记录结构做为单独一个单元!如:
unit typerecord


interface

type
arecord=record
a:integer;
b:string;
.
.
end;
然后在 serversocket 和clientsocket 都家一个 uses typerecord;
在两端都用 sengbuf(typerecord,sizeof(@typerecord)) 发送。
用receivebuf(typerecord,receivelength) 接受
 
后退
顶部