Professional Documents
Culture Documents
Objectives:
Appreciate low-level network communication using sockets Demonstrate client-server architecture through socket programming Fully understand the difference between datagram communication and stream communication Learn the Java classes that facilitate socket communication
Concepts
In order for us to write meaningful network-based application, we need to have a firm grasp of the factors involved in implementing these applications and the sound knowledge of the fundamental network programming models.
CLIENT-SERVER ARCHITECTURE
request
Basically, network-based application systems consist of a server, a client and a media for communication. The server machine hosts an application that offers requested services from clients. The client machine runs a program that makes request for services. The setup shown above uses the networking services provided by the transport layer of the TCP/IP protocol stack. The most widely used programming interfaces for these protocols are sockets. There are two protocols used for communication datagram and stream communication protocol. The datagram protocol as known as User Datagram Protocol (UDP) is a connectionless protocol. It sends independent packets of data, called datagrams. The protocol does not guarantee you the arrival and sequencing of the packets sent. During the communication, you need to send the local socket descriptor and the receiving sockets address. The stream communication is also known as the Transmission Control Protocol. It is a connection-oriented protocol. A connection must be established between two sockets prior to communication. While the server socket listens for connection request, the other (client socket) asks for
a connection. Once the sockets have been connected, they can be used to transmit data in both directions.
Classes :
o o o o o ContentHandler DatagramPacket DatagramSocket DatagramSocketImpl HttpURLConnection
o o o o o o o o o
Interfaces
o o o o ContentHandlerFactory FileNameMap SocketImplFactory URLStreamHandlerFactory
Exceptions
o o o o o o o BindException ConnectionException MalformedURLException NoRouteToHostException ProtocolException SocketException UnknownHostException
4. Perform communication with client //Receive from client String line = is.readLine( ); //Send to client os.writeBytes( hello\n);
2. Create I/O streams for communicating with the server. DataInputStream is = new DataInputStream(client.getInputStream( )); DataOutputStream os = new DataOutputStream(client.getOutputStream( ));
3. Perform I/O or communication with the server. //Receive data from the server String line = is.readLine( ); //Send data to the server os.writeBytes(Hello\n);
try{ System.out.println("Attempting To Connect To A TCP service on " + SERVER + ":" + PORT); Socket client = new Socket(SERVER, PORT); System.out.println("Connection Established"); //this is the part where we perform communication with the server client.close(); } catch(UnknownHostException uhe){ System.err.println("Could not resolve hostname: " + uhe.getMessage()); } catch(IOException ioe){ ioe.printStackTrace(); } } }
Internet Addresses
Internet addresses are used to identify the location of Internet nodes. o All data transmitted over the Internet must be addressed to a single OP address The Internet provides a communications mechanism that moves data segments (packets) from one source node to a destination node using best effort
Shown above is each packet is marked with the IP address of the source node as well as the address of the destination node o The client is identified by the IP address 128.59.22.38 o The server is identified by the IP address 204.148.170.161 o The main focus is the end to end packet delivery function of the Internet o The client host sends an IP packet containing the message Hello and addressed to the Internet node located at address 204.148.170.161 o The role of the Internet is to deliver that packet to the server using best effort. o The only guarantee that the Internet Protocol is that if the packet is delivered to the server, it will have the correct header
DNS Namespace
The DNS namespace forms a hierarchical tree structure. o Each node in the tree has a label of length up to 63 bytes o All nodes except the root node must have a non-empty text label and all children of a node must have unique labels
A domain name uniquely identifies a node in the tree o Domain names are created by traversing from any node in the tree up to the root and appending a period . between each label
null(root)
math
Resource Records contain information about the particular node such as its Internet address. DNS permits attributes (resource records) in both nodes with children and leaf nodes Every resource record must have an owner, class, TTL and type attribute o The owner is the domain name to which the resource record belongs o The class refers to the protocol family/version of the record The IPv4 records are of type IN o The TTL stands for Time-To-Live and indicates how long (in seconds) the record can be cached before it is discarded o The type attribute characterizes the information stored in the resource record Owner Class TTL Type Value www.forexworld.com.au IN 60480 A 204.148.170.161
The core DNS specification specifies the following resource record types Type A CNAME Description A host address such as 204.148.170.161 Marks the domain name as an alias for another domain. When a domain name contains a CNAME resource record, it cannot contain other record types. As a result, alias domains cannot add additional information (resource records). For example, the domain www.forexworld.com.au. may be used as an alias for the real server host server.forexworld.co.au. Identifies the CPU and OS used by a host as a string Identifies the domain name handling the e-mail (SMTP) service for this domain and provides the priority with which it should be used Points to another part of the domain space (usually used for supporting reverse
HINFO MX PTR
NS
java.net.InetAddress
Java encapsulates the concept of an IP address in a class the class is part of the java.net package and is called InetAddress. Can perform basic domain name to address and address to domain name queries. InetAddress is not capable of retrieving other resource record such as MX, HINFO and so on InetAddress class is also missing useful operations for creating addresses out of byte-arrays, obtaining all IP addresses of the local host and the use of addresses as network masks.
java.net.InetAddress
+getByName(): java.net.InetAddress +getAllByName(): java.net.InetAddress[ ] +getLocalHost(): java.net.InetAddress +getHostName(): String +getAddress(): byte[ ] +getHostAddress(): String +isMulticastAddress(): boolean
boolean try {
isReverseLookup = Character.isDigit(args[0].charAt(0));
InetAddress[] addresses = InetAddress.getAllByName(args[0]); for(int a = 0; a < addresses.length; a++) { InetAddress address = addresses[a]; if(isReverseLookup) { if(address.getHostAddress().equals(address.getHostName())) { System.out.println("Could not reverse resolve " + address.getHostAddress()); } else { System.out.println(address.getHostAddress() + " reverse resolves to " + address.getHostName()); } } else { System.out.println(address.getHostName() + " resolves to " + address.getHostAddress()); } } } catch(java.net.UnknownHostException e) { System.err.println("Cannot resolve " + args[0]); System.exit(1); } } }
System.exit(1); } } }