Unit 1 Java Networking
Unit 1 Java Networking
Java Networking
1
Networking
• Networking supplements a lot of power to simple programs. With networks, a
single program can regain information stored in millions of computers
positioned anywhere in the world.
2
Network Protocols
Transmission Control Protocol (TCP) User Datagram Protocol (UDP)
• TCP or Transmission Control Protocol • UDP or User Datagram Protocol is a
allows secure communication. connection-less protocol.
• TCP is a connection-oriented protocol. • A simpler Internet protocol in which
error-checking and recovery services
• Complete protocol for transporting are not required.
information like still images, data files,
and web pages. • Data is continuously sent to the
recipient, whether they receive it or
not.
3
IP Address
Port Number
Protocol
Networking
Terminology
MAC Address
Socket
4
IP Address
• A unique address that distinguishes a device on the
internet or a local network.
• The range of each octet varies from 0 to 255.Range
of the IP Address – 0.0.0.0 to 255.255.255.255
• For Example – 192.168.0.1
5
Port Number
• A method to recognize a particular process
connecting internet or other network information
when it reaches a server.
6
Protocol
7
MAC Address
8
Socket
9
In a connection-oriented
service, the user must establish a
Connection- connection before starting the
communication.
oriented
and
connection- However, In connectionless
less protocol protocol, the data is transported in
one route from source to
destination without verifying that
the destination is still there or not.
10
Socket
Programming
• Used for communication
between the applications
running on different JRE.
11
Socket
Programming
• The java.net package of the J2SE APIs
contains a collection of classes and interfaces
that provide the low-level communication
details, allowing you to write java socket
program.
12
Socket Programming
• Sockets provide the communication
mechanism between two computers using
TCP/IP. A client program creates a socket on
its end of the communication and attempts to
connect that socket to a server.
• When the connection is made, the server
creates a socket object on its end of the
communication. The client and the server can
now communicate by writing to and reading
from the socket.
13
Socket Class
Method Description
1) public InputStream returns the InputStream attached
getInputStream() with this socket.
14
Server Socket Class
15
Creating Server
2.Socket s=ss.accept();
16
Creating Client
17
My Server import java.io.*;
import java.net.*;
try{
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
18
My Client import java.io.*;
import java.net.*;
try{
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
19
Java URL
1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case, www.javawebapp.com is the server
name.
3. Port Number: It is
an optional attribute. If we write
http//ww.javawebapp.com:80/jaivkpanchal/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, index.jsp is the file name.
20
Java URL
• URL(String spec)
• Creates an instance of a URL from the given protocol, host, port number, and
file.
• Creates an instance of a URL from the given protocol, host, port number, file,
and handler.
• Creates an instance of a URL from the given protocol name, host name, and file
name.
21
context.
Java URL Methods
Method Description
22
Java URL Methods
public boolean equals(Object obj) it compares the URL with the given object.
23
URL Example import java.net.*;
try{
System.out.println("Protocol: "+url.getProtocol());
}catch(Exception e){System.out.println(e);}
24