Home Units Hierarchy Routines All identifiers

Class unaSocket

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type unaSocket = class(unaObject)

Description

Base class implementing Windows socket.

Hierarchy

Overview

Methods

Public constructor create(); overload;
Public constructor create(socket: tSocket; addressFamily, socketProtocol, socketType: int; const addr: sockaddr_in; len: int); overload;
Public destructor Destroy(); override;
Public function accept(out socket: tSocket; timeout: tTimeout = tTimeout(INFINITE)): unaSocket;
Public function bind(addr: pSockAddrIn = nil): int; virtual;
Public function bindSocketToPort(port: int = -1): int;
Public function check_error(timeout: tTimeout = 0): bool;
Public function close(graceful: bool = true): int;
Public function connect(addr: pSockAddrIn = nil): int;
Public class function getGeneralMTU(): uint;
Public function getMTU(): uint;
Public function getOptBool(opt: int): bool;
Public function getOptInt(opt: int): int;
Public function getPendingDataSize(noCheckState: bool = false): uint;
Public function getPort(): string;
Public function getPortInt(): word;
Public function getSockAddr(out addr: sockaddr_in): int;
Public function getSockAddrBound(out addr: sockaddr_in): int;
Public function getSocket(forceCreate: bool = false): tSocket;
Public function isConnected(timeout: tTimeout = 0): bool;
Public function isListening(): bool;
Public function listen(backlog: int = SOMAXCONN): int;
Public function okToRead(timeout: tTimeout = 0; noCheckState: bool = false): bool;
Public function okToWrite(timeout: tTimeout = 0; noCheckState: bool = false): bool;
Public function read(const buf: pointer; var size: uint; timeout: tTimeout = tTimeout(INFINITE); noCheck: bool = false): int;
Public function readString(noCheck: bool = false): aString;
Public function send(const value: aString; noCheck: bool = false): int; overload;
Public function send(buf: pointer; size: uint; noCheck: bool = false): int; overload;
Public function setOptBool(opt: int; value: bool): int;
Public function setOptInt(opt: int; value: int): int;
Public function setPort(port: word; noCheck: bool = false): bool; overload;
Public function setPort(const port: string; noCheck: bool = false): bool; overload;
Public function shutdown(how: int): int;
Public function waitForData(timeout: tTimeout = tTimeout(INFINITE); noCheckState: bool = false): bool;
Protected function closeSocket(): int; virtual;
Protected function ioctl(command: uint; var param: int32): int; virtual;
Public procedure setHost(const host: string);

Properties

Public property addressFamily: int read f_addressFamily;
Public property bindToIP: string read f_bindToIP write setBindToIP;
Public property bindToPort: string read getBindToPort write setBindToPort;
Public property handle: tSocket read f_socket;
Public property host: string read f_host write setHost;
Public property isActive: bool read getIsActive;
Public property rcvBufSize: uint read getBufSizeRcv write setBufSizeRcv;
Public property sndBufSize: uint read getBufSizeSnd write setBufSizeSnd;
Public property socketProtocol: int read f_socketProtocol;
Public property socketType: int read f_socketType;

Description

Methods

Public constructor create(); overload;

Initializes the socket.

Parameters
overlapped
Create overlapped socket (default is True).
Public constructor create(socket: tSocket; addressFamily, socketProtocol, socketType: int; const addr: sockaddr_in; len: int); overload;

Initializes the socket. This constructor is mostly used by TCP server with sockets handle returned by accept().

Parameters
socket
Windows socket handle to assign to socket object.
addressFamily
Address family (AF_INET/AF_NETBIOS/AF_INET6/AF_IRDA/AF_BTM, etc).
socketProtocol
Proto (IPPROTO_TCP/IPPROTO_UDP/IPPROTO_RM, etc).
socketType
Socket type (SOCK_STREAM/SOCK_DGRAM/SOCK_RAW/SOCK_RDM/SOCK_SEQPACKET, etc).
addr
Remote address.
len
Size of remote address.
Public destructor Destroy(); override;

Closes and destroys the socket.

