TWSocket.Connect

From Overbyte
Jump to: navigation, search

Main page -> ICS component reference -> TWSocket -> Connect

Definition

method Connect;

Description

Example

Best practices

How to

This method try to connect to remote Server.

Whenever OnSessionClosed event will be fired is case on problem during connection, you may get exception error just right after the .Connect if you did not (re)filled .Port and .Addr values before calling .Connect or you'll raise an exception.

You may also get an exception if you have some local socket problem (i.e a blocking firewall not allowing your application to create outgoing socket).

Here is a sample of connecting, you can also read .OnSessionClosed to get more info about this sample.

    WM_CONNECT2SERVER:
    begin
      try          
        if WSocket2Server.State <> wsClosed
        then WSocket2Server.close;
        inc(WSocket2Server.RetryCount);
        WSocket2Server.Port:=StringListConfigFile.Values['ServerPort'];
        WSocket2Server.Addr:=StringListConfigFile.Values['ServerAddr'];
        WSocket2Server.Connect;
      except
        // Yes you may have an error with no SessionClosed
        // For example if a local firewall is blocking socket
        on E: Exception do
        begin
          LogConnector(E.Message);
          if TimeSetEvent(WSocket2Server.RetryDelay,100,@TimeCallBackReconnect,0,TIME_ONESHOT)=0
          then LogConnector('TimeSetEvent creation error');
          // WSocket2Server.RetryDelay may be altered if Server
          // sent some 'server will be off for xxx minutes'
          // just before a clean close
        end;
      end;
    end;