Binding Sockets |
A socket, once created, is bound to a unique port number on the local host. After the socket is bound, it can be accessed by another process by using its address. There are several ways to bind a socket to a unique port.
os_socket_address( port )
. The socket is bound to the specified port on the local host. If the socket
is an os_tcp_connection_server and the host is
multihomed (has multiple IP addresses), the socket accepts connections to
the specified port via any of its IP addresses.os_socket_address( ip_address, port ) .
The socket is bound to the specified port on the host identified by ip_address
. Use this method only if you want to bind the socket to one of many IP
addresses on a multihomed host. Once a socket is constructed, use connect_to()
to connect it to another socket on either a local or a remote host. connect_to()
takes the address of the destination socket as its single argument.
For UDP sockets, connect_to()
records the default address used for sending datagrams without implying the
connection is reliable or the endpoint exists.
Once a socket is connected, write
or read blocks of memory with the write() and
read() functions. These functions act the same as the lowest level write()
and read() system calls, except these functions
generate errors consistent with other Recursion Software components. Both of
these operations return the number of bytes actually transferred.
All sockets automatically close
when they are destroyed. This behavior is convenient in most cases, especially
if you only want to use an I/O object in a certain scope. You can disable
automatic socket closing by executing auto_close( false )
before the object is out of scope.
The socket examples in the following sections use many of these features. Each example is split into a client and a server executable. To run these executables, use two windows. In one window, execute the server; in the other window, execute the client.
You can modify the client side of
these examples to connect to the server when it is running on another machine.
To do this, modify the connect_to() in the client
to include the ip_address of the server's host.
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.