Professional Documents
Culture Documents
Unit 4 Networking Basics: Raisoni Group of Institutions
Unit 4 Networking Basics: Raisoni Group of Institutions
Unit 4
Networking Basics
that provide the low-level communication details, allowing you to write programs
that focus on solving the problem at hand.
The java.net package provides support for the two common network protocols −
TCP − TCP stands for Transmission Control Protocol, which allows for reliable
communication between two applications. TCP is typically used over the Internet
Definition of Socket:
A socket is basically an endpoint of a two way communication link between two
programs running on the network.
4
A socket is bound to a port number so that the TCP layer can identify the application
that data is destined to be sent to.
An endpoint is a combination of an IP address and a port number.
5 Socket and ServerSocket classes are used for connection-oriented socket programming
DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.
Java.net.Socket class represents the socket.
RAISONI GROUP OF INSTITUTIONS
Client Side Programming
In order to initiate a clients request, you need to follow the below-mentioned steps:
1. Establish a Connection
The very first step is to establish a socket connection. A socket connection implies that
the two machines have information about each other’s network location (IP Address)
and TCP port.
You can create a Socket with the help of a below statement:
Socket socket = new Socket(“127.0.0.1”, 5000)
Here, the first argument represents the IP address of Server.
The second argument represents the TCP Port. (It is a number that represents which
application should run on a server.)
2. Communication
In order to communicate over a socket connection, streams are used for both input and
6
output the data. After establishing a connection and sending the requests, you need to
close the connection.
3. Closing the connection
The socket connection is closed explicitly once the message to the server is sent.
For Example: https://ghrietn.raisoni.net/diploma#dcse
particular website.
A. Host name.
B. B. Port number.
C. File path.
D. Protocol.
2. In the format for defining the URL what is the last part?
E. Protocol.
F. File path.
G. Port number.
H. Host name.
11
3. What does URL stands for?
a) Uniform Resource Locator
b) Uniform Resource Latch
c) Universal Resource Locator
d) Universal Resource Latch
RAISONI GROUP OF INSTITUTIONS
MCQ on URL
What will be the output of the following Java code?
1.import java.net.*;
2.class networking
3.{
4.public static void main(String[] args) throws MalformedURLException
5.{
6.URL obj = new URL("https://www.example.com/javamcq");
7.System.out.print(obj.getProtocol());
8.}
9.}
a) http
12
b) https
c) www
d) com
13
a) 1
b) 0
c) -1
d) garbage value
to read or write or get further information about when was the page/file last modified,
content length etc.
18
Method Description
import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.google.com");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}catch(Exception e){System.out.println(e);}
}
}
Example of Java InetAddress class to find hostname from IP Address
import java.net.*;
public class NewClass1 {
public static void main(String[] args) {
InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Find the Output for following code
What will be the output of the following Java program?
1.import java.net.*;
2.class networking
3.{
4.public static void main(String[] args) throws UnknownHostException
5.{
6.InetAddress obj1 = InetAddress.getByName("cisco.com");
7.System.out.print(obj1.getHostName());
8.}
9.}
a) cisco
b) cisco.com
c) www.cisco.com
d) none of the mentioned
Find the Output for following code
1.import java.net.*;
2.public class networking
3.{
4.public static void main(String[] args) throws UnknownHostException
5.{
6.InetAddress obj1 = InetAddress.getByName("cisco.com");
7.InetAddress obj2 = InetAddress.getByName("sanfoundry.com");
8.boolean x = obj1.equals(obj2);
9.System.out.print(x);
10.}
11.}
a) 0
b) 1
c) true
d) false
Find the Output for following code
1.import java.net.*;
2.class networking
3.{
4.public static void main(String[] args) throws UnknownHostException
5.{
6.InetAddress obj1 = InetAddress.getByName("sanfoundry.com");
7.InetAddress obj2 = InetAddress.getByName("sanfoundry.com");
8.boolean x = obj1.equals(obj2);
9.System.out.print(x);
10.}
11.}
a) 0
b) 1
c) true
d) false
Answer Following questions
Important methods
Method Description
Important methods
Method Description
Creating Server:
To create the server application, we need to create the instance of ServerSocket class.
Here, we are using 6666 port number for the communication between the client and
server. You may also choose any other port number. The accept() method waits for the
client. If clients connects with the given port number, it returns an instance of Socket.
1.ServerSocket ss=new ServerSocket(6666);
2.Socket s=ss.accept();//establishes connection and waits for the client
Creating Client:
To create the client application, we need to create the instance of Socket class. Here, we
need to pass the IP address or hostname of the Server and a port number. Here, we are
using "localhost" because our server is running on same system.
3.Socket s=new Socket("localhost",6666);
MyServer.java
1.import java.io.*;
2.import java.net.*;
3.public class MyServer {
4.public static void main(String[] args){
5.try{
6.ServerSocket ss=new ServerSocket(6666);
7.Socket s=ss.accept();//establishes connection
8.DataInputStream dis=new DataInputStream(s.getInputStream());
9.String str=(String)dis.readUTF();
10.System.out.println("message= "+str);
11.ss.close();
12.}catch(Exception e){System.out.println(e);}
13.}
14.}
MyClient.java
1.import java.io.*;
2.import java.net.*;
3.public class MyClient {
4.public static void main(String[] args) {
5.try{
6.Socket s=new Socket("localhost",6666);
7.DataOutputStream dout=new DataOutputStream(s.getOutputStream
());
8.dout.writeUTF("Hello Server");
9.dout.flush();
10.dout.close();
11.s.close();
12.}catch(Exception e){System.out.println(e);}
13.}
14.}
DatagramSocket and DatagramPacket
Java DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.
//DSender.java
import java.net.*;
public class DSender{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(),
ip, 3000);
ds.send(dp);
ds.close();
}
}
Example of Receiving DatagramPacket by DatagramSocket
//DReceiver.java
import java.net.*;
public class DReceiver{
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();
}
}