Package java.net provides networking classes
Key classes are marked.
For TCP java.net.Socket
For UDP java.net.DatagramPacket and java.net.DatagramSocket
Standard steps in building a TCP connection and using it:
Constructors for java.net.Socket:
Exceptions thrown by constructors:
Methods implemented by java.net.Socket:
Most useful methods are getInputStream() and getOutputStream()
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%
The InetAddress class performs the lookups.
Methods:
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%
Sorry, there is none...
Could easily write a class that reads /etc/services but that introduces
platform dependencies.
Why?
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); } } }
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 == User Datagram Protocol
Features:
Steps in using datagrams:
Two constructors:
Methods:
Constructors:
If no port is specified in the constructor, a random port is picked.
Methods:
receive() will block until a packet is actually received.
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(); } }
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?
With the information from the Network Services lecture and this one, we
can now:
We're done... End of the semester!
We have only seen the tip of the iceberg.
In no particular order:
More Java networking: