Networking

1)What is UDP and how does it work?
UDP stands for User Datagram Protocol. UDP provides an unreliable packet delivery system built on top of the IP protocol. As with IP, each packet is an individual, and is handled separately. Because of this, the amount of data that can be sent in a UDP packet is limited to the amount that can be contained in a single IP packet. Thus, a UDP packet can contain at most 65507 bytes (this is the 65535-byte IP packet size minus the minimum IP header of 20 bytes and minus the 8-byte UDP header).
UDP packets can arrive out of order or not at all. No packet has any knowledge of the preceding or following packet. The recipient does not acknowledge packets, so the sender does not know that the transmission was successful. UDP has no provisions for flow control--packets can be received faster than they can be used. We call this type of communication connectionless because the packets have no relationship to each other and because there is no state maintained.
The destination IP address and port number are encapsulated in each UDP packet. These two numbers together uniquely identify the recipient and are used by the underlying operating system to deliver the packet to a specific process (application). Each UDP packet also contains the sender's IP address and port number.
One way to think of UDP is by analogy to communications via a letter. You write the letter (this is the data you are sending); put the letter inside an envelope (the UDP packet); address the envelope (using an IP address and a port number); put your return address on the envelope (your local IP address and port number); and then you send the letter.
Like a real letter, you have no way of knowing whether a UDP packet was received. If you send a second letter one day after the first, the second one may be received before the first. Or, the second one may never be received.
1) What is a Datagram?
Datagram is another name for a UDP packet.
Java provides two classes for explicitly dealing with datagrams, DatagramSocket and DatagramPacket. These are both found in the java.net package.
3)Why use UDP if it is unreliable?
Two reasons: speed and overhead. UDP packets have almost no overhead--you simply send them then forget about them. And they are fast, because there is no acknowledgement required for each packet. Keep in mind that unreliable doesn't mean that packets can be lost or misdirected for no reason - it simply means that UDP provides no built-in checking and correction mechanism to gracefully deal with losses caused by network congestion or failure.
UDP is appropriate for the many network services that do not require guaranteed delivery. An example of this is a network time service. Consider a time daemon that issues a UDP packet every second so computers on the LAN can synchronize their clocks. If a packet is lost, it's no big deal--the next one will be by in another second and will contain all necessary information to accomplish the task.
Another common use of UDP is in networked, multi-user games, where a player's position is sent periodically. Again, if one position update is lost, the next one will contain all the required information.
A broad class of applications is built on top of UDP using streaming protocols. With streaming protocols, receiving data in real-time is far more important than guaranteeing delivery. Examples of real-time streaming protocols are RealAudio and RealVideo which respectively deliver real-time streaming audio and video over the Internet. The reason a streaming protocol is desired in these cases is because if an audio or video packet is lost, it is much better for the client to see this as noise or "drop-out" in the sound or picture rather than having a long pause while the client software stops the playback, requests the missing data from the server. That would result in a very choppy, bursty playback which most people find unacceptable, and which would place a heavy demand on the server.
4) What is Multicast and how does it work?
TCP and UDP are both unicast protocols; there is one sender and one receiver. Multicast packets are a special type of UDP packets. But while UDP packets have only one destination and only one receiver, multicast packets can have an arbitrary number of receivers.
Multicast is quite distinct from broadcast; with broadcast packets, every host on the network receives the packet. With multicast, only those hosts that have registered an interest in receiving the packet get it.
This is similar to the way an AWTEvent and its listeners behave in the Abstract Window Toolkit. In the same way that an AWTEvent is sent only to registered listeners, a multicast packet is sent only to members of the multicast group. AWT events, however, are unicast, and must be sent individually to each listener--if there are two listeners, two events are sent. With a MulticastSocket, only one is sent and it is received by many.
As you might guess, MulticastSocket is a subclass of DatagramSocket which has the extended ability to join and leave multicast groups. A multicast group consists of both a multicast address and a port number. The only difference between UDP and multicast in this respect is that multicast groups are represented by special internet addresses in the range 224.0.0.1 to 239.255.255.255, inclusive. Just as there are well-known ports for network services, there are reserved, well-known multicast groups for multicast network services.
When an application subscribes to a multicast group (host/port), it receives datagrams sent by other hosts to that group, as do all other members of the group. Multiple applications may subscribe to a multicast group and port concurrently, and they will all receive group datagrams.
When an application sends a message to a multicast group, all subscribing recipients to that host and port receive the message (within the time-to-live range of the packet, see below). The application needn't be a member of the multicast group to send messages to it.
5) What are RFCs and where can I find them?
RFC stands for "Request for Comment". The RFCs form an integral part of the Internet standards; standards are formed by first publishing a specification as an RFC.
If you wish to implement a standard protocol in Java, the first thing to do is download and read the applicable RFC.
6) How can I send objects across the network using sockets?
Objects that implement Serializable may be sent across a socket connection using an ObjectInputStream and ObjectOutputStream combination.
Here are the steps to follow:
1. First, define an object to send. As an example, we can define a class called Message to encapsulate our communications:
2. public class Message implements Serializable {
3. private int senderID;
4. private String messageText;
5.
6. public Message(int id, String text) {
7. senderID = id;
8. messageText = text;
9. }
10. public String getText() {
11. return messageText;
12. }
13. }
14. Next, instantiate the object, wrap the socket's streams in object streams, then send the message across the socket:
15. Message sayhey = new Message("123456789", "Hello");
16.
17. Socket socket = new Socket(host, port);
18. ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
19.
20. out.writeObject(sayhey);
21. On the other side of the socket, the message can be retrieved and used by invoking methods on the returned object:
22. ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
23. Message messageObject = (Message) in.readObject();
24. String messageText = messageObject.getText();
7) How can I obtain a hostname given an IP address?
The code snippet:
String host = InetAddress.getByName("216.217.9.172").getHostName();
should give you the hostname www.jguru.com
8)How can I obtain the IP address for a given hostname?
The code snippet:
ip =InetAddress.getByName("www.jguru.com").getHostAddress();
should give you the IP address 216.217.9.172
9)How can I download files from a URL using HTTP?
One way to do this is by using a URLConnection to open a stream to your desired URL, then copy the data out of the stream to a file on your local file system. As an example, here's a little snippet of code which lets you download the jGuru logo to your machine:
URL url = new URL("http://www.jguru.com/images/logo.gif");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(stream);
FileOutputStream file = new FileOutputStream("logo.gif");
BufferedOutputStream out = new BufferedOutputStream(file);
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.flush();
Note the use of "Stream" classes instead of "Reader" classes. This is done because in general a URL may contain binary data, as in this example. Also note that both the input and the output streams are buffered - this greatly speeds the download time and is far more efficient that reading and writing a single byte at a time, especially over the network.
11) What is TCP and how does it work?
Internet Protocol, or IP, provides an unreliable packet delivery system--each packet is an individual, and is handled separately. Packets can arrive out of order or not at all. The recipient does not acknowledge them, so the sender does not know that the transmission was successful. There are no provisions for flow control--packets can be received faster than they can be used. And packet size is limited.
Transmission Control Protocol (TCP) is a network protocol designed to address these problems. TCP uses IP, but adds a layer of control on top. TCP packets are lost occasionally, just like IP packets. The difference is that the TCP protocol takes care of requesting retransmits to ensure that all packets reach their destination, and tracks packet sequence numbers to be sure that they are delivered in the correct order. While IP packets are independent, with TCP we can use streams along with the standard Java file I/O mechanism.
Think of TCP as establishing a connection between the two endpoints. Negotiation is performed to establish a "socket", and the socket remains open throughout the duration of the communications. The recipient acknowledges each packet, and packet retransmissions are performed by the protocol if packets are missed or arrive out of order. In this way TCP can allow an application to send as much data as it desires and not be subject to the IP packet size limit. TCP is responsible for breaking the data into packets, buffering the data, resending lost or out of order packets, acknowledging receipt, and controlling rate of data flow by telling the sender to speed up or slow down so that the application never receives more than it can handle.
There are four distinct elements that make a TCP connection unique:
 IP address of the server
 IP address of the client
 Port number of the server
 Port number of the client
