basic.%bind

C Function/BASIC Program, Releases: AP/Unix

Binds a name to a socket.

Syntax

code=%bind( socket, addr.family, address, port )

Description

binds a socket to a named resource. In case of a network, the resource would be an access point into the system (see the example below).

To compile successfully, the statement 'cfunction socket.builtin' must be included in the source code.

'socket' Is the file descriptor of the local socket returned by a previous call to the Pick/BASIC C function '%socket'.

'addr.family' Specifies the addressing scheme used by the protocol. This field must match the address family used when creating the socket. Valid values are defined in the include: 'dm,bp,unix.h socket.h'.

'address' Legal values are defined in the include: 'dm,bp,unix.h socket.h'.

'port' Port number on the local host. The legal value for this field depends on the protocol. On TCP/IP, for example, valid port number are from 1024 to 32767. This field may not be applicable for all protocols.

Upon successful completion, a value of 0 is returned in 'code'. In case of an error, a value of -1 is returned and the (Pick/BASIC) function 'SYSTEM(0)' is set to the value of 'errno'.

Example

cfunction socket.builtin
include dm,bp,unix.h socket.h

* Create a socket
fd=%socket( AF$INET, SOCKET$STREAM, 0 )

* Bind the socket to a local Ethernet port.
* Use default address.
if %bind( fd, AF$INET, INADDR$ANY, 1024 )<0 then
crt 'bind failed'; stop
end

* Wait for incoming connection
%listen( fd, 1 )

* Accept a connectionn
addr=0; port=0
fd=%accept( socket, &addr, &port )

* Read data from the data link
%read( fd, buffer, 1024 )

See Also

Command Name Type Description
basic.%listen C Function Listens for incoming connections and limits the backlog of incoming connections.
basic.%gethostid C Function Gets the unique identifier of the current host.
basic.%socket C Function Creates a socket.
basic.%accept C Function Accept a connection on a socket.
basic.%read C Function Reads from a Unix file.

User Comments

What do you think?

Share your experience or ask a question by using the form below.

Login to leave your comments.