Public function accept(out socket: tSocket; timeout: tTimeout = tTimeout(INFINITE)): unaSocket;

Blocks until new connection will be established with socket, or timeout value expires. NOTE: this method should be used with TCP sockets only.

Parameters
socket
[OUT] New socket handle.
timeout
Timeout for operation.
Returns

New connected client socket, or nil if timeout or error occur.

Public function bind(addr: pSockAddrIn = nil): int; virtual;

Binds a socket to local address.

Parameters
addr
Local address to bind to.
Returns

0 if successfull, or specific WSA error.

Public function bindSocketToPort(port: int = -1): int;

Binds the socket to specified port. If port value is -1 (default), this function uses the bindToPort property as a port number to bind to. If port value is 0, this function auto-assigns first available port number for socket.

Returns

0 if successfull (or socket is not connected/listening), or specific WSA error.

Public function check_error(timeout: tTimeout = 0): bool;

Checks if there is some error with socket.

Parameters
timeout
Timeout in milliseconds (default is 0).
Returns

True if there are some error.

Public function close(graceful: bool = true): int;

Closes the socket.

Parameters
graceful
If True, socket will gracefully close all operations, and then disconnect (default is True).
Returns

0 if successfull (or socket was not connected/listening), or specific WSA error.

Public function connect(addr: pSockAddrIn = nil): int;

Connects the socket to remote host.

Parameters
addr
Host/port/proto to connect to. If nil (default) own host/port/proto properties will be used instead.
Returns

0 if socket has been connected successfully or specific WSA error.

Public class function getGeneralMTU(): uint;

Returns maximum transmission unit (MTU) size.

NOTE: this value should not be used as real MTU size, but only as an estimation for most cases.

Public function getMTU(): uint;

Returns maximum transmission unit (MTU) size for connected socket.

Public function getOptBool(opt: int): bool;

Returns boolean option of the socket.

Public function getOptInt(opt: int): int;

Returns integer option of the socket.

Public function getPendingDataSize(noCheckState: bool = false): uint;

Returns number of bytes available to read from socket.

Public function getPort(): string;

Returns remote port value as string.

Public function getPortInt(): word;

Returns remote port value as word.

Public function getSockAddr(out addr: sockaddr_in): int;

Returns sockaddr_in structure filled with address:port values which corresponds to the socket.

Parameters
addr
[OUT] Address record.
Returns

0 if successfull, or a specific WSA error.

Public function getSockAddrBound(out addr: sockaddr_in): int;

Returns sockaddr_in structure filled with address:port values which corresponds to the bound socket. Do not use if socket is not bound yet.

Parameters
addr
[OUT] Address record.
Returns

0 if successfull, or a specific WSA error.

Public function getSocket(forceCreate: bool = false): tSocket;

Returns socket handle.

Parameters
forceCreate
Forces socket creation when true (default is False).
Returns

Socket handle (or INVALID_SOCKET if socket was not created or was closed and forceCreate is False).

Public function isConnected(timeout: tTimeout = 0): bool;

Checks if socket is connected.

Parameters
timeout
Timeout in milliseconds.
Returns

True if socket is connected to remote host, and there was no error.

Public function isListening(): bool;

Checks if socket is listening.

Parameters
timeout
Timeout in milliseconds.
Returns

True if socket is listening, and there was no error.

Public function listen(backlog: int = SOMAXCONN): int;

Binds the socket to specified port and starts listening for incoming connections.

Parameters
backlog
Number of pending client connections (default is SOMAXCONN).
Returns

0 if successfull (or socket is already listening), or specific WSA error.

Public function okToRead(timeout: tTimeout = 0; noCheckState: bool = false): bool;

Checks if it is OK to read data from socket now.

Parameters
timeout
Timeout in milliseconds (default is 0, ignored if noCheckState is True).
noCheckState
Do not check the state of socket (default is False).
Returns

True if there are some chances data could be read now.

Public function okToWrite(timeout: tTimeout = 0; noCheckState: bool = false): bool;

Checks if it is OK to write data into socket now.

Parameters
timeout
Timeout in milliseconds (default is 0, ignored if noCheckState is True).
noCheckState
Do not check the state of socket (default is False).
Returns

