Networking Basics
Networking Basics
Unit-4
Networking Basics
By
Prof. Mane D.S.
H.O.D. Computer Engineering Department
S.P.M. Polytechnic, Kumathe
Client-Server and network
• A computer running a program that makes a request for
services is called client machine.
• A computer running a program that offers requested
services from one or more clients is called server machine.
• The media for communication can be wired or wireless
network.
Client
Network
Department of Computer
Engineering
Java Networking Terminology
• IP Address
• Protocol
• Port Number
• MAC Address
• Connection-oriented and connection-less
protocol
• Socket
Department of Computer
Engineering
IP Address
• IP address is a unique number assigned to a
node of a network e.g. 192.168.0.1 . It is
composed of octets that range from 0 to
255.
• It is a logical address that can be changed.
Department of Computer
Engineering
Protocol
• A protocol is a set of rules basically that is
followed for communication. For example:
• TCP
• UDP
• FTP
• Telnet
• SMTP
• POP etc.
Department of Computer
Engineering
Ports
• The port number is used to uniquely identify
different applications.
• It acts as a communication endpoint between
applications.
• The port number is associated with the IP address
for communication between two applications.
Department of Computer
Engineering
MAC Address
• MAC (Media Access Control) Address is a
unique identifier of NIC (Network Interface
Controller). A network node can have
multiple NIC but each with unique MAC.
Department of Computer
Engineering
Connection-oriented and
connection-less
• In protocol
connection-oriented
protocol, acknowledgement is sent by the
receiver. So it is reliable but slow. The
example of connection-oriented protocol is
TCP.
• But, in connection-less protocol,
acknowledgement is not sent by the
receiver. So it is not reliable but fast. The
example of connection-less protocol is UDP.
Department of Computer
Engineering
Socket
logical connection
1343 Client
192.168.0.2
80 1567
Server Client
192.168.0.3
192.168.0.1
5488
Client
192.168.0.4
Department of Computer
Engineering
Reserved Ports
• If we consider the range of the port numbers,
there are 0 to 65,535 ports available.
• The port numbers ranging from 0 - 1023 are
reserved ports or we can say that are
restricted ports.
• All the 0 to 1023 ports are reserved for use by
well-known services such as FTP, telnet and
http and other system services.
Department of Computer
Engineering
• Reserved port numbers.
Department of Computer
Engineering
java networking classes and
interfaces
• To enable the rapid development of network
applications , Java provides a set of classes,
defined in a package called java.net.
• Key classes, interfaces, and exceptions in
java.net package simplifying the complexity
involved in creating client and server
programs are:
Department of Computer
Engineering
The Classes in java.net Package
• ContentHandler • ServerSocket
• DatagramPacket • Socket
• DatagramSocket • SocketImpl
• DatagramSocketImpl • URL
• HttpURLConnection • URLConnection
• InetAddress • URLEncoder
• MulticastSocket • URLStreamHandler
Department of Computer
Engineering
The Interfaces in java.net package
• ContentHandlerFactory
• FileNameMap
• SocketImplFactory
• URLStreamHandlerFactory
Department of Computer
Engineering
Exceptions in java.net package
• BindException
• ConnectException
• MalformedURLException
• NoRouteToHostException
• ProtocolException
• SocketException
• UnknownHostException
• UnknownServiceException
Department of Computer
Engineering
InetAddress
• The InetAddress class is used to encapsulate
both the numerical IP address and the domain
name for that address.
• We interact with this class by using the name
of an IP host, which is more convenient and
understandable than its IP address.
• The InetAddress class hides the number
inside.
Department of Computer
Engineering
InetAddress class
• static methods you can use to create new
InetAddress objects.
– getByName(String host)
– getAllByName(String host)
– getLocalHost()
InetAddress x = InetAddress.getByName(
“msbte.com”
);
Throws UnknownHostException
Department of Computer
Engineering
22
Factory Methods
• static InetAddress getLocalHost( ) throws UnknownHostException
– returns the IP address of the localhost machine.
System.out.println(Address.getHostAddress());
System.out.println(Address.getHostName());
if(Address.isMulticastAddress())
Department of Computer
Engineering
TCP/IP SOCKET PROGRAMMING
• The two key classes used in creation of server
and client programs are:
• ServerSocket
– Wait for requests to come in over the network
– Implemented by java.net.ServerSocket class
• Sockets (client)
– Used to send and receive data
– Can be thought of as a pair of input and output
streams
– Implemented by java.net.Socket class
Department of Computer
Engineering
Server vs. Client Socket
Server socket: waiting for connection requests
Client socket: transmission of data
server socket
connection request
client socket
Department of Computer
Engineering
Server socket
The constructors used to server socket are given below. All of them
throw IO Exception
Department of Computer
Engineering
Server
• Methods
socket
Some method defined in the Server Socket are:
Methods Description
public Socket accept() throws IOException Waits for a connection request and
returns a Socket
public void setSoTimeout(int timeout) Sets the time-out value for how long the
server socket waits for a client during the
accept().
)
public int getLocalPort() Returns the port number on which this
socket is listening
Department of Computer
Engineering
Methods used by both Server and Client Sockets
Methods Description
Department of Computer
Engineering
Advanced Java Programming
Unit-4
Networking Basics
By
Prof. Mane D.S.
H.O.D. Computer Engineering Department
S.P.M. Polytechnic, Kumathe
URL Class
Java URL Class present in java.net package,
deals with URL (Uniform Resource Locator)
which uniquely identify or locate resources on
internet.
Department of Computer
Engineering
Constructors of URL class
• URL (String urlspecifier)
– Creates a URL object from the String representation.
Department of Computer
Engineering
URL Connection Class
• The abstract class is the
URLConnection
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.
Department of Computer
Engineering
Constructor of URLConnection class
• protected URLConnection(URL url)
– Constructs a URL connection to the specified URL.
A connection to the object referenced by the URL
is not created.
Department of Computer
Engineering
Methods of URLConnection class
• public abstract void connect()throws IOException
– Opens a communications link to the resource referenced by this URL.
• public URL getURL()
– Returns the value of this URLConnection's URL field.
• public int getContentLength()
– Returns the value of the content-length header field.
• public String getContentType()
– Returns the value of the content-type header field.
• public long getDate()
– Returns the value of the date header field.
• public Object getContent()throws IOException
– Retrieves the contents of this URL connection.
• public InputStream getInputStream()throws IOException
– Returns an input stream that reads from this open connection.
• public OutputStream getOutputStream()throws IOException
– Returns an output stream that writes to this connection.
Department of Computer
Engineering
Advanced Java Programming
Unit-4
Networking Basics
By
Prof. Mane D.S.
H.O.D. Computer Engineering Department
S.P.M. Polytechnic, Kumathe
UDP User Datagram Protocol
• A protocol that sends independent packets of data,
called datagrams, from one computer to another with
no guarantees about arrival. UDP is not connection-
based like TCP and is not reliable:
– Sender does not wait for acknowledgements
– Arrival order is not guaranteed
– Arrival is not guaranteed
• Used when speed is essential, even in cost of reliability
– e.g. streaming media, games, Internet telephony, etc.
Department of Computer
Engineering
UDP SOCKET PROGRAMMING
• algorithm for UDP client
– Find the IP address and port number of server
– Create a UDP socket
– Send/ receive data with server using the socket
– Close the connection
Department of Computer
Engineering
Datagram Socket class
The Java DatagramSocket class is used to send
and receive the datagrams it is connection less
socket. That is there is no guarantee of message
delivery.
It has following constructors
1. DatagramSocket()
2. DatagramSocket(int port)
3. DatagramSocket(int port, InetAddress address)
Department of Computer
Engineering
Datagram Packet class
The Java DatagramPacket is class that can be
used to send the packets. If you send multiple
packet, it may arrive in any order. Additionally,
packet delivery is not guaranteed.
It has following constructors
1. DatagramPacket(byte[] barr, int length)
2. DatagramPacket(byte[] barr, int length,
InetAddress address, int port)
Department of Computer
Engineering
The key methods of DatagramPacket class
Method Description
byte[] getData() Returns the data buffer
int getLength() Returns the length of the data to be sent or
the length of the data received.
void setData(byte[] buf) Sets the data buffer for this packet.
void setLength(int length) Sets the length for this packet
Department of Computer
Engineering
import java.net.*;
public class DgramRec
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}
Department of Computer
Engineering
import java.net.*;
public class DGramSender
{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket();
String str = "Java is Easy!!!!!";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(),
str.length(),ip, 3000);
ds.send(dp);
ds.close();
}
}
50
Example of Java Socket Programming
(Read-Write both side)
Department of Computer
Engineering
MyServer.java
import java.net.*;
import java.io.*;
class MyServer{
public static void main(String args[])throws Exception{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
}} Department of Computer
Engineering
MyClient.java
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Serve
r says: "+str2);
}
dout.close();
s.close();
D}ep}artment of 54
Computer Engineering