Each requested client socket is assigned a unique port number while the server port number is always the same. If any of these numbers is different, the socket is different. A server can thus listen to one and only one port, and talk to multiple clients at the same time.
So a TCP connection is somewhat like a telephone connection; you need to know not only the phone number (IP address), but because the phone may be shared by many people at that location, you also need the name or extension of the user you want to talk to at the other end (port number). The analogy can be taken a little further. If you don't hear what the other person has said, a simple request ("What?") will prompt the other end to resend or repeat the phrase, and the connection remains open until someone hangs up.
12) What's a MalformedURLException?
When you try to create a new URL by calling its constructor, it will throw a MalformedURLException if the URL string is not parseable or contains an unsupported protocol.
13) How can I get the real local host IP address in an applet?
Applet security restrictions do not let you get this in an untrusted applet via InetAddress.getLocalHost().
However, you can get this address by creating a Socket connection back to the web server from which you came and asking the Socket for the local address:
URL url = getDocumentBase();
String host = url.getHost();
Socket socket = new Socket(host, 80);
InetAddress addr = socket.getLocalAddress();
String hostAddr = addr.getHostAddress();
System.out.println("Addr: " + hostAddr);

14) How do I get the real local host IP address in an application (or trusted applet)?

The InetAddress class provides the necessary support:
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost.getHostName());
System.out.println(localHost.getHostAddress());


