就是因为网络不通,那它总得提示返回说网络不通,
问题是如何返回这个值。
我使用的是delphi6.

解决方案 »

  1.   

    你设置了ServerName或者ServerGUID属性了吗?
      

  2.   

    那当然了,scktsrvr.exe同样也找开了。该设的我全设了。
      

  3.   

    告诉你这样,既然你给设的都设了,你不要写Try ...Except..,看看出现什么样异常,最好能把异常信息得到,这样就知道那里有错了!
      

  4.   

    语句没出错。
    try 前加:data1.SocketConnection1.Connected:=false;
      

  5.   

    另外:data1.SocketConnection1.Connected:=true;//当连接失败时,整个程序就死在那里.
    后加:exit;
      

  6.   

    你的tcpip里是不是设了dns和网关了
    如果是那样连接就会变慢
      

  7.   

    我是想知道socketconnection的host连接失败时,
    如何立即传回连接失败。
    summernightrain(夏夜雨):我是想在程序出错时,屏蔽这个出错信息。
    lanren_me(阿波) :我就是不能立即得到连接失败的信息,至少要等到10多分钟才会回传信息这
                     样的等待是无法忍受的。
    snjun(取个啥名儿好呢?!):就是因为连接速度慢,我要如何加快这个速度。
      

  8.   

    用Try Except end的确无法捕获该连接错误,你必须在Application.OnException事件中通过判断参数E的错误类别来捕捉它,例如:
      if E is EoleException then begin
        DoSomeThing;
      end
    等十多分钟太夸张了吧,二三分钟差不多。
      

  9.   

    Guoxc(大浪乘风) :终于有人理解了!不过我确实是等太久是真是无法忍受。
    你所说的参数E的错误具体如何定义,能讲得详细一点吗?谢谢!
    分数多少不成问题。
      

  10.   

    Guoxc(大浪乘风):能讲得详细一点吗?谢谢!
      

  11.   

    对于参数E,在连接出错是系统会自动弹出一个出错对话框你认真看一下会看到其中有出错的类别,例如有EOleSysError,由此可以判断E的类名,用如下语句:
      if E is EoleSysError then
         ShowMessage(Inttostr((E as EOleSysError).ErrorCode));
    可得出错误码,记下该错误码然后可以用如下语句:
      if E is EoleSysError then
        if (E as EOleSysError).ErrorCode = 错误码 then
          DoSomeThing;
      

  12.   

    不行,你的这个语句要放在哪里。
    我是这样写的可是还是不行。
     socketconnection1.host:='shd01ffff';
       socketconnection1.Connected:=true;
    if E is EoleSysError then  .......
      

  13.   

    放在Application.OnException事件中
      

  14.   

    我怎么没看见这个?  Application.OnException
      

  15.   

    将Additional控件面板下最右边数第二个控件TApplicationEvents放到主窗本上,在该控件的事件中有。 
      

  16.   

    我将你上面的语句放在TApplicationEvents的事件中
    可是运行结果还是一样,不会返回结果还要设置什么吗?
      

  17.   

    不用设置了啊,所有出错都会触发OnException事件,你在该事件中设置断点跟踪一下看,要等一段时间的。
      

  18.   

    我的整个程序就这样对吗?
    procedure TForm1.Button2Click(Sender: TObject);
    begin
     socketconnection1.host:='shd01ffff';
     socketconnection1.Connected:=true;
    end;procedure TForm1.ApplicationEvents1Exception(Sender: TObject;
      E: Exception);
    begin
    showmessage('11');
    if E is EoleSysError then
        ShowMessage(Inttostr((E as EOleSysError).ErrorCode));
    end;
      

  19.   

    程序没问题,主机shd01ffff存在吗?有没有对应的应用程序?
      

  20.   

    我是故意的,当主机不存在时,捕获错误信息,可我不能立即回传错误。
    我在程序中调试,一会儿就会提示出错(socketconnection1.Connected:=true;)
    可就是无法继续执行下一条,还有也不会执行到这里(if E is EoleSysError then)
    这是为什么?
      

  21.   

    主机不存在就是raise socket Error
    那你就把做Socket异常处理。
    uses
      ScktComp;procdure MySocketError(ErrorCode: Integer);
    begin
      ShowMessage(Format('Error: %s', SysErrorMessage(ErrorCode));
    end;buttononclick:
    begin
      ScktComp.SetErrorProc(MySocketError);
      try
        SocketConnection1.Connected := True;
      finally
        ScktComp.SetErrorProc(nil);
      end;
    end;
      

  22.   

    ShowMessage(Format('Error: %s', SysErrorMessage(ErrorCode));
    这句无法通过如何改。
      

  23.   

    ShowMessage(Format('Error: %s', [SysUtils.SysErrorMessage(ErrorCode)]);
    忘打[]
      

  24.   

      SocketConnection1.Connected := True;  //
    我用断点调试,当这句出错时,提示'sindows socket error:(10049),on api 'connect''
    然后程序不会往下运行,就一直停留在这句上。
      

  25.   

    老兄,这不就是raise吗,如果不是调试环境,这个raise就是在MySocketError中处理的。
    不信你编绎后不要在delphi下运行,然后在
    procedure MySocketError(ErrorCode: Integer);
    begin
      ShowMessage('dana, Socket出错了,错误代码是:%d,可以用SysErrorMessage函数看一下错误代码的意思');
    end;如果SocketConnection的异常是在MySocketError中处理的,那么就是Socket的错,
    如果不是,那就是你的服务端程序的问题了
      

  26.   

    D6没试,
    》》没错,我不在调试状态下同样也是不行
    什么意思?
    主机不存在,连接肯定会出错,所以在MySocketError作你的错误处理,
    还会不会连接10分钟啦。
     
    var
      Form1: TForm1;
    procedure MySocketError(ErrorCode: Integer);
    begin
      Form1.ErrorProc(ErrorCode);
    end;procedure TForm1.ErrorProc(ErrorCode: Integer);
    begin
      MessageBox(Handle, '连接服务器主机出错,请确定选择的是服务器。', 'Error', MB_OK + MB_ICONERROR);
      //do other thing
    end;不然你用TClientSocket试一下连接主机,连接成功后再试其它原因。
      

  27.   

    如此,唉,D6拿来玩,D5来做事,d6只闻其名,不见其身,等我什么时候有空了,我再赶赶集,买几张碟回来,hoho,好久没出去了。
      

  28.   

    dana,可否联系我。[email protected]
      

  29.   

    贴片文章,不是灌水::)
    Handling Winsock errors - by Borland Developer Support Staff Technical Information DatabaseTI4524D.txt - Handling Winsock errorsCategory   :Windows API
    Platform   :All-32Bit
    Product    :All32Bit,   Description:
    For any of the following exception handling methods to work,
    the VCL must in some way become aware that an error condition
    exists.  If a call to the Winsock does not return, or does
    not provide information to the TCustomWinSocket descendant
    that called it, then there is no mechanism to handle the condition.  
    OnError exception handler One method for trapping exception conditions in a descendant of
    TCustomWinSocket is to use an OnError exception handler.   This
    method will only handle a limited set of conditions because the
    mechanism provided by the Winsock for event notification only
    reacts to a limited list of conditions.  To be notified of an
    exception condition within the Winsock, TCustomWinSocket registers
    user message CM_SocketMessage to be sent to the component, and the
    CMSocketMessage message handler raises an exception.  The message
    is registered with the Winsock by an API call to WSASyncSelect.
    WSASyncSelect is a request for event notification of socket read,
    writes, connect, close, and accept events.  If the exception
    condition is not read, write, connect, close or accept, or if the
    CM_SocketMessage is not sent by the Winsock for any reason, the
    error handler will not fire.Usage: procedure TChatForm.ClientSocketError(Sender: TObject;
      Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
      var ErrorCode: Integer);
    const
      ErrorEvents: array[eeGeneral..eeAccept] of string = (
        'eeGeneral',
        'eeSend',
        'eeReceive',
        'eeConnect',
        'eeDisconnect',
        'eeAccept'
      );
    begin
    ListBox1.Items.Add('ClientSocketError.   TErrorEvent: ' +
      ErrorEvents[ErrorEvent] + '    ErrorCode: ' + IntToStr(ErrorCode));
      ErrorCode := 0; // don't raise an exception
    end;
    Definition: procedure TCustomWinSocket.CMSocketMessage(var Message: TCMSocketMessage);  function CheckError: Boolean;
      var
        ErrorEvent: TErrorEvent;
        ErrorCode: Integer;
      begin
        if Message.SelectError <> 0 then
        begin
          Result := False;
          ErrorCode := Message.SelectError;
          case Message.SelectEvent of
            FD_CONNECT: ErrorEvent := eeConnect;
            FD_CLOSE: ErrorEvent := eeDisconnect;
            FD_READ: ErrorEvent := eeReceive;
            FD_WRITE: ErrorEvent := eeSend;
            FD_ACCEPT: ErrorEvent := eeAccept;
          else
            ErrorEvent := eeGeneral;
          end;
          Error(Self, ErrorEvent, ErrorCode);
           if ErrorCode <> 0 then
            raise ESocketError.CreateFmt(sASyncSocketError, [ErrorCode]);
        end else Result := True;
      end;begin
      with Message do
        if CheckError then
          case SelectEvent of
            FD_CONNECT: Connect(Socket);
            FD_CLOSE: Disconnect(Socket);
            FD_READ: Read(Socket);
            FD_WRITE: Write(Socket);
            FD_ACCEPT: Accept(Socket);
          end;
    end;
    Object Pascal Exception Handling
    You can also wrap a specific call in a try..except block or
    setting an application level exception handler.  For this to
    work, the component must in some way become aware of the
    exception condition and an exception must be raised for the
    exception to be trapped here.Example of Application Exception Handler: TChatForm = class(TForm)
    .
    .
      public
        procedure AppException(Sender: TObject; E: Exception);
      end;
    .
    .
    implementation
    .
    .
    procedure TChatForm.AppException(Sender: TObject; E: Exception);
    begin
      ListBox1.Items.Add('AppException: ' + E.Message);
    end;procedure TChatForm.FormCreate(Sender: TObject);
    begin
      Application.OnException := AppException;
    end;
    Example of Try..Except block:with ClientSocket do
      try
        Active := True;
      except
        on E: Exception do
          ListBox1.Items.Add('Try..except during open: ' + E.Message);
      end;
    end;SocketErrorProc 
    For calls that use the CheckSocketResult function to check the
    result returned by WSAGetLastError, errors can be handled in a
    programmer defined function by setting the SocketErrorProc.Usage: Interface
    .
    .
    procedure MySocketError(ErrorCode: Integer);implementation
    .
    .
    procedure MySocketError(ErrorCode: Integer);
    begin
      ShowMessage('MySocketError: ' + IntToStr(ErrorCode));
    end;procedure TChatForm.FormCreate(Sender: TObject);
    begin
      SocketErrorProc := MySocketError;
    end;
    Defined: function CheckSocketResult(ResultCode: Integer; const Op: string):
     Integer;
    begin
      if ResultCode <> 0 then
      begin
        Result := WSAGetLastError;
        if Result <> WSAEWOULDBLOCK then
          if Assigned(SocketErrorProc) then
            SocketErrorProc(Result)
          else raise ESocketError.CreateFmt(sWindowsSocketError,
            [SysErrorMessage(Result), Result, Op]);
      end else Result := 0;
    end;
    Help Text for SocketErrorProc: Unit ScktCompSocketErrorProc handles error messages that are received from a Windows socket
    connection.threadvar SocketErrorProc: procedure (ErrorCode: Integer);Assign a value to SocketErrorProc to handle error messages
    from Windows socket API calls before the socket component raises an
    exception. Setting SocketErrorProc prevents the socket component from
    raising an exception.  SocketErrorProc is a thread-local variable. It
    only handles errors that arise from the Windows socket API calls made within
    a single execution thread.