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.
). The socket is bound to the specified port on the local host. If the socket is an
os_tcp_connection_serverand 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
to connect it to another socket on either a local or a remote host.
takes the address of the destination socket as its single argument.
For UDP sockets,
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
functions. These functions act the same as the lowest level
system calls, except these functions generate errors consistent with other ObjectSpace 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
in the client to include the
of the server's host.
Copyright ©1994,1995,1996 ObjectSpace, Inc.
All Rights Reserved - For use by licensed users only.