True if there are some chances data could be send now.

Public function read(const buf: pointer; var size: uint; timeout: tTimeout = tTimeout(INFINITE); noCheck: bool = false): int;

Reads data from socket.

Parameters
buf
Data buffer.
size
[IN/OUT] Size of buffer, how many bytes to read. On success specifies how many bytes were actually read.
timeout
Timeout in milliseconds (default is INFINITE).
noCheck
Do not check the state of socket (default is False).
Returns

Otherwise returns some specific WSA error.

Public function readString(noCheck: bool = false): aString;

Reads a string from socket.

Public function send(const value: aString; noCheck: bool = false): int; overload;

Sends string of bytes over socket.

Parameters
value
String of bytes to be sent.
noCheck
Do not check socket's state if True. Default if False.
Returns

0 if sent operation done was OK, or specific WSA error.

Public function send(buf: pointer; size: uint; noCheck: bool = false): int; overload;

Sends data over socket.

Parameters
buf
Pointer to data buffer.
size
Size of buffer.
noCheck
Do not check socket's state if True. Default if False.
Returns

0 if sent operation was OK, or specific WSA error.

Public function setOptBool(opt: int; value: bool): int;

Sets boolean option of the socket.

Public function setOptInt(opt: int; value: int): int;

Sets integer option of the socket.

Public function setPort(port: word; noCheck: bool = false): bool; overload;

Sets remote port number for the socket to connect to.

Public function setPort(const port: string; noCheck: bool = false): bool; overload;

Sets remote port for the socket to connect to.

Public function shutdown(how: int): int;

Shuts down the socket. Call close() to clean up the socket before reusing it again.

Parameters
how
One of the following: SD_RECEIVE/SD_SEND/SD_BOTH.
Returns

0 if successfull (or socket was not connected/listening), or specific WSA error.

Public function waitForData(timeout: tTimeout = tTimeout(INFINITE); noCheckState: bool = false): bool;

Waits until data will be available to read from socket or timeout expire.

Protected function closeSocket(): int; virtual;

Closes the socket.

Returns

0 if successfull (or socket was not created), or specific WSA error.

Protected function ioctl(command: uint; var param: int32): int; virtual;

Issues an IOCTL command on a socket.

Parameters
command
IOCTL command.
param
[IN/OUT] Command param.
Returns

0 if successfull, or specific WSA error.

Public procedure setHost(const host: string);

Sets remote host address/DNS name. Does nothing if socket is active (connected/listening).

Properties

Public property addressFamily: int read f_addressFamily;

Specifies socket address family. For most applications you should use the AF_INET value. Other possible values: AF_INET/AF_NETBIOS/AF_INET6/AF_IRDA/AF_BTM

Public property bindToIP: string read f_bindToIP write setBindToIP;

Specifies IP address the socket should bind to. Default value means the socket will bind to any availabe interface.

Public property bindToPort: string read getBindToPort write setBindToPort;

Specifies Port name or number the socket should bind to. Default value (0) means the socket will bind to first availabe port number. Client sockets should always be bind to default port (0), unless you need some special behaviour.

Public property handle: tSocket read f_socket;

Socket handle.

Public property host: string read f_host write setHost;

Specifies remote host value for the socket. This value will be used to connect socket to.

Public property isActive: bool read getIsActive;

Returns True is socket is TCP server socket and is listening, or if socket is TCP client and is connected, or if socket is bind to UDP port/interface. Otherwise returns false.

Public property rcvBufSize: uint read getBufSizeRcv write setBufSizeRcv;

Size of receiving buffer.

Public property sndBufSize: uint read getBufSizeSnd write setBufSizeSnd;

Size of sending buffer.

Public property socketProtocol: int read f_socketProtocol;

Specifies protocol number to be used with socket (IPPROTO_TCP/UDP, etc.)

Public property socketType: int read f_socketType;

Specifies socket type to be used with socket (SOCK_STREAM/DGRAM, etc.)

(c) 2012 Lake of Soft


Generated by PasDoc 0.12.1 on 2012-10-09