Chap 1
Chap 1
• UDP − UDP stands for User Datagram Protocol, a connection-less protocol that
allows for packets of data to be transmitted between applications.
OSI LAYER TCP LAYER
Transmission control protocol (TCP) User datagram protocol (UDP)
Method Description
// Demonstrate URL.
import java.net.*;
class URLDemo {
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("http://www.osborne.com/downloads");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext:" + hp.toExternalForm());
}
}
URL
CONNECTION
URLConnection is a general-purpose class for accessing the
attributes of a remote resource
InetAddress
class
Domain
name
▪ IP address -> main factor for communication
▪ Ip address -> 32 bit or 128 bits
▪ Inet address class provides methods to get Ip of any host name ( server or client side)
▪ Address- It is a logical or physical address that uniquely identifies a host or a machine in a
telecommunication network.
❑To create an InetAddress object, you have to use one of the available factory methods
Output
default/206.148.209.138
osborne.com/198.45.24.162
www.nba.com/64.241.238.153
www.nba.com/64.241.238.142
INSTANCE METHODS
The InetAddress class also has several other methods, which can
be used on the objects returned by the methods
Methods Description
boolean equals(Object Returns true if this object has the same Internet
other) address as other
byte[ ] getAddress( ) Returns a byte array that represents the object’s
Internet address in network byte order
String toString( ) Returns a string that lists the host name and the IP
address for convenience
▪ TCP is a Network Protocol that stands for Transfer Control Protocol,
which allows well-founded communication between applications.
▪ The communication mechanism between two systems, using TCP, can
be established using Sockets and is known as Socket Programming.
▪ Socket programming is a concept of Network Programming, that
suggests writing programs that are executed across multiple systems,
which are connected to each other using a network.
Mechanism of socket programming
• A client creates a socket at its end of transmission and strives to
connect the socket to the server. When a connection is established,
the server creates a socket at its end and, the client and server can
now ready communicate through writing and reading methods.
Steps in TCP connection
1. An object of ServerSocket is instantiated, and desired port number is specified, on which connection is
going to take place.
2. The accept method of ServerSocket is invoked, in order to hold the server in listening mode. This
method won’t resume until a client is connected to the server through the given port number.
3. Now, on the client-side, an object of Socket is instantiated, and desired port number and IP address is
specified for the connection.
4. An attempt is made, for connecting the client to the server using the specified IP address and port
number. If the attempt is successful, the client is provided with a Socket that is capable of
communicating to the respective server, with write and read methods. If unsuccessful, the desired
exception is raised.
5. Since a client is connected to the server, accept method on the server-side resumes, providing
a Socket that is capable of communicating to the connected client.
6. Once the communication is completed, terminate the sockets on both, the server and the client-side.
TCP/IP CLIENT SOCKETS
❑TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-to- point, stream-
based connections between hosts on the Internet
❑A socket can be used to connect Java’s I/O system to other programs that may reside either on
the local machine or on any other machine on the Internet
❑There are two kinds of TCP sockets in Java. One is for servers, and the other is for clients
❑The ServerSocket class is designed to be a “listener,” which waits for clients to connect
before doing anything
❑The Socket class is designed to connect to server sockets and initiate protocol exchanges
TCP/IP CLIENT SOCKETS
A socket can be examined at any time for the address and port
information associated with it, by use of the following methods
The queue length tells the system how many client connections it
can leave pending before it should simply refuse connections. The
default is 50
TCP/IP SERVER SOCKETS
❑Once the datagram has been released to its intended target, there is no
assurance that it will arrive or even that someone will be there to catch it
❑When the datagram is received, there is no assurance that it hasn’t been
damaged in transit or that whoever sent it is still there to receive a response
❑Java implements datagrams on top of the UDP protocol by using two classes:
The DatagramPacket object is the data container, while the
DatagramSocket is the mechanism used to send or receive the
DatagramPackets
DatagramPacket
DATAGRA - Constructors
MS
❑DatagramPacket(byte data[ ], int size) - specifies a buffer that will
receive data, and the size of a packet