Sockets/es

From Free Pascal wiki
Jump to navigationJump to search

English (en) español (es) français (fr) slovenčina (sk)

Acerca de

Sockets es un paquete modificado-LGPL para Lazarus. Define el componente TSocketClient para conectar aplicaciones a un servidor genérico de sockets en redes que utilizan TCP/IP.

La descarga contiene los ficheros de componentes de Pascal, el fichero de paquete de Lazarus y ficheros de recursos y además los ficheros de texto de la licencia LGPL-modificada.

Este componente se ha diseñado para aplicaciones que se ejecuten en plataformas Linux y Win32.

Autor

Antonio d'Avino

Licencia

LGPL modificada (leer COPYING.modifiedLGPL y COPYING.LGPL, ambos incluidos en el paquete).

Descarga

Se puede encontrar la última versión estable en Lazarus CCR Files page o en los sites del autor http://infoconsult.homelinux.net.

  • Actualmente también puedes descargar el paquete Sockets desde aquí.

Log de cambios

  • Versión 0.2.0 2005/06/01

Roadmap

  • Implementation of asynchronous mode in write operations.
  • Adding of TSocketServer class to package

Dependencias / Requerimientos del sistema

  • Lazarus 0.9.6 (FPC 1.9.8)

Stado: Beta

Incidencias: Testeado en Windows (Win2K) y Linux (Mdk 10.1).

Installation

  • In the lazarus/components directory, untar (unzip) the files from sockets-laz-package-<version>.tar.gz file. The sockets folder will be created.
  • Open lazarus
  • Open the package sockets_laz.lpk with Component/Open package file (.lpk)
  • (Click on Compile only if you don't want to install the component into the IDE)
  • Click on Install and answer 'Yes' when you are asked about Lazarus rebuilding. A new tab named 'Sockets' will be created in the components palette.

Note for Win32 users: In case you experience some difficults in compiling the Pascal files, you may need to add the following path: <some-drive>:<some-path>\lazarus\lcl\units\i386\win32 (ie: c:\programs\lazarus\lcl\units\i386\win32) to the 'Other unit files ...' in 'Paths' tab of 'Compiler Options' of the lazarus package manager window.

Usage

Drop a TSocketClient component on a form, for any different client socket connections your application needs. Mandatory property of this component you have to set are:

  • Host : the TCP/IP address or the URL of the PC where the server sockets you want to connect to is running;
  • Port : the TCP/IP port on wich the server process is listening;

Optional properties you may set are:

  • DosLf : If set to 'True', a CR (0x0D) is prepend to LF (0x0A) when a writeLineLf procedure is executed (see forward).
  • ReadCharsThereshold : the number of characters read from channel before a OnReadChars event is triggered (see forward). 0 is equivalent to 1.

Ahora ya se puede establecer al valor 'True' la propiedad Active para abrir el canal. Se genera una excepción si falla la conexión (host no alcanzable, la no existencia un un proceso escuchando en un puerto del servidor, errores de red...).

La conexión se cierra estableciendo al valor 'False' la propiedad Active. Además, se puede testear la propiedad Active para verificar si el estado de la conexión esta en ON (el cierre de la conexión por parte del servidor establece la propiedad Active en 'False'.


Métodos públicos de la clase TSocketClient.

  • function bufferChars : Integer. It returns the number of chars received from the server process through the channel and available for reading.
  • function getBuffer (var buffer: String) : Integer. Esta función copia los caracteres del buffer interno del componente en la cadena de parámetro 'buffer'. Retorna el número de caracteres copiados y vacía el buffer.
  • function readLine (var buffer: String) : Boolean. The characters in internal buffer are copied into the parameter string 'buffer', upto, but not including, the first LF (0x0A9) character occurrence. Any CR characters (0x0D) are discarded.

Si se copia la línea completa, entonces la función retorna true. Si no encuentra algún caracter LF, entonces copia el buffer interno completo en el parámetro 'buffer' y a continuación la función retorna false.

Nota: el contenido pendiente en el buffer interno se encuentra disponible además cuando se cierra la conexión. Solamente se perderá si la conexión de vuelve a abrir.

  • function writeChar (buffer : Char ) : ShortInt. This function write a single char on channel, returning 0 in case of success or a value less than 0 if it fails.
  • function writeLine (buffer : String) : ShortInt. The content of parameter buffer is sent to server. The function returns 0 in case of success or a value less than 0 if it fails.
  • function writeLineLf (buffer : String) : ShortInt. Same of writeLine function, but a LF char (0x0A) is appended (a couple of CR/LF, if property DOSLf is true). The function returns 0 in case of success or a value less than 0 if it fails.

Note: at this moment, the write methods of the class are not asynchronous, so they may be blocking for your application in case of failing. This will be fixed in next release.

Events of TSocketClient class.

  • OnBufferFull : this event is triggered when the internal buffer is full. No others characters are received from channel until characters are read from internal buffer.
  • OnEndOfLineReceive : this event occurs where a LF character (0x0A) is received from server.
  • OnReadChars : this event is triggered when a number of at least ReadCharsThereshold is pending in internal buffer.
  • OnStateChange : this event occurs any time the Active property change its value.