📄 blcksock.pas
字号:
created. If Value is resolved as IPv4 IP, then is created IPv4 socket. If
value is resolved as IPv6 address, then is created IPv6 socket.}
procedure CreateSocketByName(const Value: String);
{:Destroy socket in use. This method is also automatically called from
object destructor.}
procedure CloseSocket; virtual;
{:Abort any work on Socket and destroy them.}
procedure AbortSocket; virtual;
{:Connects socket to local IP address and PORT. IP address may be numeric or
symbolic ('192.168.74.50', 'cosi.nekde.cz', 'ff08::1'). The same for PORT
- it may be number or mnemonic port ('23', 'telnet').
If port value is '0', system chooses itself and conects unused port in the
range 1024 to 4096 (this depending by operating system!). Structure
LocalSin is filled after calling this method.
Note: If you call this on non-created socket, then socket is created
automaticly.
Warning: when you call : Bind('0.0.0.0','0'); then is nothing done! In this
case is used implicit system bind instead.}
procedure Bind(IP, Port: string);
{:Connects socket to remote IP address and PORT. The same rules as with
@link(BIND) method are valid. The only exception is that PORT with 0 value
will not be connected!
Structures LocalSin and RemoteSin will be filled with valid values.
When you call this on non-created socket, then socket is created
automaticly. Type of created socket is by @link(Family) property. If is
used SF_IP4, then is created socket for IPv4. If is used SF_IP6, then is
created socket for IPv6. When you have family on SF_Any (default!), then
type of created socket is determined by address resolving of destination
address. (Not work properly on prilimitary winsock IPv6 support!)}
procedure Connect(IP, Port: string); virtual;
{:Sends data of LENGTH from BUFFER address via connected socket. System
automatically splits data to packets.}
function SendBuffer(Buffer: Tmemory; Length: Integer): Integer; virtual;
{:One data BYTE is sent via connected socket.}
procedure SendByte(Data: Byte); virtual;
{:Send data string via connected socket. Any terminator is not added! If you
need send true string with CR-LF termination, you must add CR-LF characters
to sended string! Because any termination is not added automaticly, you can
use this function for sending any binary data in binary string.}
procedure SendString(Data: AnsiString); virtual;
{:Send integer as four bytes to socket.}
procedure SendInteger(Data: integer); virtual;
{:Send data as one block to socket. Each block begin with 4 bytes with
length of data in block. This 4 bytes is added automaticly by this
function.}
procedure SendBlock(const Data: AnsiString); virtual;
{:Send data from stream to socket.}
procedure SendStreamRaw(const Stream: TStream); virtual;
{:Send content of stream to socket. It using @link(SendBlock) method}
procedure SendStream(const Stream: TStream); virtual;
{:Send content of stream to socket. It using @link(SendBlock) method and
this is compatible with streams in Indy library.}
procedure SendStreamIndy(const Stream: TStream); virtual;
{:Note: This is low-level receive function. You must be sure if data is
waiting for read before call this function for avoid deadlock!
Waits until allocated buffer is filled by received data. Returns number of
data received, which equals to LENGTH value under normal operation. If it
is not equal the communication channel is possibly broken.
On stream oriented sockets if is received 0 bytes, it mean 'socket is
closed!"
On datagram socket is readed first waiting datagram.}
function RecvBuffer(Buffer: TMemory; Length: Integer): Integer; virtual;
{:Note: This is high-level receive function. It using internal
@link(LineBuffer) and you can combine this function freely with other
high-level functions!
Method waits until data is received. If no data is received within TIMEOUT
(in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT. Methods
serves for reading any size of data (i.e. one megabyte...). This method is
preffered for reading from stream sockets (like TCP).}
function RecvBufferEx(Buffer: Tmemory; Len: Integer;
Timeout: Integer): Integer; virtual;
{:Similar to @link(RecvBufferEx), but readed data is stored in binary
string, not in memory buffer.}
function RecvBufferStr(Length: Integer; Timeout: Integer): AnsiString; virtual;
{:Note: This is high-level receive function. It using internal
@link(LineBuffer) and you can combine this function freely with other
high-level functions.
Waits until one data byte is received which is also returned as function
result. If no data is received within TIMEOUT (in milliseconds)period,
@link(LastError) is set to WSAETIMEDOUT and result have value 0.}
function RecvByte(Timeout: Integer): Byte; virtual;
{:Note: This is high-level receive function. It using internal
@link(LineBuffer) and you can combine this function freely with other
high-level functions.
Waits until one four bytes are received and return it as one Ineger Value.
If no data is received within TIMEOUT (in milliseconds)period,
@link(LastError) is set to WSAETIMEDOUT and result have value 0.}
function RecvInteger(Timeout: Integer): Integer; virtual;
{:Note: This is high-level receive function. It using internal
@link(LineBuffer) and you can combine this function freely with other
high-level functions.
Method waits until data string is received. This string is terminated by
CR-LF characters. The resulting string is returned without this termination
(CR-LF)! If @link(ConvertLineEnd) is used, then CR-LF sequence may not be
exactly CR-LF. See @link(ConvertLineEnd) description. If no data is
received within TIMEOUT (in milliseconds) period, @link(LastError) is set
to WSAETIMEDOUT. You may also specify maximum length of reading data by
@link(MaxLineLength) property.}
function RecvString(Timeout: Integer): AnsiString; virtual;
{:Note: This is high-level receive function. It using internal
@link(LineBuffer) and you can combine this function freely with other
high-level functions.
Method waits until data string is received. This string is terminated by
Terminator string. The resulting string is returned without this
termination. If no data is received within TIMEOUT (in milliseconds)
period, @link(LastError) is set to WSAETIMEDOUT. You may also specify
maximum length of reading data by @link(MaxLineLength) property.}
function RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString; virtual;
{:Note: This is high-level receive function. It using internal
@link(LineBuffer) and you can combine this function freely with other
high-level functions.
Method reads all data waiting for read. If no data is received within
TIMEOUT (in milliseconds) period, @link(LastError) is set to WSAETIMEDOUT.
Methods serves for reading unknown size of data. Because before call this
function you don't know size of received data, returned data is stored in
dynamic size binary string. This method is preffered for reading from
stream sockets (like TCP). It is very goot for receiving datagrams too!
(UDP protocol)}
function RecvPacket(Timeout: Integer): AnsiString; virtual;
{:Read one block of data from socket. Each block begin with 4 bytes with
length of data in block. This function read first 4 bytes for get lenght,
then it wait for reported count of bytes.}
function RecvBlock(Timeout: Integer): AnsiString; virtual;
{:Read all data from socket to stream until socket is closed (or any error
occured.)}
procedure RecvStreamRaw(const Stream: TStream; Timeout: Integer); virtual;
{:Read requested count of bytes from socket to stream.}
procedure RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: Integer);
{:Receive data to stream. It using @link(RecvBlock) method.}
procedure RecvStream(const Stream: TStream; Timeout: Integer); virtual;
{:Receive data to stream. This function is compatible with similar function
in Indy library. It using @link(RecvBlock) method.}
procedure RecvStreamIndy(const Stream: TStream; Timeout: Integer); virtual;
{:Same as @link(RecvBuffer), but readed data stays in system input buffer.
Warning: this function not respect data in @link(LineBuffer)! Is not
recommended to use this function!}
function PeekBuffer(Buffer: TMemory; Length: Integer): Integer; virtual;
{:Same as @link(RecvByte), but readed data stays in input system buffer.
Warning: this function not respect data in @link(LineBuffer)! Is not
recommended to use this function!}
function PeekByte(Timeout: Integer): Byte; virtual;
{:On stream sockets it returns number of received bytes waiting for picking.
0 is returned when there is no such data. On datagram socket it returns
length of the first waiting datagram. Returns 0 if no datagram is waiting.}
function WaitingData: Integer; virtual;
{:Same as @link(WaitingData), but if exists some of data in @link(Linebuffer),
return their length instead.}
function WaitingDataEx: Integer;
{:Clear all waiting data for read from buffers.}
procedure Purge;
{:Sets linger. Enabled linger means that the system waits another LINGER
(in milliseconds) time for delivery of sent data. This function is only for
stream type of socket! (TCP)}
procedure SetLinger(Enable: Boolean; Linger: Integer);
{:Actualize values in @link(LocalSin).}
procedure GetSinLocal;
{:Actualize values in @link(RemoteSin).}
procedure GetSinRemote;
{:Actualize values in @link(LocalSin) and @link(RemoteSin).}
procedure GetSins;
{:If you "manually" call Socket API functions, forward their return code as
parameter to this function, which evaluates it, eventually calls
GetLastError and found error code returns and stores to @link(LastError).}
function SockCheck(SockResult: Integer): Integer;
{:If @link(LastError) contains some error code and @link(RaiseExcept)
property is @true, raise adequate exception.}
procedure ExceptCheck;
{:Returns local computer name as numerical or symbolic value. It try get
fully qualified domain name. Name is returned in the format acceptable by
functions demanding IP as input parameter.}
function LocalName: string;
{:Try resolve name to all possible IP address. i.e. If you pass as name
result of @link(LocalName) method, you get all IP addresses used by local
system.}
procedure ResolveNameToIP(Name: string; const IPList: TStrings);
{:Try resolve name to primary IP address. i.e. If you pass as name result of
@link(LocalName) method, you get primary IP addresses used by local system.}
function ResolveName(Name: string): string;
{:Try resolve IP to their primary domain name. If IP not have domain name,
then is returned original IP.}
function ResolveIPToName(IP: string): string;
{:Try resolve symbolic port name to port number. (i.e. 'Echo' to 8)}
function ResolvePort(Port: string): Word;
{:Set information about remote side socket. It is good for seting remote
side for sending UDP packet, etc.}
procedure SetRemoteSin(IP, Port: string);
{:Picks IP socket address from @link(LocalSin).}
function GetLocalSinIP: string; virtual;
{:Picks IP socket address from @link(RemoteSin).}
function GetRemoteSinIP: string; virtual;
{:Picks socket PORT number from @link(LocalSin).}
function GetLocalSinPort: Integer; virtual;
{:Picks socket PORT number from @link(RemoteSin).}
function GetRemoteSinPort: Integer; virtual;
{:Return @TRUE, if you can read any data from socket or is incoming
connection on TCP based socket. Status is tested for time Timeout (in
milliseconds). If value in Timeout is 0, status is only tested and
continue. If value in Timeout is -1, run is breaked and waiting for read
data maybe forever.
This function is need only on special cases, when you need use
@link(RecvBuffer) function directly!}
function CanRead(Timeout: Integer): Boolean; virtual;
{:Same as @link(CanRead), but additionally return @TRUE if is some data in
@link(LineBuffer).}
function CanReadEx(Timeout: Integer): Boolean; virtual;
{:Return @TRUE, if you can to socket write any data (not full sending
buffer). Status is tested for time Timeout (in milliseconds). If value in
Timeout is 0, status is only tested and continue. If value in Timeout is
-1, run is breaked and waiting for write data maybe forever.
This function is need only on special cases!}
function CanWrite(Timeout: Integer): Boolean; virtual;
{:Same as @link(SendBuffer), but send datagram to address from
@link(RemoteSin). Usefull for sending reply to datagram received by
function @link(RecvBufferFrom).}
function SendBufferTo(Buffer: TMemory; Length: Integer): Integer; virtual;
{:Note: This is low-lever receive function. You must be sure if data is
waiting for read before call this function for avoid deadlock!
Receives first waiting datagram to allocated buffer. If there is no waiting
one, then waits until one comes. Returns length of datagram stored in
BUFFER. If length exceeds buffer datagram is truncated. After this
@link(RemoteSin) structure contains information about sender of UDP packet.}
function RecvBufferFrom(Buffer: TMemory; Length: Integer): Integer; virtual;
{$IFNDEF CIL}
{:This function is for check for incoming data on set of sockets. Whitch
sockets is checked is decribed by SocketList Tlist with TBlockSocket
objects. TList may have maximal number of objects defined by FD_SETSIZE
constant. Return @TRUE, if you can from some socket read any data or is
incoming connection on TCP based socket. Status is tested for time Timeout
(in milliseconds). If value in Timeout is 0, status is only tested and
continue. If value in Timeout is -1, run is breaked and waiting for read
data maybe forever. If is returned @TRUE, CanReadList TList is filled by all
TBlockSocket objects what waiting for read.}
function GroupCanRead(const SocketList: TList; Timeout: Integer;
const CanReadList: TList): Boolean;
{$ENDIF}
{:By this method you may turn address reuse mode for local @link(bind). It
is good specially for UDP protocol. Using this with TCP protocol is
hazardous!}
procedure EnableReuse(Value: Boolean);
{:Try set timeout for all sending and receiving operations, if socket
provider can do it. (It not supported by all socket providers!)}
procedure SetTimeout(Timeout: Integer);
{:Try set timeout for all sending operations, if socket provider can do it.
(It not supported by all socket providers!)}
procedure SetSendTimeout(Timeout: Integer);
{:Try set timeout for all receiving operations, if socket provider can do
it. (It not supported by all socket providers!)}
procedure SetRecvTimeout(Timeout: Integer);
{:Convert IPv6 address from their string form to binary. This function
working only on systems with IPv6 support!}
function StrToIP6(const value: string): TSockAddrIn6;
{:Convert IPv6 address from binary to string form. This function working
only on systems with IPv6 support!}
function IP6ToStr(const value: TSockAddrIn6): string;
{:Return value of socket type.}
function GetSocketType: integer; Virtual;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -