首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

TSocketServer与TSocketClient通信报错(指针报错,获取到的socket.data一直为nil)

2013-11-03 
TSocketServer与TSocketClient通讯报错(指针报错,获取到的socket.data一直为nil)这是我申明的一个记录:typ

TSocketServer与TSocketClient通讯报错(指针报错,获取到的socket.data一直为nil)
这是我申明的一个记录:
type
  TCon=record
    ConType:string;
    Status:Integer;
  end;
  PCon=^TCon;

客户端发送信息:
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
  var C:pcon;
begin
  if(Key=vk_return)then
  begin
    if(Memo1.Text<>'')then
    begin
      c:=new(Pcon);
      c^.ConType:='Text';
      c^.Status:=0;
      ClientSocket1.Socket.Data:=c;
      ClientSocket1.Socket.SendText(Memo1.Text);
    end;
  end;
end;
服务端接收信息:
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
  var C:Pcon;
begin
  c:=Pcon(Socket.Data);
  ShowMessage('Type:'+c^.ConType+'状态:'+inttostr(c^.Status));
  Memo2.Lines.Add(Socket.ReceiveText);
end;

我打断点跟踪到红色的那一行,发现socket.data为nil,有经验的帮我看看,弄好长时间了没弄出来

[解决办法]
不推荐使用这2个控件,不是太稳定的。建议使用TTcpClient  和 TTcpServer 这2个控件。
http://download.csdn.net/detail/unearth/6485055  去下载我写的示例代码。

热点排行