Next: UNIXNetworkStreamWriter, Previous: UNIXNetworkStream, Up: Classes [Index]
UNIXNetworkStreamReader
ClassUNIXNetworkStreamReader
objects receive data using
UNIX domain sockets on a single machine. The class defines
methods to create a connection and check the connection
for incoming data, and read any data for the application.
Here are slightly abreviated versions of the sockread.c
and
sockwrite.c
example programs. This is the source code of
sockread.c
.
int main () { UNIXNetworkStreamReader new reader; SystemErrnoException new ex; FileStream new f; String new sockName; String new data; sockName = "testsocket"; reader makeSocketPath sockName; printf ("reader socket: %s\n", reader socketPath); /* Delete a socket from a previous connection if necessary. */ if (f exists reader socketPath) { f deleteFile reader socketPath; } reader open; if (ex pending) { ex handle; unlink (reader socketPath); return -1; } while (1) { data = reader sockRead; if (data length > 0) { printf ("%s\n", data); } if (ex pending) { ex handle; break; } usleep (1000); } return 0; }
And here is sockwrite.c
. For details about its
operation, refer to the UNIXNetworkStreamWriter
section.
See UNIXNetworkStreamWriter.
int main (int argc, char **argv) { UNIXNetworkStreamWriter new writer; SystemErrnoException new ex; String new sockName; String new data; sockName = "testsocket"; writer makeSocketPath sockName; printf ("writer socket: %s\n", writer socketPath); writer open; if (ex pending) { ex handle; } writer sockWrite argv[1]; if (ex pending) { ex handle; } exit (0); }
The programs communicate if started from different shells used by different virtual terminals or different windows on a graphical desktop, or if the reader program is started in the background and the writer started in the foreground.
charsRead
An Integer
that contains the number of characters read by the
last reception of data from a connection, or zero if there is no data
waiting. Refer to the method sockRead
, below.
open
(void
)
Creates a socket to read data from the socket name by the
receiver’s socketPath
instance variable, which is
defined in UNIXNetworkStream
class. See UNIXNetworkStream.
The method returns the file number of the socket, or -1 if there
is an error, and raises a SystemErrnoException
if an error
occurred in the network library routines.
openOn
(String
socketpath)
Creates a new reader socket and binds it to socketpath. Sets
the receiver’s socketPath
instance variable to the argument.
Returns an Integer
with the file number of the newly created
socket.
sockRead
(void
)
Returns a String
containing data received from the socket
previously created by the open
method, above, and sets
the charsRead
instance variable to the number
of characters read.
If no data is waiting, the method returns an empty string and sets the
receiver’s charsRead
instance variable to 0.
If an error occurs during one of the system calls, the method
raises a SystemErrnoException
.
Next: UNIXNetworkStreamWriter, Previous: UNIXNetworkStream, Up: Classes [Index]