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 *
Recall, a network connection between two machines is uniquely defined by the following:
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.
$
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) |
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);
}
}
}
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. |
Some useless trivia:
Main purposes of DNS are:
Why do we use DNS?
Some reasons why a DNS interface is generally non-trivial:
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. |
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 == User Datagram Protocol
Features:
Steps in using datagrams:
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(); } }
$ java UDPTest
$
Problems with this program?
What would happen?
Why?
Solutions?