15) My machine has more than one IP address. How can I specify which address to use for a socket connection?

Both Socket and ServerSocket have a constructor which allows you to specify a particular local IP address to use. In the case of Socket, there are two constructors:
public Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
public Socket(String host, int port, InetAddress localAddr, int localPort)
In the case of ServerSocket, the constructor is:
public ServerSocket(int port, int backlog, InetAddress localAddr)
All three of these constructors may throw an IOException.
16) Are there any performance advantages to be gained by using a Socket to connect to an HTTP server, instead of URL or URLConnection?
Using a Socket gives you more control over the connection - you can set socket options, use your own streams, or even use your own socket implementation factory. However, this comes at great expense; URL and URLConnection do a lot of things for you that you would need to implement from scratch. For example, you would have to completely implement the HTTP protocol, including support for proxies, maintain this code, and keep it up to date as HTTP evolves. URL and URLConnection let you extend functionality through content and protocol handlers - you would need to implement this mechanism yourself or lose that extensibility. I don't know why you would want to do this - the network overhead for HTTP dwarfs any potential savings in CPU time you might get from dealing with low-level sockets, and you lose generality, flexibility, and maintainability.
If you're convinced you really want control at the socket level, I would suggest that you consider writing and using your own protocol handler instead of reinventing URL and URLConnection.
17) Do I have to explicitly close the socket input and output streams before I close a socket?

While closing the socket will close the underlying socket input and output streams, programs typically wrap these streams in other streams. It is important to flush and close these other streams, preferably in a finally block, before closing the socket. This will help to ensure that all application data is properly sent or received and that your program isn't unnecesarily holding on to file descriptors that it no longer needs. An example of this follows:
Socket socket;
BufferedOutputStream out;
try {
socket = new Socket(host, port);
out = new BufferedOutputStream(socket.getOutputStream());
//
// write to the socket using the"out" stream
//
}
catch (IOException e) {
// handle errors
}
finally {
try {
out.flush();
out.close();
socket.close();
}
catch (IOException e) { /*ignore*/ }
}
Although the structure of the finally block is a little awkward in this case, it does ensure that the streams and the socket are properly closed down, whether or not an exception occurred.
18) What protocols are supported by the URL and URLConnection classes?
The protocols supported by URL and URLConnection are VM-specific. Sun's JDK supports a different set than the browser VMs or third-party JDK VMs. The protocols that seem to be common to all implementations are HTTP and FTP and file. Other protocols that might be supported include HTTPS, mailto, gopher, and TELNET.
In the table below, some of the more common VMs are listed showing the major protocols they support. There are other protocols not shown in this table that may be supported.
JDK 1.2 Netscape 4.7 IE 5.0
file Yes Yes Yes
http Yes Yes Yes
https Yes* Yes Yes
ftp Yes Yes Yes
telnet No Yes No
mailto Yes Yes Yes
gopher Yes Yes Yes
* JDK 1.2 supports HTTPS only when the JSSE extension is installed
If a protocol is not supported in your VM, you can extend the functionality of the URL class by writing and installing your own protocol handler.
19) How can I use the mailto: protocol from URL or URLConnection?
Both URL and URLConnection act as mail user agents when using the mailto: protocol. They need to connect to a mail transfer agent to effect delivery. An MTA, for example sendmail, handles delivery and forwarding of e-mail.
Note that a mailto: URL doesn't contain any information about where this MTA is located - you need to specify it explicitly by setting a Java system property on the command line. For example, to run the program mailto.java shown below, you would use the command:
java -Dmail.host=mail.yourisp.net mailto
This tells the mailto: protocol handler to open a socket to port 25 of mail.yourisp.net and speak SMTP to it. You should of course substitute the name of your own SMTP server in the above command line.
import java.io.*;
import java.net.*;

public class mailto {

public static void main(String[] args) {
try {
URL url =
new URL("mailto:foo@bar.com");
URLConnection conn =
url.openConnection();
PrintStream out =
new PrintStream(conn.getOutputStream(), true);

out.print(
"From: jguru-fan@yourisp.com"+"\r\n");
out.print(
"Subject: Works Great!"+"\r\n");
out.print(
"Thanks for the example - it works great!"+"\r\n");
out.close();
System.out.println("Message Sent");
} catch (IOException e) {
System.out.println("Send Failed");
e.printStackTrace();
}
}
}

