java net package
INTRODUCTION TO NETWORKING:
The Java execution environment is designed so that applications can be easily written to efficiently communicate and share processing with remote systems. Much of this functionality is provided with the standard Java API within the java.net package.
TCP/IP Protocols:
Three protocols are most commonly used within the TCP/IP scheme and a closer investigation of their properties is warranted. Understanding how these three protocols (IP, TCP, and UDP) interact is critical to developing network applications.
Internet Protocol (IP):
IP is the keystone of the TCP/IP suite. All data on the Internet flows through IP packets, the basic unit of IP transmissions. IP is termed a connectionless, unreliable protocol. As a connectionless protocol, IP does not exchange control information before transmitting data to a remote system—packets are merely sent to the destination with the expectation that they will be treated properly. IP is unreliable because it does not retransmit lost packets or detect corrupted data. These tasks must be implemented by higher level protocols, such as TCP.
IP defines a universal-addressing scheme called IP addresses. An IP address is a 32-bit number and each standard address is unique on the Internet. Given an IP packet, the information can be routed to the destination based upon the IP address defined in the packet header. IP addresses are generally written as four numbers, between 0 and 255, separated by period. (for example, 124.148.157.6)
While a 32-bit number is an appropriate way to address systems for computers, humans understandably have difficulty remembering them. Thus, a system called the Domain Name System (DNS) was developed to map IP addresses to more intuitive identifiers and vice-versa. You can use www.netspace.org instead of 128.148.157.6.
It is important to realize that these domain names are not used nor understood by IP. When an application wants to transmit data to another machine on the Internet, it must first translate the domain name to an IP address using the DNS. A receiving application can perform a reverse translation, using the DNS to return a domain name given an IP address. There is not a one-to-one correspondence between IP addresses and domain names: A domain name can map to multiple IP addresses, and multiple IP addresses can map to the same domain name.
Java provides a class to work with IP Addresses, InetAddress.
THE INETADDRESS CLASS:
This class represents an Internet Protocol (IP) address. Applications should use the methods getLocalHost, getByName, or getAllByName to create a new InetAddress instance. Transmission Control Protocol (TCP)
Most Internet applications use TCP to implement the transport layer. TCP provides a reliable, connection-oriented, continuous-stream protocol. The implications of these characteristics are:
- Reliable. When TCP segments, the smallest unit of TCP transmissions, are lost or corrupted, the TCP implementation will detect this and retransmit necessary segments.
- Connection-oriented. TCP sets up a connection with a remote system by transmitting control information, often known as a handshake, before beginning a communication. At the end of the connect, a similar closing handshake ends the transmission.
- Continuous-stream. TCP provides a communications medium that allows for an arbitrary number of bytes to be sent and received smoothly; once a connection has been established, TCP segments provide the application layer the appearance of a continuous flow of data.
Because of these characteristics, it is easy to see why TCP would be used by most Internet applications. TCP makes it very easy to create a network application, freeing you from worrying how the data is broken up or about coding error correction routines. However, TCP requires a significant amount of overhead and perhaps you might wish to code routines that more efficiently provide reliable transmissions given the parameters of your application. Furthermore, retransmission of lost data may be inappropriate for your application, because such information's usefulness may have expired.
An important addressing scheme which TCP defines is the port. Ports separate various TCP communications streams which are running concurrently on the same system. For server applications, which wait for TCP clients to initiate contact, a specific port can be established from where communications will originate. These concepts come together in a programming abstraction known as sockets.
User Datagram Protocol (UDP) :
UDP is a low-overhead alternative to TCP for host-to-host communications. In contrast to TCP, UDP has the following features:
- Unreliable. UDP has no mechanism for detecting errors nor retransmitting lost or corrupted information.
- Connectionless. UDP does not negotiate a connection before transmitting data. Information is sent with the assumption that the recipient will be listening.
- Message-oriented. UDP allows applications to send self-contained messages within UDP datagrams, the unit of UDP transmission. The application must package all information within individual datagrams.
As with TCP, UDP provides the addressing scheme of ports, allowing for many applications to simultaneously send and receive datagrams. UDP ports are distinct from TCP ports. For example, one application can respond to UDP port 512 while another unrelated service handles TCP port 512.
Uniform Resource Locator (URL):
While IP addresses uniquely identify systems on the Internet, and ports identify TCP or UDP services on a system, URLs provide a universal identification scheme at the application level. Anyone who has used a Web browser is familiar with seeing URLs, though their complete syntax may not be self-evident. URLs were developed to create a common format of identifying resources on the Web, but they were designed to be general enough so as to encompass applications that predated the Web by decades. Similarly, the URL syntax is flexible enough so as to accommodate future protocols.
URL Syntax :
The primary classification of URLs is the scheme, which usually corresponds to an application protocol. Schemes include http, ftp, telnet, and gopher. The rest of the URL syntax is in a format that depends upon the scheme. These two portions of information are separated by a colon to give us:
scheme-name:scheme-info
Thus, while mailto:dwb@netspace.org indicates "send mail to user dwb at the machine netspace.org," ftp://dwb@netspace.org/ means "open an FTP connection to netspace.org and log in as user dwb."
General URL Format:
Most URLs used conform to a general format that follows the following pattern:
scheme-name://host:port/file-info#internal-reference
Scheme-name is a URL scheme such as HTTP, FTP, or Gopher. Host is the domain name or IP address of the remote system. Port is the port number on which the service is listening; since most application protocols define a standard port, unless a non-standard port is being used, the port and the colon which delimits it from the host is omitted. File-info is the resource requested on the remote system, which often times is a file. However, the file portion may actually execute a server program and it usually includes a path to a specific file on the system. The internal-reference is usually the identifier of a named anchor within an HTML page. A named anchor allows a link to target a particular location within an HTML page. Usually this is not used, and this token with the # character that delimits it is omitted.
Java and URLs:
Java provides a very powerful and elegant mechanism for creating network client applications allowing you to use relatively few statements to obtain resources from the Internet. The java.net package contains the sources of this power, the URL and URLConnection classes.
THE URL CLASS :
Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.
In general, a URL can be broken into several parts. The previous example of a URL indicates that the protocol to use is http (HyperText Transport Protocol) and that the information resides on a host machine named www.ncsa.uiuc.edu. The information on that host machine is named demoweb/url-primer.html. The exact meaning of this name on the host machine is both protocol dependent and host dependent. The information normally resides in a file, but it could be generated on the fly. This component of the URL is called the file component, even though the information is not necessarily in a file.
A URL can optionally specify a "port", which is the port number to which the TCP connection is made on the remote host machine. If the port is not specified, the default port for the protocol is used instead. For example, the default port for http is 80. An alternative port could be specified as:
http://www.ncsa.uiuc.edu:8080/demoweb/url-primer.html
A URL may have appended to it an "anchor", also known as a "ref" or a "reference". The anchor is indicated by the sharp sign character "#" followed by more characters. For example,
http://java.sun.com/index.html#chapter1
This anchor is not technically part of the URL. Rather, it indicates that after the specified resource is retrieved, the application is specifically interested in that part of the document that has the tag chapter1 attached to it. The meaning of a tag is resource specific.
An application can also specify a "relative URL", which contains only enough information to reach the resource relative to another URL. Relative URLs are frequently used within HTML pages.
For example, if the contents of the URL:
http://java.sun.com/index.html
contained within it the relative URL:
FAQ.html
it would be a shorthand for:
http://java.sun.com/FAQ.html
The relative URL need not specify all the components of a URL. If the protocol, host name, or port number is missing, the value is inherited from the fully specified URL. The file component must be specified. The optional anchor is not inherited.
Example for URL :
GetURLApp.java
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
public class GetURLApp
{
public static void main(String args[])
{
try
{
if(args.length!=1) error("Usage: java GetURLApp URL");
System.out.println("Fetching URL: "+args[0]);
URL url = new URL(args[0]);
BufferedReader inStream = new BufferedReader(
new InputStreamReader(url.openStream()));
String line;
while ((line = inStream.readLine())!= null)
{
System.out.println(line);
}
inStream.close();
}
catch (MalformedURLException ex)
{
error("Bad URL");
}
catch (IOException ex)
{
error("IOException occurred.");
}
}
public static void error(String s){
System.out.println(s);
System.exit(1);
}
}
THE URLCONNECTION CLASS :
The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL. Instances of this class can be used both to read from and to write to the resource referenced by the URL. In general, creating a connection to a URL is a multistep process:
openConnection()
connect()
Manipulate parameters that affect the connection to the remote resource.
Interact with the resource; query header fields and contents.
1. The connection object is created by invoking the openConnection method on a URL.
2. The setup parameters and general request properties are manipulated.
3. The actual connection to the remote object is made, using the connect method.
4. The remote object becomes available. The header fields and the contents of the remote object can be accessed.
The setup parameters are modified using the following methods:
• setAllowUserInteraction
• setDoInput
• setDoOutput
• setIfModifiedSince
• setUseCaches
and the general request properties are modified using the method: setRequestProperty
Default values for the AllowUserInteraction and UseCaches parameters can be set using the methods setDefaultAllowUserInteraction and setDefaultUseCaches. Default values for general request properties can be set using the setDefaultRequestProperty method.
Each of the above set methods has a corresponding get method to retrieve the value of the parameter or general request property. The specific parameters and general request properties that are applicable are protocol specific.
The following methods are used to access the header fields and the contents after the connection is made to the remote object:
- getContent
- getHeaderField
- getInputStream
- getOutputStream
- getContentEncoding
- getContentLength
- getContentType
- getDate
- getExpiration
- getLastModified
In the common case, all of the pre-connection parameters and general request properties can be ignored: the pre-connection parameters and request properties default to sensible values. For most clients of this interface, there are only two interesting methods: getInputStream and getObject, which are mirrored in the URL class by convenience methods.
More information on the request properties and header fields of an http connection can be found at:
http://www.w3.org/hypertext/WWW/Protocols/HTTP1.0/draft-ietf-http-spec.html
TCP Socket Basics:
Sockets were originally developed at the University of California at Berkeley as a tool to easily accomplish network programming. Originally part of UNIX operating systems, the concept of sockets has been incorporated into a wide variety of operating environments, including Java.
What is a Socket?
A socket is a handle to a communications link over the network with another application. A TCP socket is one that utilizes the TCP protocol, inheriting the behavior of that transport protocol. Four pieces of information are needed to create a TCP socket:
• The local system's IP address
• The TCP port number which the local application is using
• The remote system's IP address
• The TCP port number to which the remote application is responding
Sockets are often used in client-server applications: A centralized service waits for various remote machines to request specific resources, handling each request as it arrives. In order for clients to know how to communicate with the server, standard application protocols are assigned well-known ports. On UNIX operating systems, ports below 1024 can only be bound by applications with super-user (for example, root) privileges, and thus for control, these well-known ports lie within this range, by convention. Some well known ports are shown in the following table.
Well-known TCP ports and services
Port
Service
21 FTP
23 Telnet
25 SMTP (Internet Mail Transfer)
79 Finger
80 HTTP
For many application protocols, you can merely use the Telnet application to connect to the service port and then manually emulate a client. This may help you understand how client-server communications work.
Client applications must also obtain, or bind, a port to establish a socket connection. Because the client initiates the communication with the server, such a port number could conveniently be assigned at runtime. Client applications are usually run by normal, unprivileged users on UNIX systems, and thus these ports are allocated from the range above 1024. This convention has held when migrated to other operating systems, and client applications are generally given a dynamically-allocated port above 1024.
Because no two applications can bind the same port on the same machine simultaneously, a socket uniquely identifies a communications link. Realize that a server may respond to two clients on the same port, since the clients will be on different systems and/or different ports; the uniqueness of the link's characteristics are preserved.
JAVA TCP SOCKET CLASSES :
Java has a number of classes, which allow you to create socket-based network applications. The two classes you use include java.net.Socket and java.net.ServerSocket.
THE SERVERSOCKET CLASS
public class ServerSocket extends Object
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.
The actual work of the server socket is performed by an instance of the SocketImpl class. An application can change the socket factory that creates the socket implementation to configure itself to create sockets appropriate to the local firewall.
Example for ServerSocket:
ServerExample.java
import java.io.*;
import java.net.*;
import java.util.Date;
public class ServerExample
{
public static void main(String args[])
{
ServerSocket server = null;
Socket socket = null;
BufferedOutputStream send = null;
try
{
server = new ServerSocket(3000);
System.out.println("server started");
while(true)
{
socket = server.accept();
send = new BufferedOutputStream(socket.getOutputStream());
String date = (new Date()).toString();
byte data[] = new byte[date.length()];
data = date.getBytes();
send.write(data,0,data.length);
send.flush();
System.out.println("data flushed");
send.close();
socket.close();
}
}
catch(Exception err) {
System.out.println("Exception in transferring data to client");
}
}
}
THE SOCKET CLASS :
This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines. The actual work of the socket is performed by an instance of the SocketImpl class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.
Example for Socket :
import java.io.*;
import java.net.*;
public class ClientExample {
public static void main(String args[]) {
Socket socket = null;
BufferedInputStream receive = null;
if(args.length == 0){
System.out.println("Usage : java ClientExample
System.exit(0);
}
String ser_address = args[0];
try {
socket = new Socket(InetAddress.getByName(ser_address),3000);
receive = new BufferedInputStream(socket.getInputStream());
System.out.println("socket created");
byte data[] = new byte[100];
receive.read(data,0,data.length);
String date = new String(data);
System.out.println("Date from server : "+date);
receive.close();
socket.close();
} catch(Exception err){
System.out.println("Exception in accessing file");
}
}
}
Overview of UDP Messaging :
Programming with UDP has significant ramifications. Understanding these factors will inform your network programming. UDP is a good choice for applications in which communications can be separated into discrete messages, where a single query from a client invokes a single response from a server. Time-dependent data is particularly suited to UDP. UDP requires much less overhead, but the burden of engineering any necessary reliability into the system is your responsibility. For instance, if clients never receive responses to their queries—perfectly possible and legitimate with UDP—you might want to program the clients to retransmit the request or perhaps display an informative message indicating communication difficulties.
UDP Socket Characteristics :
UDP is described as unreliable, connectionless, and message-oriented. A common analogy that elucidates UDP is that of communicating with postcards. A dialog with UDP must be quanticized into small messages that fit within a small packet of a specific size, although some packets can hold more data than others. When you send out a message, you can never be certain that you will receive a return message. Unless you do receive a return message, you have no idea if your message was received—your message could have been lost en route, the recipient’s confirmation could have been lost, or the recipient might be ignoring your message. The postcards you will be exchanging between network programs are referred to as datagrams. Within a datagram, you can store an array of bytes. A receiving application can extract this array and decode your message, possibly sending a return datagram response. As with TCP, you will program in UDP using the socket programming abstraction. However, UDP sockets are very different from TCP sockets. Extending the analogy, UDP sockets are much like creating a mailbox. A mailbox is identified by your address, but you don't construct a new one for each person to whom you will be sending a message. Instead, you place an address on the postcard that indicates to whom the message is intended. You place the postcard in the mailbox and it is (eventually) sent on its way.
When receiving a message, you could potentially wait forever until one arrives in your mailbox. Once one does, you can read the postcard. Meta-information appears on the postcard that identifies the sender through the return address. As the previous analogies suggest, UDP programming involves the following general tasks:
• Creating an appropriately addressed datagram to send.
• Setting up a socket to send and receive datagrams for a particular application.
• Inserting datagrams into a socket for transmission.
• Waiting to receive datagrams from a socket.
• Decoding a datagram to extract the message, its recipient, and other meta-information.
Java UDP Classes :
The java.net package has the tools that are necessary to perform UDP communications. For working with datagrams, Java provides the DatagramPacket and DatagramSocket classes. When receiving a UDP datagram, you also use the DatagramPacket class to read the data, sender, and meta-information.
THE DATAGRAMPACKET CLASS
This class represents a datagram packet. Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order.
Example for DatagramPacket :
import java.net.*;
import java.io.*;
public class DatagramClient
{
public static void main(String args[])
{
if(args.length == 0)
{
System.out.println("Usage : java DatagramClient
System.exit(0);
}
String address = args[0];
DatagramPacket dgp = null;
DatagramSocket dgs = null;
byte receive[] = new byte[50];
try
{
dgs = new DatagramSocket(5000,InetAddress.getByName(address));
dgp = new DatagramPacket(receive,receive.length);
dgs.receive(dgp);
System.out.println("data received : "+(new String(receive)));
dgs.close();
}
catch(Exception err) {
System.out.println("Exception in client");
}
}
}
THE DATAGRAMSOCKET CLASS :
This class represents a socket for sending and receiving datagram packets. A datagram socket is the sending or receiving point for a connectionless packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.
0 comments:
Post a Comment