Java
Networking
Interview Questions With Answers
Networking:
Q: 1) What is the difference between URL instance and URLConnection instance?
Ans : A URL instance represents the location of a resource, and a URLConnection instance represents a link for accessing or communicating
with the resource at the location.
Q: 2) How do I make a connection to URL?
Ans : You obtain a URL instance and then invoke openConnection on it.
URLConnection is an abstract class, which means you can't directly create
instances of it using a constructor. We have to invoke openConnection
method on a URL instance, to get the right kind of connection for your
URL.
Eg. URL url;
URLConnection connection;
try{ url = new URL("...");
conection = url.openConnection();
}catch (MalFormedURLException e) { }
Q: 3) What Is a Socket?
A socket is one end-point of a two-way communication link between two
programs running on the network. A socket is bound to a port number so
that the TCP layer can identify the application that data is destined to
be sent.Socket classes are used to represent the connection between a
client program and a server program. The java.net package provides two
classes--Socket and ServerSocket--which implement the client side of the
connection and the server side of the connection, respectively.
Q: 4) What information is needed to create a TCP Socket?
Ans : The Local System’s IP Address and Port Number. And the Remote
System's IPAddress and Port Number.
Q: 5) What are the two important TCP Socket classes?
Ans : Socket and ServerSocket.
ServerSocket is used for normal two-way socket communication. Socket class
allows us to read and write through the sockets. getInputStream() and
getOutputStream() are the two methods available in Socket class.
Q: 6) When MalformedURLException and UnknownHostException throws?
ANS: When the specified URL is not connected then the URL throw MalformedURLException and If InetAddress’ methods getByName and
getLocalHost are unabletoresolve the host name they throwan
UnknownHostException.
<<<Previous Next>>>