SDSU CS 596: Client-Server Programming
Spring Semester, 1997
Doc 4 Java Networking

To Lecture Notes Index
San Diego State University -- This page last updated Feb 6, 1997
----------

Java Networking

Java Networking *

Building a Connection *

IP Connecting Parts *

Simple Java Client *

Socket – Java IP networking *

Socket – Important Methods *

Domain Name Service (DNS) *

DNS Through java.net.InetAddress *

Example use of InetAddress class *

UDP Networking *

Example UDP Client *

Example UDP Client Sample Run *

 


Building a Connection

Recall, a network connection between two machines is uniquely defined by the following:

  1. The protocol (TCP or UDP)
  2. The address of the local machine (the one building the connection)
  3. The port number used by the local machine
  4. The address of the remote machine (the one we are connecting to)
  5. The port number used on the remote machine

 

Identify the parts:

$ telnet saturn.sdsu.edu 13
trying 130.191.229.1...
Connected to saturn.sdsu.edu.
Escape character is '^]'.
Tue Feb 22 15:13:56 2057
Connection closed by foreign host.
$

 

 


IP Connecting Parts

Information a client normally needs:

Information

Data from example

1.

The protocol

TCP (implied by the telnet program)

2.

The name of the server machine

Saturn.sdsu.edu

3.

The port number used on the remote machine

13

 

The missing parts:

Information

Where does it come from?

4.

The address of the local machine

This is automatically retrieved from the operating system.

5.

The port number used on the local machine

This is automatically generated for the program by the operating system. Effectively, the OS assigns a random port number within a known range of ports. (1024 - 5000)

 


Simple Java Client

import java.net.*;
import java.io.*;

public class GetTime
   {
   public static void main(String[] args)
      {
      Socket   client = null;
      try
         {
         client = new Socket("saturn.sdsu.edu", 13);
         }
      catch (UnknownHostException hostError)
         {
         System.err.println(hostError.getMessage());
         System.exit(1);
         }
      catch (IOException genericError)
         {
         System.err.println(genericError.getMessage());
         System.exit(1);
         }
      try
         {
         DataInputStream in =
                 new DataInputStream(client.getInputStream());
         String   inputLine;
         while ((inputLine = in.readLine()) != null)
            {
            System.out.println("Received: " + inputLine);
            }
         }
      catch (IOException IOError)
         {
         System.err.println(IOError.getMessage());
         System.exit(1);
         }
      }
   }

Socket – Java IP networking

java.net.Socket

 Several constructors:

 The constructor builds the connection

Exceptions throws by the constructors:

Exception

Reason it is raised

UnknownHostException

The specified host is not valid or cannot be looked up

net.SocketException

Something went wrong while attempting to build a connection. Most common cause is "Connection refused" which means there is no service for the specified port

IOException

Catchall. The java.net.*Exception classes are all derived from this.


 

Socket – Important Methods

close()
Terminates an existing connection.
getInputStream() and getOutputStream()
These provide access to IO through the connection. NOTE, network IO is always done in packets. This may produce strange results. Solution: use the Buffered IO classes.
getLocalPort()
#5 of the missing data!

 

 

 

 


Domain Name Service (DNS)

Some useless trivia:

 

Main purposes of DNS are:

 

Why do we use DNS?

 

Some reasons why a DNS interface is generally non-trivial:

 


DNS Through java.net.InetAddress

There are no public constructors

An InetAddress object can only be created by methods in the java.net package

Basic methods:

String getHostName()

Returns the name of the machine identified by the InetAddress.

byte[] getAddress()

Returns an array of four bytes with the IP address.

 

 Static methods used to access the DNS:

InetAddress getByName(String)

Lookup the address of a machine.

InetAddress getLocalHost()

#4 of the missing data!

InetAddress[] getAllByName(String)

Lookup all addresses for a machine.

 

 


Example use of InetAddress class

import java.net.InetAddress;

public class SimpleDNS
   {

   public static void main(String[] args)
      {
      try
         {
         InetAddress address =
            InetAddress.getByName(args[0]);
         byte ip[] = address.getAddress();

         for (int octet=0; octet < ip.length; octet++)
            {
            System.out.print(((int)ip[octet]) & 0xff);
            }
         System.out.println();
         }
      catch (IOException DNSerror)
         {
            System.err.println(DNSerror.getMessage());
            System.exit(1);
         }
      }
   }
}

 

$ java SimpleDNS www.sdsu.edu

130.191.13.5.

$


UDP Networking

UDP == User Datagram Protocol

Features:

 

 

Steps in using datagrams:

 

 


Example UDP Client

import java.net.*;

public class UDPTest
   {
   public static void main(String[] args)
                            throws Exception
      {
      DatagramSocket  socket;
      DatagramPAcket  packet;
      InetAddress     address;
      byte[]          message = new byte[256];

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

      //
      // Receive reply and display on screen
      //
      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


$ java UDPTest

Received: Mon Feb 06 15:55:35 1997

$

 

Problems with this program?

 

What would happen?

 

Why?

 

Solutions?

 

----------