SDSU CS 596 Client-Server Programming
Java Networking

[To Lecture Notes Index]
San Diego State University -- This page last updated February 22, 1996

Contents of TITLE Lecture

  1. Java Networking
  2. TCP Networking
    1. Building a TCP connection
      1. java.net.Socket
      2. java.net.Socket methods
    2. Domain Name Service (DNS)
      1. Java interface to DNS
      2. Example use of InetAddress class
    3. Java interface to known services
  3. Trivial TCP client example
    1. Trivial TCP client example (continued)
  4. UDP Networking
    1. The java.net.DatagramPacket class
    2. The java.net.DatagramSocket class
    3. Example UDP client
    4. Example UDP client sample run
  5. Status so far
    1. Sorry... No such luck
  6. Major topics still to cover

Java Networking


Package java.net provides networking classes


Key classes are marked.

For TCP java.net.Socket

For UDP java.net.DatagramPacket and java.net.DatagramSocket


TCP Networking



Building a TCP connection


Standard steps in building a TCP connection and using it:


java.net.Socket


Constructors for java.net.Socket:


Exceptions thrown by constructors:



java.net.Socket methods


Methods implemented by java.net.Socket:


Most useful methods are getInputStream() and getOutputStream()


Domain Name Service (DNS)


Internet Protocol addresses are always numeric.

We generally don't use IP addresses:

Did you know that rohan's address is 130.191.143.100?

DNS is a service that translates between host names and IP addresses.

Example lookup programs (clients):


Example:

rohan%  host ender
ender.sdsu.edu          A       130.191.13.2
rohan%  host 130.191.13.2
Name: ender.sdsu.edu
Address: 130.191.13.2
rohan%

Java interface to DNS


The InetAddress class performs the lookups.

Methods:


Example use of InetAddress class

import java.net.InetAddress;

public class SimpleDNS
{
  public static void main(String args[])      
                     throws Exception
  {
      InetAddress address = 
         InetAddress.getByName(args[0]);
      byte IP[] = address.getAddress();
	
      for (int index = 0; index < IP.length;
                                     index++)
      {
         if (index > 0)
           System.out.print(".");
         System.out.print(((int)IP[index])& 0xff);
      }
    System.out.println();
  }
}

Use:

rohan% java SimpleDNS www.sdsu.edu
130.191.13.5
rohan%


Java interface to known services


Sorry, there is none...

Could easily write a class that reads /etc/services but that introduces platform dependencies.

Why?


Trivial TCP client example

import java.net.Socket;
import java.io.InputStream;

public class SimpleTelnet
{
  public static void main(String args[])
                 throws Exception
  {
    String	serverName = args[0];
    int	portNumber = 
              Integer.parseInt(args[1]);

    Socket	server = null;
    server = new Socket(serverName,
                         portNumber);

    InputStream	input = 
                  server.getInputStream();
    int		byteCount;
    byte		inputBuffer[] = new byte[1024];
	
    while ((byteCount = 
      input.read(inputBuffer, 0, 1024)) != -1)
    {
      String	stuff = new String(
                inputBuffer, 0, 0, byteCount);
          System.out.println("Received: " + 
                            stuff);
    }
  }
}

Trivial TCP client example (continued)


This client only reads... We'll test it with a service that only writes.

The daytime service is assigned to port 13.

moria% java SimpleTelnet moria.sdsu.edu 13
Received: Thu Feb 22 14:55:18 1996
moria%


UDP Networking


UDP == User Datagram Protocol

Features:



Steps in using datagrams:


The java.net.DatagramPacket class


Two constructors:


Methods:


The java.net.DatagramSocket class


Constructors:


If no port is specified in the constructor, a random port is picked.

Methods:


receive() will block until a packet is actually received.


Example UDP client

import java.net.*;
class DatagramTest
{
  public static void main(String[] args)
                             throws  Exception
  {
    DatagramSocket  socket;
    DatagramPacket  packet;
    InetAddress     address;
    byte[]          message = new byte[256];
    int             port = 13;

    //
    // Send empty request
    //
    socket = new DatagramSocket();
    address = InetAddress.getByName("moria.sdsu.edu");
    packet = new DatagramPacket(message, message.length,
                                address, port);
    socket.send(packet);

    //
    // Receive reply and print
    //
    packet = new DatagramPacket(message,
                                message.length);
    socket.receive(packet);
    String received = new String(packet.getData(), 0);
    System.out.println("Received: " + received);
    socket.close();
  }
}

Example UDP client sample run


rohan% java DatagramTest
Received: Mon Feb 26 23:07:06 1996

rohan%


If either the request packet or reply packet were to get dropped/lost, the program would hang. Why?


Possible solutions?


Status so far


With the information from the Network Services lecture and this one, we can now:






We're done... End of the semester!


Sorry... No such luck


We have only seen the tip of the iceberg.




















Major topics still to cover


In no particular order:



More Java networking: