户机向服务器发送消息时,服务器没开机或重启,在客户机怎么处理?(正常情况下,你发一个消息过去,服务器发一个消息让你通过!)比如说有这样一个属性么?ClientSocket.没有收到任何东西:=True;有没有这样一个方法去知道有没有消息收到!(或者在一段时间内没有收到任何消息!)

解决方案 »

  1.   

    我现在一般都用indy的TCPClient:
    try
      IdTCPClient1.Connect;
    except
      On E: EIdSocketError do ...
    end;
      

  2.   

    服务器的IP你知道的吧?如果服务器关机了,就PING不通它了。所以用PING就可以了啊。
      

  3.   

    unit Unit_Share_Lqb;
    interface
    uses
    Windows,Dialogs,winsock,ShellApi,Sysutils,FileCtrl,ExtCtrls,tlhelp32,Forms,stdctrls;
    function PingDestIP_Lqb(IP_Str:String):Boolean;
    type
    //**********PingDestIP_Lqb Begin**************************************
    PIPOptionInformation = ^TIPOptionInformation;
      TIPOptionInformation = packed record
    TTL: Byte;
        TOS: Byte;
        Flags: Byte;
        OptionsSize: Byte;
        OptionsData: PChar;
      end;
    PIcmpEchoReply = ^TIcmpEchoReply;
      TIcmpEchoReply = packed record
       Address: DWORD;
        Status: DWORD;
        RTT: DWORD;
        DataSize: Word;
        Reserved: Word;
        Data: Pointer;
        Options: TIPOptionInformation;
      end;
    TIcmpCreateFile = function: THandle; stdcall;
      TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;
      TIcmpSendEcho=function(IcmpHandle:THandle;DestinationAddress:DWORD;RequestData:Pointer;
    RequestSize: Word;RequestOptions: PIPOptionInformation;ReplyBuffer: Pointer;
        ReplySize: DWord;Timeout: DWord):DWord; stdcall;
    //**********PingDestIP_Lqb End**************************************implementationfunction  PingDestIP_Lqb(IP_Str:String):Boolean;
    var
    hICMP: THANDLE;
      IcmpCreateFile:TIcmpCreateFile;
      IcmpCloseHandle:TIcmpCloseHandle;
      IcmpSendEcho:TIcmpSendEcho;
    IPOpt:TIPOptionInformation;// IP Options for packet to send
      FIPAddress:DWORD;
      pReqData,pRevData:PChar;
      pIPE:PIcmpEchoReply;// ICMP Echo reply buffer
      MyString:string;
      FTimeOut:DWORD;
      BufferSize:DWORD;
    begin
    Result:=False;
      @ICMPCreateFile:=GetProcAddress(LoadLibrary('icmp.dll'),'IcmpCreateFile');
      @IcmpCloseHandle:=GetProcAddress(LoadLibrary('icmp.dll'),'IcmpCloseHandle');
      @IcmpSendEcho:=GetProcAddress(LoadLibrary('icmp.dll'),'IcmpSendEcho');
      hICMP := IcmpCreateFile; FIPAddress := inet_addr(PChar(IP_Str));
      BufferSize := SizeOf(TICMPEchoReply) + 40;
      GetMem(pRevData,40);
      GetMem(pIPE,BufferSize);
      FillChar(pIPE^, SizeOf(pIPE^), 0);
      pIPE^.Data := pRevData;
      MyString := 'Hello,World';
      pReqData := PChar(MyString);
      FillChar(IPOpt, Sizeof(IPOpt), 0);
      IPOpt.TTL := 64;
      FTimeOut := 10;
      if IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString), @IPOpt, pIPE, BufferSize, FTimeOut)=1 then
        if pReqData^ = pIPE^.Options.OptionsData^ then Result:=True;
      IcmpCloseHandle(hICMP);
      FreeMem(pRevData);
      FreeMem(pIPE);
    end;
      

  4.   

    up
    很简单,用服务器名找IP地址,如果IP地址为空,找不到服务器
    这类文章请在CSDN中搜索,抄一段函数,作一下判断即可,只是方式换一下而以,比楼上的代码更简捷
      

  5.   

    如果是这样的话,只要服务器开着,就能PING通,
    好像这样也不行吧!
      

  6.   

    Good Good  Study Day  Day     Up
    我up
      

  7.   

    你用ClientSocket连接服务器,对方关机,肯定OnError被调用,连不起,不用发消息吧
      

  8.   

    设置ClientType为ctNonBlocking, 当发出一串报文时,在ClientSocket的OnRead事件是写上:
    var
      str: Pchar;
      temp: array[0..1023] of Char;
    begin
      temp := nil;
      try
        Socket.ReceiveBuf(temp,sizeof(temp));
        StrPCopy(temp, Str);
        if str = emptystr then
          showmessage('Cannot connected to SocketServer');
      except
      end
    end;
      

  9.   

    try
      IdTCPClient1.Connect;
    except
      On E: EIdSocketError do ...
    end;
    这样做的话,如果有异常他自己也会报一个错的?!