Java Networking Programming Overview


Sockets

  • When creating a socket, you pass in the hostname and port number.
  • The java.net.Socket constructor does the gethostbyname() and the socket() system call, sets up the server's sockaddr_in structure, and executes the connect() call.
  • All you have to do is catch the errors, which are subclassed from the familiar IOException.
  • You can both read and write on the same Socket.

Reading / Writing Data

  • Reading / Writing on the same Socket

    • Java allows to both read and write on the same Socket.
    • This requirement, copying data in both directions at more or less the same time - from the keyboard to the remote program, and from the remote program to the screen - there are two approaches.
    • Some I/O libraries in C have a function called poll() or select() that allows you to examine a number of files to see which ones are ready for reading or writing.
    • Java does not support this model. The other model, which works on most platforms and is the norm in Java, is to use two threads, one to handle the data transfer in each direction.
    • The class Pipe encapsulates on threads and the code for copying data in one direction; two instances are used, one to drive each direction of transfer independently of the other.

  • Reading / Writing Textual Data

    • Construct a BufferedReader or PrintWriter object.
    • Both objects can be created using the socket's getInputStream() or getOutputStream() methods.
    • There is no method to fetch a Reader or Writer, partly because some network services are limited to ASCII, but mainly because the Socket class was decided on before there were Reader and Writer classes. You can always create a Reader from an InputStream or a Writer from an OutputStream using the conversion classes. The paradigm for the two most common forms is:
        BufferedReader is = new BufferedReader (
            new InputStreamReader(sock.getInputStream()));
      
        PrintWriter os = new PrintWriter(sock.getOutputStream(), true);
  • Reading / Writing Binary Data

    • Construct a DataInputStream or DataOutputStream object.
    • Both objects can be created using the socket's getInputStream() or getOutputStream() methods.
    • The simplest paradigm is:
        DataInputStream is = new DataInputStream(sock.getInputStream());
        DataOutputStream os = new DataOutputStream(sock.getOutputStream());
    • If the volumne of data might be large, insert a bufferd stream for efficiency. The paradigm is:
        DataInputStream is = new DataInputStream(
            new BufferedInputStream(sock.getInputStream()));
      
        DataOutputStream os = new DataOutputStream(
            new BufferedOutputStream(sock.getOutputStream()));

Server Sockets

InetAddress

  • The InetAddress is mainly used for looking up a host's address name or number.
  • It is also possible to obtain the address at the other end of a connection.
  • The InetAddress object represents the Internet address of a given computer or host.
  • There are no public constructors, you can obtain an InetAddress object by calling the static byName method, passing in either a hostname like wwww.iDevelopment.info or a network address as a String, like "12.4.234.11".