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.

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 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 connect_to() in the client to include the ip_address of the server's host.


Copyright ©1994,1995,1996 ObjectSpace, Inc.
All Rights Reserved - For use by licensed users only.