20) What is the difference between a URI and a URL?
URLs are a subset of all URIs.
The term "Uniform Resource Locator" (URL) refers to the subset of URI that identify resources via a representation of their primary access mechanism (e.g., their network "location"), rather than identifying the resource by name or by some other attribute(s) of that resource.
21) How do I create a simple Datagram client?
1. First allocate space to hold the data we are sending and create an instance of DatagramPacket to hold the data.
2. byte[] buffer = new byte[1024];
3. int port = 1234;
4. InetAddress host = InetAddress.getByName("jguru.com");
5. DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
6. host, port);
7. Create a DatagramSocket and send the packet using this socket.
8. DatagramSocket socket = new DatagramSocket();
9. socket.send(packet);
The DatagramSocket constructor that takes no arguments will allocate a free local port to use. You can find out what local port number has been allocated for your socket, along with other information about your socket if needed.
// Find out where we are sending from
InetAddress localHostname = socket.getLocalAddress();
int localPort = socket.getLocalPort();
The client then waits for a reply from the server. Many protocols require the server to reply to the host and port number that the client used, so the client can now invoke socket.receive() to wait for information from the server.
22) How do I create a simple Datagram server?
To create a server with UDP, do the following:
1. Create a DatagramSocket attached to a port.
2. int port = 1234;
3. DatagramSocket socket = new DatagramSocket(port);
4. Allocate space to hold the incoming packet, and create an instance of DatagramPacket to hold the incoming data.
5. byte[] buffer = new byte[1024];
6. DatagramPacket packet =
7. new DatagramPacket(buffer, buffer.length);
8. Block until a packet is received, then extract the information you need from the packet.
9. // Block on receive()
10. socket.receive(packet);
11.
12. // Find out where packet came from
13. // so we can reply to the same host/port
14. InetAddress remoteHost = packet.getAddress();
15. int remotePort = packet.getPort();
16.
17. // Extract the packet data
18. byte[] data = packet.getData();
The server can now process the data it has received from the client, and issue an appropriate reply in response to the client's request.
23) What is a port?
Port numbers are the means by which an operating system routes incoming packets to the appropriate waiting process. Only one process at a time can listen for incoming packets on a given port. The combination of destination IP address and port uniquely identifies the destination process for a packet. Likewise, the combination of source IP address and port uniquely identifies the source process.
For both TCP and UDP, the port number field of a packet is specified as a 16-bit unsigned integer - this means that valid port numbers range from 1 through 65535. (Port number 0 is reserved and can't be used).
Java does not have any unsigned data types; Java's short data type is 16 bits, but its range is -32768 to 32767 because it is a signed type. Thus, short is not large enough to hold a port number, so all classes which use or return a port number must represent the port number as an int. In the JDK 1.1+, using an int with a value greater than 65535 will generate an IllegalArgumentException. In the JDK 1.0.2 and earlier, values greater than 65535 are truncated and only the low-order 16 bits are used.
Port numbers 1 through 255 are reserved for well-known services. A well-known service is a service that is widely implemented which resides at a published, "well-known", port. If you connect to port 80 of a host, for instance, you may expect to find an HTTP server. On UNIX machines, ports less than 1024 are privileged and can only be bound by the root user. This is so an arbitrary user on a multi-user system can't impersonate well-known services like TELNET (port 23), creating a security problem. Windows has no such restrictions, but you should program as if it did so that your applications will work cross-platform.
24) How do I control whether URLConnection uses the POST or GET method when sending data to an HTTP server?
Since these methods are part of the HTTP protocol, they can only be used when the URL is an "http://" URL. In that case, you can cast the connection returned from URL.openConnection() to be a HttpURLConnection, which has a setRequestMethod() method which lets you select whether GET or POST is used. For example:
if (connection instanceof HttpURLConnection)
((HttpURLConnection)connection).setRequestMethod("POST");
GET is used by default.
25) What is a proxy server and how does it work?
A proxy is something or somebody representing some other party. In the case of an HTTP proxy server, it is a server that relays browser requests to the appropriate web site, receives the web server response, and relays the response back to the browser. An HTTP proxy server is typically used through a firewall to avoid clients connecting directly to the Internet. The HTTP protocol explains how a client should get pages through a proxy.
26) What is a URI?
A URI (Uniform Resource Identifier) is a means of unambiguously locating a resource on the Internet.
There are two types of URIs:
 URL (Uniform Resource Locator)
 URN (Uniform Resource Name)
