Unit 2 Java Networking
Unit 2 Java Networking
Unit 2 Java Networking
Jatin Ambasana
INDEX
1. Java Networking
2. Java Networking Terminology
3. Java Socket Programming
4. Java URL
5. Java Urlconnection Class
6. Java Inetaddress Class
7. Datagram
1. JAVA NETWORKING:
Java Networking is a concept of connecting two or more computing devices
together so that we can share resources.
Java socket programming provides facility to share data between different
computing devices.
Advantage of Java Networking : Sharing resources
2. Java Networking Terminology:
3. Port Number : 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.
4. 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.
Method Description
1) public InputStream getInputStream() returns the InputStream attached with
this socket.
Method Description
1) public Socket accept() returns the socket and establish a
connection between server and client.
MyServu.java
import java.io.*;
import java.net.*;
public class MyServu {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
Program 1: Sending data from client to server
MyClientu.java
import java.io.*;
import java.net.*;
public class MyClientu {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
4. Java URL
The Java URL class represents an URL. URL is an acronym for Uniform
Resource Locator. It points to a resource on the World Wide Web. For
example http://srpec.org.in
A URL contains many information:
Protocol: In this case, http is the protocol.
Server name or IP Address: In this case, www.srpec.org.in is the
server name.
Port Number: It is an optional attribute. If we write
http//srpec.org.in:80/contacts/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.
File Name or directory name: In this case, index.php is the file
name.
Commonly used methods of Java URL
class
Method Description
public String getProtocol() it returns the protocol of the URL.
Method Description
public static InetAddress it returns the instance of
getByName(String host) throws InetAddress containing LocalHost
UnknownHostException IP and name.
public String getHostName() it returns the host name of the IP
address.
public String getHostAddress() it returns the IP address in string
format.
6 Datagram:
A datagram is an independent self-contained message
sent over the network whose arrival, arrival time and
content are not guaranteed.
It is a basic transfer unit associated with a packet-
switched network.
The java.net package has 3 classes to work with datagram
1. DatagramSocket
2. DatagramPacket
3. MulticastSocket
An application can send or recive DatagramPacket using
DatagramSocket. In addition the DatagramPacket can be
broadcasted using MulticastSocket.
UDP Socket Programming or datagram:
Java DatagramSocket and DatagramPacket classes are used for connection-
less socket programming.