ClientSocket连接不成功3980次就报告错误,说缓冲区不足的,不知大家没有见过。
代码如下:大家试试,一直找不出问题的所在,郁闷中。
var
  Form1: TForm1;
  i:integer;implementation{$R *.dfm}procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if ClientSocket1.Active then
    ClientSocket1.Close;
  ClientSocket1.Open;
  inc(i);
  caption:=intToStr(i);
end;procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  ErrorCode:=0;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Enabled:=true;
  Timer1.Interval:=2;
  ClientSocket1.Address:='127.0.0.1';
  ClientSocket1.Port:=8000;
  i:=0;
end;

解决方案 »

  1.   

    不知道你的 Timer1 的时间间隔设置为多久,
    不因该太短, ClientSocket.Open; 打开连接是需要些时间的
    代码改为以下试试:
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      if (ClientSocket1.Socket<>nil) or ( ClientSocket1.Active) then
        ClientSocket1.Close;
      ClientSocket1.Open;
      inc(i);
      caption:=intToStr(i);
    end;procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      ErrorCode:=0;
      Socket.Close;
      ClientSocket1.Close;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Timer1.Enabled:=true;
      Timer1.Interval:=5000;
      ClientSocket1.Address:='127.0.0.1';
      ClientSocket1.Port:=8000;
      i:=0;
    end;
      

  2.   

    procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      ErrorCode:=0;
      Socket.Close;   // 关闭套接字,否则重复连接导致资源不足
    end;
      

  3.   

    把timer的时时间设得短是为了测试用,如果设得长的话,则几个小时后一样结果,与这无关的。
    我把socket.close也一样报错误。错误如下:
    由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
    大家测一测。
    有没有高手啊
      

  4.   

    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      if ClientSocket1.Active then
        ClientSocket1.Close;
      ClientSocket1.Open;       // 不能关了就马上连接的,因为关闭操作还没完成,资源还没释放。
      inc(i);
      caption:=intToStr(i);
    end;只能等到 ClientSocket1Disconnect 事件发生后(套接字关闭操作真正完成)再在事件处理程序外控制 ClientSocket1 重新进行连接。我用TClientSocket/TServerSocket控件写过断线自动连接的程序,这组控件不能像这样频繁连接的。
      

  5.   

    if ClientSocket1.Active then
    begin
        ClientSocket1.Close;
        Sleep(60000);    //加上以后绝对OK,
    end;
      ClientSocket1.Open;还有一个更好的办法,就是修改SOCKET选项,setsockopt函数,设置SOCKET没有半等待状态,当然这样的话,楼主的代码要稍微修改下
      

  6.   

    TO: DDGG(叮叮当当) 
    当ClientSocket连接不上时,根本就无法触发ClientSocketDisconnect。难倒连接一次不成功时,就永远无法真正关闭套接字吗。。具体怎么写呢,我照上面说的作过了,还是不行,我设timer=2
    仅仅只是为了测试,只是为了快速得到结果,实际会设长一点,但运行一段时还是报错误啊。
    有没有好的解决方法。急切等待中!!!!!!!
      

  7.   

    //当ClientSocket连接不上时,根本就无法触发ClientSocketDisconnect
    你错了,ClientSocket连接不上时,是会触发ClientSocketError事件的,当然这当中有一个超时取值的问题,但这是TClientSocket控件内定的,没办法更改。
    ClientSocketError事件触发后,由于我们在事件处理程序中写了 ErrorCode:=0; Socket.Close; ,所以就会触发事件 ClientSocketDisconnect。
      

  8.   

    TO: DDGG(叮叮当当) 
    当ClientSocket连接不上时,确实无法触发ClientSocketDisconnect,我代码如下,请你试试
    procedure Delay(Msecs: Integer);
    var
      FirstTickCount: real;
    begin
      FirstTickCount:= GetTickCount;
      Repeat
        Application.ProcessMessages ;
      Until((GetTickCount - FirstTickCount) >= LongInt(Msecs));
    end;procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      ClientSocket1.Close;
      ErrorCode:=0;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
    Timer1.Enabled:=true;
      Timer1.Interval:=5;
      ClientSocket1.Address:='127.0.0.1';
      ClientSocket1.Port:=8000;
      ClientSocket1.Open;
      i:=0;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      if ClientSocket1.Active then
        ClientSocket1.Close;
      //Delay(3);
      //application.ProcessMessages;end;procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    begin
      ClientSocket1.Open;
      inc(i);
      caption:=intToStr(i);
    end;
      

  9.   

    因为你连接失败所用winsocket没释放
      

  10.   

    to:aus(天兵) 
    //因为你连接失败所用winsocket没释放
    要怎样释放呢,以否说具体一点
      

  11.   

    期待中
    我也有这个问题
    估计是delphi的问题
    即时clientsocket被free了,泄漏的内存还是存在
    查了这个控件中几乎可能和内存相关的属性也没有办法释放掉泄漏的内存
      

  12.   

    TO: qzxyd(只会种菜)procedure TForm1.ClientSocket1Error(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    begin
      ErrorCode:=0;
      Socket.Close;   // 关闭套接字,否则重复连接导致资源不足
                      // 注意这里是Socket.Close,不是ClientSocket1.Close;
    end;