0% found this document useful (0 votes)
2 views

Query_OverFlow-1

The document outlines a series of interview questions and answers related to Java Socket Programming and Networking, covering topics such as Inet Address, Server Socket vs. Socket, Datagram and Multicast Sockets, DNS Servers, and network commands like netstat and tcpdump. It includes code examples for creating server and client sockets, as well as explanations of various networking concepts. The document serves as a comprehensive guide for understanding key networking principles in Java and their practical applications.

Uploaded by

bhavanipriy73
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Query_OverFlow-1

The document outlines a series of interview questions and answers related to Java Socket Programming and Networking, covering topics such as Inet Address, Server Socket vs. Socket, Datagram and Multicast Sockets, DNS Servers, and network commands like netstat and tcpdump. It includes code examples for creating server and client sockets, as well as explanations of various networking concepts. The document serves as a comprehensive guide for understanding key networking principles in Java and their practical applications.

Uploaded by

bhavanipriy73
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

QueryOverFlow

QOF Series

Date : Nov 16th to Nov 23 rd


Topic : Java Socket Programming & Networking

Curated By, Documented By,


Madhavakumar D Emmanuel Prithish S
2021PECCS312 2022PECCS663
CODERS FORUM CODERS FORUM
QUERY:

Interviewer : What's Inet Address, port number?


Candidate : Inet Address is class in Java.net. This class
implements IP socket address. Combination of IP
address and Port number. Objects are immutable. Used
for binding and connecting purposes.
Purpose: It is used to identify a device on a network
using its IP address and sometimes hostname.

Interviewer : Give an example for creating an Inet Address


object.

Candidate : int port = 8080;


InetAddress address =
InetAddress.getByName("localhost");
Socket socket = new Socket(address, port);
System.out.println("Connected to: " + address + "
on port " + port);

Interviewer : I thought for creating Socket, we directly give


String like, new Socket("localhost", 8080); Is this correct? Or
wrong?
Candidate : To minimizer the code. We can use Inet socket address
for single line object creation.

Interviewer : What other advantages of using Inet Address


rather than directly using String?
Candidate : Inet address ensures that it's a valid address, and then it
can be passed to a socket.

Interviewer : Can same application (software/program) use


multiple ports?
Candidate : Yeah, we can use to identify the server and client with
same application.
QUERY:
Interviewer : What is the difference between Server Socket and
Socket. How to create them?

Candidate :

Server Socket
1.Used on the server side to listen for incoming connection
requests.

2.Acts as a listener and waits for a connection request.

3.Lives until the server is shut down or the listening port is


closed.

4.Belongs to the java.net.ServerSocket class.

5.Created only on the server-side.

Socket
1.Used on both server and client sides to communicate after a
connection is established.

2.Handles the communication between two endpoints

3.Exists as long as the communication session is active.

4.Belongs to the java.net.Socket class.

5.Created on both the client-side and server-side.


Creating a Server Socket on the Server Side

import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Server is listening on port 5000...");
Socket socket = serverSocket.accept();
System.out.println("Client connected!");
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new
PrintWriter(socket.getOutputStream(), true);
String message = in.readLine();
System.out.println("Received: " + message);
out.println("Echo: " + message);
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Creating a Socket on the Client Side

import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
System.out.println("Connected to the server!");
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(),
out.println("Hello, Server!");
String response = in.readLine();
System.out.println("Server responded: " + response);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Interviewer : What's the difference between: .listen(num)


and .accept() methods in Server Socket?

Candidate : In Java, there is no listen(num) method in


Server Socket. Instead, the Server Socket constructor
automatically prepares the server to listen for incoming
client requests.
accept() method:

The accept() method in the Java ServerSocket class listens


for and accepts incoming connections to a socket

Interviewer :

Looking into the program,

A: In server side, we are giving the port number of the server.

B: In client side, we are giving the IP address and port number


of the client.

👆Which of the above statements are correct?

Candidate : A is correct.The reason why B is incorrect is


because we specify the IP address and port number of the
server to which we connect in client side.

Interviewer : Then what port number that the client will take?

Candidate : Client's port number is dynamically assigned by


the operating system.
QUERY:

Interviewer : Datagram Socket / Multicast Socket.


What are they and how they send & receive datagram
packets ?

Candidate : Datagram socket send and receive data


without establishing a connection. It creates a packet and
sends it to the specified target IP and port, while the socket
listening on that port receives the packets.

Multicast socket extends datagram socket and enables


transfer to multiple recipients simultaneously the packets are
send to a multicast IP and the sockets listening for packets
addressed to the group's IP receive’s them.

Interviewer : Can you please give short examples for both?

Candidate :

Datagram socket:

DatagramSocket socket = new DatagramSocket();


String m = "Hello";
InetAddress adr = InetAddress.getByName("127.0.0.1");
DatagramPacket p = new DatagramPacket(m.getBytes(), 5,
adr, 5000);
socket.send(p);
byte[] b = new byte[1024];
DatagramPacket r = new DatagramPacket(b, b.length);
socket.receive(r);
socket.close();
Multicast Socket:

int port = 5000;


String multicastAdr = "224.0.0.1";
InetAddress adr = InetAddress.getByName(multicastAdr);
NetworkInterface n =
NetworkInterface.getByName("wlam0");
MulticastSocket r = new MulticastSocket(port);
r.joinGroup(new InetSocketAddress(adr, port), n);
byte[] b = new byte[1024];
DatagramPacket rp = new DatagramPacket(b, b.length);
r.receive(rp);
r.leaveGroup(new InetSocketAddress(adr, port), n);
r.close();

Interviewer : What's InetSocketAddress and NetworkInterface ?

Candidate : InetSocket Address represents IP address and port


number.We know that Inet address represents IP address, thus
InetSocket Address combines Inet address and port number and
it's used when dealing with sockets as they need both IP address
and port number.

Network Interface is a class that represents a network interface


available on the machine, be it is software or hardware and also
the IP addresses is assigned to the network interface and it allows
us to ensure that the interface is capable of multicast
communication.
QUERY:

Interviewer : What is Connect time out ? What is Read


time out ?

Candidate : Connect timeout is the maximum duration


to establish a connection with the server, if the
connection cannot be established within the time the
connection fails and results connect time out.

Read timeout is the maximum time to read the data


from the server. If the server takes more time to send
data it results in read timeout.
QUERY:

Interviewer: How to use netstat and tcpdump (linux) commands.


Just the basic ?

Candidate 1 :

1)Netstat is used to show the network related info.

