用ServerSocket和ClientSocket控件进行数据传输时,用ReceiveBuf()函数接受不到数据,请问怎么解决?
代码:
服务端
unit serverUnit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
{type
TBuffer=Record
name:string;
sex:string;
end;}
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
// rbuf:TBuffer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ServerSocket1.Active:=True;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ServerSocket1.Active:=False;
end;
procedure TForm1.ServerSocket1Accept(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.RemoteHost+' '+Socket.RemoteAddress);
// Socket.ReceiveBuf(rbuf^,sizeof(TBuffer));
// Memo1.Lines.Add(rbuf^.name+' '+rbuf^.sex);
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
// name,sex:string;
rbuf:^string;
begin
getmem(rbuf,sizeof(string));
if Socket.Connected then
try
Memo1.Lines.Add(Inttostr(socket.ReceiveLength));
Socket.ReceiveBuf(rbuf^,socket.receivelength);
memo1.Lines.add(inttostr(length(rbuf^)));
// name:=rbuf.name;
// sex:=rbuf.sex;
Memo1.Lines.Add(rbuf^)
except
showmessage('连接错误');
ServerSocket1.Close;
Freemem(rbuf);
end;
end;
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
// showmessage(errorevent);
showmessage(Inttostr(errorcode));
end;
end.
客户端
unit ClientUnit4;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
{type
TBuffer=Record
name:string;
sex:string;
end;}
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
// sbuf:TBuffer;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ClientSocket1.Open;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ClientSocket1.Active:=False;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
sbuf:^string;
begin
getmem(sbuf,sizeof(string));
sbuf^:='name';
label1.Caption:=Inttostr(Length(sbuf^));
// sbuf.sex:='sex+ldofw;dsofe;apdfk;asdfhfioeklnalsdnfienaldnfe';
ClientSocket1.Socket.SendBuf(sbuf,sizeof(string));
sleep(100);
end;
end.
在服务器端接收数据时,Memo1.Lines.Add(Inttostr(socket.ReceiveLength));这句显示的时发送的字节数,memo1.Lines.add(inttostr(length(rbuf^)));这句显示rbuf为空。
请问错在那里了?我在网上找了很长时间都没找到原因,请高手指教。