Sconnect
Previous Topic  Next Topic 

Synopsis: 

       socket=Sconnect(addr,port[,timeout_10ths])  


Arguments:

       address - The IP address to connect to

       port - The IP port to connect to

       [timeout_10ths] - Optional timeout (in 10th seconds) to wait for the connection to complete

       

Description:  This function will attempt to make a TCP connection to the IP address and port specified as arguments.    The IP address can be specified either in dot notation (192.168.2.1) or as a named address.    The function looks at the first character of the address and if this character is numeric then it will assume that the address is in dot notation, otherwise it will assume it is a named address.


If the optional argument timeout_10ths is specified then the function will block and will not return until the timeout expires or the connection completes and will return a handle to the connecting socket (or a negative error value).    


If the timeout_10ths argument is not specified then the function will always return immediately with the connecting socket handle (or a negative error value).


Note that the connecting socket handle will not be fully useable for sending or receiving data until the socket connection has completed.    If an attempt is made to send or receive on a socket that has not yet fully connected then the function will return -3 (EWOULDBLOCK).


The programmer can use Scheck() to check if the socket is writable or in error as a way of checking if the connection has completed or has failed to connect.


Example:


       // Attempt to make a socket connection

       socket=Sconnect("google.com",80);


       // Loop waiting for connection or error

       while(1)

               // Check if socket is ready for write (connection complete)

               x=Scheck(socket,1);

               if(x eq 1)

                       break;

               endif


               // Check for error

               x=Scheck(socket,2);

               if(x eq 1)

                       errlog("Error could not connect to google.com");

                       stop;

               endif

               // prevent tight loop to allow windows to receive messages..

               sleep(1);

       endwhile


       // If we get here we are connected...

       etc



Returns:    The function will return the connecting socket handle (which is used in subsequent calls to socket functions) or else a negative error value.