2)tcpdump is a packet sniffing and packet analysis tool.

Candidate 2 :

1)Netstat can display the status of TCP and UDP endpoints,


routing tables, and interface information.

Interviewer: What is network related info?

Candidate : Like active network connection, listening ports,


routing table , protocol statistics, interface statistics etc.

Interviewer: What is packet sniffing?

Candidate : Packet sniffing means capturing the data which


moves through the network analysis if it possesses any danger
for the network and it also monitor the network traffic in real
time.
QUERY:

Interviewer : What's DNS Server? Why does it exist?

Candidate 1: DNS server is a server which translates domain


name into IP address. It exist because it translates domain
name which can be read by a human to IP address which can
be read by a machine.

Candidate 2: It is difficult for the people to remember IP


address so it gives domain name and map it to corresponding
IP address.

It stores and manages huge data of domain name and IP


address so that when we browse it opens the page correctly.

This ensures redundancy, reliability, and faster access to


websites as local DNS servers can often provide quicker
responses.

Candidate 3: In simple it acts as contact in our phone in the


case we only need to remember the name of the person who
needs to be contact not is number (that is if you saved his
number).
Interviewer : Will it convert only DomainName to IP, or can
it work in the reverse too? IP to DomainName?

Candidate : Yes, DNS can work in reverse as well. This


process is called Reverse DNS Lookup.
This is done using a special domain called in-addr.arpa (for
IPv4) or ip6.arpa (for IPv6). When a reverse lookup is
performed, DNS servers query this special domain to find the
domain name associated with a given IP address.

Interviewer : In somecases, multiple domain names like:


example.org & example.com can map to the same IP address,
right? In that case, which domain name will it return?

Candidate : Yes, Host header in http request specifies the


domain requested. So the actual domain name gets "returned"
depends on what the user originally requested.

Interviewer : No I mean, one IP is tied to two domain names.


In that case, if we give the IP, which Domain Name will be
given?

Candidate : The reverse DNS will return only one Primary


Domain of the IP address which the server's administrator
pointer(ptr) record points to if there is no ptr record the sever
returns nothing or the ip address itself.

Interviewer : Can you describe the step-by-step process of


this? I mean, if we are typing http://google.com in our
Browser, the HTTP request is sent to Google Server, right?
Or Not?
Candidate : Yes it is sent to the Google server.

1) We type address on the search bar then the browser will


extract the protocol which is used in this case (http) and the
domain name.

2) It uses DNS to look at the IP address of the domain name in


this case (google.com).

3) Then it's connected to the server where the domain is hosted.

4) And then it's send a http request.

5) After which the server process the request which is send by


the browser.

6) And again the server send’s a response to the request ( like


ok I will send the page ). It send’s the data of the webpage like
html,css and javascript codes to the browser .

7) Atlast the browser will display the Webpage to the user.

You might also like