| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1790 人关注过本帖
标题:socket的receivebuf()函数接收数据问题
只看楼主 加入收藏
noable
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-8-17
收藏
 问题点数:0 回复次数:0 
socket的receivebuf()函数接收数据问题

用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为空。

请问错在那里了?我在网上找了很长时间都没找到原因,请高手指教。

搜索更多相关主题的帖子: socket 函数 receivebuf 数据传输 string 
2007-08-17 16:52
快速回复:socket的receivebuf()函数接收数据问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023327 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved