Next: , Previous: , Up: Classes   [Index]


TCPIPNetworkStream

TCPIPNetworkStream Class

TCPIPNetworkStream class contains methods for sockets that are used for both writing and reading.

Methods that are specific to programs that read from sockets are defined in TCPIPNetworkStreamReader class, See TCPIPNetworkStreamReader, and methods that work with programs that write to network connections are defined in TCPIPNetworkStreamWriter class See TCPIPNetworkStreamWriter.

Here is a program that writes data to a network connection.


static char *msg = "First connect.\n";

int main (int argc, char **argv) {
  SystemErrnoException new ex;
  TCPIPNetworkStreamWriter new client;

  if (argc != 2) {
    printf ("Usage: ip6writer <server_hostname>\n");
  }
  client openOn argv[1];
  client writeText msg;
  if (ex pending) {
    ex handle;
  }

  client closeSock;
}

And here is the corresponding program that reads the data from the network connection and displays it.


int main () {
  TCPIPNetworkStreamReader new server;
  Exception new ex;
  String new output;
  int newsocket;

  server openOn;

  newsocket = server acceptSock;

  if (newsocket > 0) {
    output = server readText newsocket;
    if (ex pending) {
      ex handle;
    }
    printf ("%s\n", output);
    server closeSock newsocket;
  } else if (ex pending) {
    ex handle;
  } else {
    printf ("Connection timed out.\n");
  }
  
  server closeSock;
}

Instance Variables

sock

An Integer that contains the file handle number of the socket created when a network connection is opened.

Instance Methods

addrInfo (String hostName, String canonicalNameOut, List addrsOut

Performs a lookup of hostName’s IP addresses. If the lookup is successful, returns the host’s canonical name in canonNameOut and each of the host’s addresses as a string in addrsOut. The method’s return value is an Integer with the number of addresses found.

If the lookup causes an error, the method raises an Exception (not a SystemErrnoException) and returns 0.

closeSock (void)
closeSock (Integer sock_fh)

With no arguments, closes the receiver’s socket. With one argument, closes the socket given as the argument. If closing a socket causes an error, the method raises a SystemErrnoException.

createSocketBasic (void)

Creates a socket with the domain AF_INET and the protocol SOCK_STREAM, and sets the receiver’s sock instance variable to the new socket’s file handle number, an Integer, and then returns the socket’s file handle number. If not successful, the method raises a SystemErrnoException and returns 0.

hostToAddress (String hostname)

Given the name of a network host as the argument, the method returns a String containing the host’s dotted quad Internet address.

If the hostname lookup doesn’t return any results, the method raises an Exception (not a SystemErrnoException) and returns an empty String.


#include <stdio.h>   /* contains printf prototype */

int main () {
  TCPIPNetworkStream new net;
  String new address;

  address = net hostToAddress "MyHostName"; /* Substitute your host's name. */

  printf ("%s\n", address);
}

readText (void)
readText (Integer sock_fh)

Reads a socket’s input and returns the input as a String object. With no arguments, the method uses the receiver’s socket file handle, which is normally assigned by createSocketBasic, above.

If a socket file handle is given as the argument, then the method performs the read on that handle. This is useful when performing reads after a call to acceptSock or a similar method. See TCPIPNetworkStreamReader.

If an error occurs while reading, the methods raise a SystemErrnoException.

readVec (Integer sock_fh, Vector data_vec_out)

Reads binary data from the socket given as the first argument and returns the data in the Vector object give as the second argument.

The method raise a SystemErrnoException if an error occurs while reading.

This example is a simple server that receives image data and writes it to a file.


int main () {
  TCPIPNetworkStreamReader new server;
  SystemErrnoException new ex;
  Vector new output;
  WriteFileStream new writeF;
  int newsocket;
  char *socket_out;

  server openOn;

  newsocket = server acceptSock;  /* INADDR_ANY */

  if (newsocket > 0) {
    server readVec newsocket, output;
    if (ex pending) {
      ex handle;
    }
    server closeSock newsocket;
  } else if (ex pending) {
    ex handle;
  } else {
    printf ("Connection timed out.\n");
  }

  server closeSock;

  writeF openOn "image-copy.jpeg";
  writeF writeVec output;
  writeF closeStream;
}

Here is the corresponding client program that transmits the image data.


int main () {
  SystemErrnoException new ex;
  TCPIPNetworkStreamWriter new client;
  ReadFileStream new readF;
  Vector new photo;
  LongInteger new imageSize;

  readF openOn "image.jpeg";

  readF statStream;
  imageSize = readF streamSize;

  photo = readF readVec imageSize;

  readF closeStream;

  client openOn "127.0.0.1"; /* Edit with the reciever's actual network
                                address. */

  client writeVec photo;
  if (ex pending) {
    ex handle;
  }

  client closeSock;
}
readText (String text)

Writes the text given as the argument to the reciever’s socket. If the number of bytes actually written isn’t equal to the length of text, then the method raises a SystemErrnoException.

readText (Vector data)

Writes the data contained in the argument to the receiver’s socket. The method raises an Exception if the argument is not a Vector, or a SystemErrnoException if an error occurs while writing.

Examples of client and server programs which handle binary data are given in the entry for readVec, above.


Next: , Previous: , Up: Classes   [Index]