A URL is a pointer to a particular resource on the Internet at a particular location, while a URN is a pointer to a particular resource but without reference to a particular location.

27) Is there a way to programmatically find out if a port number is in use?
You can do the following:
try {
ServerSocket s = new ServerSocket(portno);
}
catch(IOException e) {
System.out.println("port " + portno + " already in use");
}

28) How can I determine when multiple instances of my program are running on the same machine?
The most portable, general way is for your program to attempt to create a server socket on a particular port. The first instance will be able to create the port and so it will know that it's the first. Subsequent instances won't be able to bind to that server socket and so they'll know that they aren't the first.
If you need to track or otherwise manage all of the instances then you can change things such that if they can't bind to the server socket that they then try to connect to the first instance of the program using a client connect through the socket and you can do whatever you want from there.
Of course, if you're worried about controlling the number of copies of your program for "copy protection" purposes or you're wanting to control multiple instances of unsigned applets then you need to mediate the connections through a trusted server that you control.
29) How would you send a file over a socket connection?
If both ends of the connection knew exactly what was happening or expected (e.g. a single file with a constant filename, in the simplest case), you could do something as simple as have one end read from a FileInputStream and write to the socket's OutputStream, while the other end reads from the socket's InputStream and writes to a FileOutputStream.
If you need to transfer multiple files, or need to transfer some details about the file other than the contents (for example, the file name and path), you could create a class which represents the file and serialize it out to the socket. At the simplest that might look like this:
import java.io.*;

public class TransferableFile implements Serializable
{
private byte[] contents;
private String fileName;

public TransferableFile(String _fileName) {
fileName = _fileName;
// Open a FileInputStream and populate
// the contents array
}

public void writeFile() {
// Open a FileOutputStream to write
// the contents array
}
}
.. at one end you could create the TransferableFile and serialize it out to the socket. At the other end you deserialize and invoke writeFile().
An improvement on that would be to do the file I/O in the readObject() and writeObject() methods. That is, read the file from the disk in writeObject() and stream the data straight out to the socket, then write the data directly to the disk in readObject(). This would allow you to handle arbitrarily large files without changing the memory requirement.
If you need to do anything more complex than that, your best bet is to get an FTP client/server. A quick search in this Networking FAQ or in Google should list a few options.
30) What is "local loop back" used in networking
127.0.0.1 is a special IP addressed reserved for the local host, or "loopback address". It refers to the host on which your program is running. Operating systems typically implement "network" communications with the local host by a sort of software short-circuiting the ethernet hardware, effectively looping back the connection.
31) My computer is mapped to more than one IP adress. From my program, how can I retrieve all the IP addresses the machine is mapped to?
You can use something like:
InetAddress ia=InetAddress.getLocalHost();
InetAddress[] a=ia.getAllByName(ia.getHostName());
32) What is a Keep-Alive? How is it implemented differently in HTTP 1.0 and HTTP 1.1?
Http operates on what is called the request/response paradigm. The client application generates a request for information which is passed to the server, which then replies to it. In the earliest implementation of HTTP, each request created a new socket connection to the server, sent the command, then read the response from the same connection. Although this was simple to specify and implement, it was also slow, especially in a high volume situation.
Keep-alives were added to HTTP to basically reduce the significant overhead of rapidly creating and closing socket connections for each new request. The following is a summary of how it works within HTTP 1.0 and 1.1:
HTTP 1.0
The HTTP 1.0 specification does not really delve into how Keep-Alive should work. Basically, browsers that support Keep-Alive appends an additional header to the request as:
Connection: Keep-Alive
When the server processes the request and generates a response, it also adds a header to the response:
Connection: Keep-Alive
When this is done, the socket connection is not closed as before, but kept open after sending the response. When the client sends another request, it reuses the same connection. The connection will continue to be reused until either the client or the server decides that the conversation is over, and one of them drops the connection.
HTTP 1.1
Under HTTP 1.1, Keep-Alive is implemented differently. All connections, by default, are kept open, unless stated otherwise with the following header:
Connection: close

33) what is URLConnection?

URLConnection is a general purpose class for accessing the attributes of a remote resource. Once you make a connection to a remote server, you can use URLConnection to inspect the properties of the remote object before actually transporting it locally.These attributes are exposed by the http protocol specification and as such only make sense for URL objects that are using the http protocol.

1 comment:

kenzie jones said...

Well explained article.I like it. I am impressed by your article. It helps me a lot in my project. You mentioned all the aspects in the effective way. Thanks.
electronic signatures