brl_socket.md 4.2 KB


id: brl.socket title: BRL.Socket

sidebar_label: BRL.Socket

Functions

Function CreateUDPSocket:TSocket()

Create a UDP socket

The new socket is not bound to any local or remote address.

Returns

A new socket

Function CreateTCPSocket:TSocket()

Create a TCP socket

The new socket is not bound to any local or remote address.

Returns

A new socket

Function CloseSocket( socket:TSocket )

Close a socket

All sockets should eventually be closed. Once closed, a socket can no longer be used.

Function BindSocket( socket:TSocket, localPort, family:Int = AF_INET_)

Bind a socket to a local port

If localPort is 0, a new local port will be allocated. If localPort is not 0, BindSocket will fail if there is already an application bound to localPort.

Returns

True if successful, otherwise false

Function ConnectSocket( socket:TSocket, AddrInfo:TAddrInfo )

Connect a socket to a remote ip and port

For both UDP and TCP sockets, ConnectSocket will fail if the specified ip address could not be reached.

In the case of TCP sockets, ConnectSocket will also fail if there is no application listening at the remote port.

Returns

True if successful, otherwise false

Function SocketListen( socket:TSocket,backlog=0 )

Start listening at a socket

The specified socket must be a TCP socket, and must already be bound to a local port.

Function SocketAccept:TSocket( socket:TSocket,timeout=0 )

Accept new connections on a listening socket

The specified socket must be a TCP socket, and must be listening.

Returns

A new socket, or Null if no connection was made in the specified timeout

Function SocketConnected( socket:TSocket )

Get socket connection status

SocketConnected allows you to determine if a TCP connection is still alive or has been remotely closed.

SocketConnected should only be used with TCP sockets that have already connected via ConnectSocket or SocketAccept.

Returns

True if socket is connected

Function SocketReadAvail( socket:TSocket )

Get number of bytes available for reading from a socket

Returns

Number of bytes that may be read without causing the socket to block

Function SocketLocalIP:String( socket:TSocket )

Get local ip of a socket

Function SocketLocalPort( socket:TSocket )

Get local port of a socket

Function SocketRemoteIP:String( socket:TSocket )

Get remote ip of a socket

Function SocketRemotePort( socket:TSocket )

Get remote port of a socket

Function DottedIP$( ip:Int )

Convert an ip address to a dotted string

Returns

Dotted string version of ip address

Function DottedIPToInt:Int(addr:String)

Converts a dotted IPv4 string to an ip address.

Returns

An integer version of an ip address.

Function InetPton:Int(family:Int, src:String, dst:Byte Ptr)

Converts an IP address string into a binary representation.

For AFINET, dst should be an Int or 32-bit (4 bytes) in size. For AFINET6, dst should be 128-bits (16 bytes) in size.

Function HostIp:String( HostName$, index:Int=0, family:Int = AF_UNSPEC_ )

Convert a host name to an ip address

Returns

Host ip address, or 0 if host not found

Function HostIps:String[]( HostName$, family:Int = AF_UNSPEC_ )

Get all ip addresses for a host name

Returns

Array of host ips, or Null if host not found

Function HostName$( HostIp:String, family:Int = AF_UNSPEC_ )

Convert a host ip address to a name

Returns

Name of host, or Null if host not found

Function AddrInfo:TAddrInfo[](host:String, service:String, hints:TAddrInfo)

Returns an array of TAddrInfo objects.