Java programming language provides the networking API to connect to external URLs, clients and systems. Java networking interview questions are commonly asked for senior level java programming positions or if the job role requires networking experience.
Below Java networking interview questions, answers, tips and samples will help you get you grounded on the basic Java networking concepts; which is sufficient for most interviews.
Important keywords are provided at the end of the questions. Review them, make them a part of Java networking vocabulary, and talk about them confidently during your interview process.
The Java API provides the URL class which can be used to represent the URL address. You can create the URL object if you have the URL address string. The URL class provides getter methods to get the components of the URL such as host name, port, path, query parameters etc.
String urlString = 'http://www.codinggrid.com';
URL url = new URL(urlString);
The Java API provides the 'URLConnecton' class which can be used to create a connection to a URL. If you have a URL object, you can get the URLConnection object by calling openConnection() method on the URL object. Once you have the URLConnection object you can connect to the URL resource by calling the connect() method on the URLConnection object. You can use the URLRequest object to setup parameters and properties that you may need for making the URL connection.
String urlString = 'http://www.codinggrid.com';
URL myUrl = new URL(urlString);
URLConnection myUrlConnection = myUrl.openConnection();
myUrlConnection.connect();
1. Create the URL object
2. Create URLConnection object
3. Open connection to URL
4. Get input stream from connection
5. Read from input stream
6. Close input stream
1. Create the URL object
2. Create URLConnection object
3. Open connection to URL
4. Get output stream from connection
5. Write to output stream
6. Close output stream
Following are some key methods provided in the URL class
openConnection() – Returns a URLConnection object that represents a connection to the URL
getContent() – Gets the contents of the URL
getHost() – Gets the host name of the URL
getPath() – Gets the path of the URL
getPort() – Gets the port of the URL
getProtocol() – Gets the protocol name of the URL
Sockets are end points in the communication link between a client program and a server program exchanging data over a network.
On the server side: a socket is bound to a specific port number. The server listens to the socket, waiting for a client to make a connection request.If a connection from a client is successful, the existing socked is used to communicate with that client. In addition a new socket is created and ties to the same port so that the server can listen to new connections from other clients.A new
On the client side: ...
*** See complete answer in the Java Interview Guide.
1. Open a socket
2. Open an input stream and output stream to a socket
*** See complete answer in the Java Interview Guide.
TCP is a protocol that provides a reliable, point-to-point communication channel that client-server application use to communicate with each other. To communicate over TCP, a client program and server program must first establish a connection to each other through sockets at each end of the communication channel. To communicate, the client and server reads from and writes to the sockets bound to the connection.
Like TCP, UDP is protocol that provides a communication channel that client-server applications use to communicate with each other. But unlike TCP...
*** See complete answer in the Java Interview Guide.
Datagram is an independent, self-contained packet of information send over the network between server and client programs in UDP protocol. The delivery of datagrams to their destinations in not guaranteed. The order of arrival...
*** See complete answer in the Java Interview Guide.
Using UDP protocol, a server can send or broadcast datagrams to multiple clients. Java programming language provides java.net.MultigramSocket which can be used to broadcast datagrams to multiple client programs...
*** See complete answer in the Java Interview Guide.
A network interface is the point of interconnection between a computer and a private or public network.
*** See complete answer in the Java Interview Guide.
You can get a list of IP addresses that are assigned to a network interface using the NetworkInterface class.
*** See complete answer in the Java Interview Guide.