Tuesday, April 16, 2024
HomeJavaPrime 15 Java NIO, Socket, and Networking Interview Questions Solutions for Skilled...

Prime 15 Java NIO, Socket, and Networking Interview Questions Solutions for Skilled Builders


Good day guys, in case you are making ready for Java developer interview then you could know that Networking and Socket Programming is without doubt one of the necessary areas of Java programming language, particularly for these programmers, who’re working in consumer server-based functions. Data of necessary protocols like TCP and UDP intimately is essential, particularly in case you are within the enterprise of writing high-frequency buying and selling functions, which talk through FIX Protocol or native trade protocol. On this article, we are going to a few of the steadily requested questions on networking and socket programming, principally primarily based round TCP IP protocol. 
This text is kinda mild on NIO although, because it does not embrace questions from multiplexing, selectors, ByteBuffer, and FileChannel but it surely does embrace classical questions just like the distinction between IO and NIO.

The principle focus of this publish is to make Java builders acquainted with low-level components like how TCP and UDP protocol works, socket choices and writing multi-threaded servers in Java.

The questions mentioned right here are usually not actually tied up with Java programming language and can be utilized in any programming language, which permits programmers to put in writing client-server functions.

By the best way, In case you are going for an interview on Funding banks for a core Java developer position, you higher put together effectively on Java NIO, Socket Programming, TCP, UDP, and Networking together with different in style subjects like multi-threadingCollections API, and Rubbish Assortment tuning. It’s also possible to contribute any query, which is requested to you or associated to socket programming and networking and may be helpful for Java interviews.

15 Finest Java Networking and Socket Programming Questions with Solutions for 3 to five Years Skilled Programmers

Right here is my checklist of 15 interview questions associated to networking fundamentals, web protocol, and socket programming in Java. Although it does not include primary questions from APIs like Server, ServerSocket, it focuses on the high-level idea of writing the scalable server in Java utilizing NIO selectors and how one can implement that utilizing threads, their limitations, and points, and many others.

I’ll in all probability add a couple of extra questions primarily based on some greatest practices whereas writing socket-based functions in Java. If you understand an excellent query on this matter, be happy to recommend.

1. What’s distinction between TCP and UDP protocol? 

There are lots of variations between TCP (Transmission Management Protocol) and UDP (Person Datagram Protocol), however the principle is TCP is connection-oriented, whereas UDP is connectionless. This implies TCP gives assured supply of messages within the order they’re despatched, whereas UDP does not present any supply assure.

Due to this assure, TCP is slower than UDP, because it must carry out extra work. TCP is greatest suited to the message, which you’ll’t afford to lose, like. order and commerce messages in digital buying and selling, wire switch in banking and finance, and many others. UDP is extra suited to media transmission, the place the lack of one packet, referred to as datagrams is reasonably priced and does not have an effect on the standard of service.

This reply is sufficient for many of the interviews, however it’s good to be extra detailed if you end up interviewing as Java developer for a high-frequency buying and selling desk. Among the factors which many candidates overlook to say is about order and knowledge boundary.

In TCP, messages are assured to be delivered in the identical order as they’re despatched however knowledge boundary shouldn’t be preserved, which implies a number of messages may be mixed and despatched collectively, or receiver might obtain one a part of the message in a single packet and different components of the message in subsequent packet.

Although the appliance will obtain the total message and in the identical order. TCP protocol will do the assembling of message for you. However, UDP sends a full message in a datagram packet, if purchasers obtain the packet it’s assured that it’s going to get the total message, however there isn’t a assure that the packet will are available in the identical order they’re despatched.

In brief, you have to point out the next variations between TCP and UDP protocol whereas answering in the course of the interview :

  • TCP is assured supply, UDP shouldn’t be assured.
  • TCP ensures order of messages, UDP does not.
  • Information boundary shouldn’t be preserved in TCP, however UDP preserves it.
  • TCP is slower in comparison with UDP.

for a extra detailed reply, see my publish 9 variations between TCP and UDP protocol.

2. How does TCP handshake works? 

Three messages are exchanged as a part of TCP head-shake e.g. Initiator sends SYN,  upon receiving this Listener sends SYN-ACK, and at last, initiator replied with ACK, at this level TCP connection is moved to ESTABLISHED state. This course of is well comprehensible by trying on the following diagram.

Java Networking Interview Questions and Answers



3. How do you implement dependable transmission in UDP protocol? 

That is normally a follow-up to the earlier interview query. Although UDP does not present a supply assure on the protocol stage, you possibly can introduce your personal logic to keep up dependable messaging e.g. by introducing sequence numbers and retransmission.

If the receiver finds that it has missed a sequence quantity, it might ask for a replay of that message from the Server. TRDP protocol, which is used Tibco Rendezvous (a preferred excessive pace messaging middle-ware) makes use of UDP for sooner messaging and gives reliability assure by utilizing sequence quantity and retransmission.

4. What’s Community Byte Order? How do two hosts talk if they’ve totally different byte-ordering?
There are two methods to retailer two bytes in reminiscence, little-endian (least vital byte on the beginning tackle) and big-endian (most important byte on the beginning tackle). They’re collectively referred to as host byte order. 

For instance, an Intel processor shops the 32-bit integer as 4 consecutive bytes in reminiscence within the order 1-2-3-4, the place 1 is essentially the most vital byte.

IBM PowerPC processors would retailer the integer within the byte order 4-3-2-1. Networking protocols corresponding to TCP are primarily based on a particular community byte order, which makes use of big-endian byte ordering. If two machines are speaking with one another they usually have totally different byte ordering, they’re transformed to community byte order earlier than sending or after receiving.

Subsequently, a little-endian micro-controller sending to a UDP/IP community should swap the order through which bytes seem inside multi-byte values earlier than the values are despatched onto the community, and simply swap the order through which bytes seem in multi-byte values acquired from the community earlier than the values are used. In brief, you can even say community byte order is the usual of storing bytes throughout transmission, and it makes use of a big-endian byte ordering mechanism.

What is Network Byte Order?

5. What’s Nagle’s algorithm?

If the interviewer is testing your information of TCP/IP protocol then it is very uncommon for him to not ask this query. Nagle’s algorithm is a means of enhancing the efficiency of TCP/IP protocol and networks by decreasing the variety of TCP packets that have to be despatched over the community. It really works by buffering small packets till the buffer reaches Most Phase Dimension.

Since small packets, which include just one or 2 bytes of information, has extra overhead by way of TCP header, which is 40 bytes. These small packets may also result in congestion in a gradual community. Nagle’s algorithm tries to enhance the effectivity of TCP protocol by buffering them, to ship a bigger packet.

Additionally, Nagle’s algorithm has a adverse impact on non-small writes, so in case you are writing massive knowledge on packets then it is higher to disable Nagle’s algorithm

Normally, Nagle’s algorithm is a protection in opposition to the careless software, which sends plenty of small packets to the community, but it surely is not going to profit or have a adverse impact on well-written functions, which correctly takes care of buffering.

Right here can also be a pleasant diagram which explains the community visitors with and with out Nagle’s algorithm:

What is Nagle's algorithm?

6. What’s TCP_NODELAY? 

TCP_NODELAY is an choice to disable Nagle’s algorithm, offered by numerous TCP implementations. Since Nagle’s algorithm performs badly with the TCP delayed acknowledgment algorithm, it is higher to disable Nagle’s if you end up doing the write-write-read operation.

The place a learn after two successive writes on the socket might get delayed as much as 500 milliseconds till the second write has reached the vacation spot. 

If latency is extra involved over bandwidth utilization like in a network-based multi-player sport, the consumer needs to see motion from different gamers instantly, it is higher to bypass Nagle’s delay by utilizing the TCP_NODELAY flag.

7. What’s multicasting or multicast transmission? Which Protocol is mostly used for multicast? TCP or UDP? 

Multi-casting or multicast transmission is one too many distributions, the place the message is delivered to a bunch of subscribers concurrently in a single transmission from the writer. Copies of messages are routinely created in different community components e.g. Routers, however solely when the topology of a community requires it.

Tibco Rendezvous helps multicast transmission. Multi-casting can solely be applied utilizing UDP as a result of it sends full knowledge as a datagram bundle, which may be replicated and delivered to different subscribers. Since TCP is a point-to-point protocol, it cannot ship messages to a number of subscribers, till it has the hyperlink between every of them.

Although UDP shouldn’t be dependable, and messages could also be misplaced or delivered out of order. Dependable multicast protocols corresponding to Pragmatic Normal Multicast (PGM) have been developed so as to add loss detection and retransmission on high of IP multicast. 

IP multicast is broadly deployed in enterprises, business inventory exchanges, and multimedia content material supply networks. Widespread enterprise use of IP multicast is for IPTV functions 

Here’s a good diagram which explains how multicasting works:

What is multicasting or multicast transmission?

8. What’s the distinction between Matter and Queue in JMS? 

The principle distinction between Matter and Queue in Java Messaging Service is that Queue is used for level to level communication  which is mostly one to 1, whereas writer is used for publish and subscribe communication which is one to many. 

It’s best to use Matter to ship messages when we have now a number of customers to shopper messages. If we arrange a number of listener threads to devour messages from Queue, every message will probably be dispatched to just one thread and never all threads. However within the case of Matter, every subscriber will get its personal copy of the message.
What is the difference between Topic and Queue in JMS?

9. What’s the distinction between IO and NIO in Java?

The principle distinction between NIO and IO is that NIO gives asynchronous, non-blocking IO, which is essential to put in writing sooner and scalable networking programs. Whereas many of the utility from IO lessons are blocking and gradual. 

NIO takes benefit of asynchronous system calls in UNIX programs corresponding to choose() system name for community sockets. Utilizing choose(), an software can monitor a number of sources on the identical time and may also ballot for community exercise with out blocking. 

The choose() system name identifies if knowledge is pending or not, then learn() or write() could also be used understanding that they’ll full instantly.

What is the difference between IO and NIO in Java?



10. How do you write a multi-threaded server in Java?

A multi-threaded server is one that may serve a number of purchasers with out blocking. Java gives wonderful assist to builders corresponding to servers. Previous to Java 1.4,  you possibly can write a multi-threaded server utilizing conventional socket IO and threads. 

This had a extreme limitation on scalability as a result of it creates a brand new thread for every connection and you may solely create a set variety of threads, relying upon the machine’s and platform’s functionality.

Although this design may be improved by utilizing thread swimming pools and employee threads, it’s nonetheless a resource-intensive design. After JDK 1.4 and NIO’s introduction, writing scalable and multi-threaded servers grow to be a bit simpler. You possibly can simply create it in a single thread by utilizing Selector, which takes benefit of the asynchronous and non-blocking IO mannequin of Java NIO.

How do you write a multi-threaded server in Java?

11. What’s the ephemeral port?

In TCP/IP connection normally incorporates 4 issues, Server IP, Server port, Consumer IP, and Consumer Port. Out of those 4, 3 are well-known more often than not, what shouldn’t be identified is consumer port, that is the place ephemeral ports come into the image.

Ephemeral ports are dynamic ports assigned by your machine’s IP stack, from a specified vary, referred to as the ephemeral port vary, when a consumer connection explicitly does not specify a port quantity.

These are short-lived, non permanent ports, which may be reused as soon as the connection is closed, however most of IP software program does not reuse ephemeral port till the entire vary is exhausted. Much like TCP, UDP protocol additionally makes use of an ephemeral port, whereas sending datagram. 

In Linux ephemeral port vary is from 32768 to 61000, whereas in home windows default ephemeral port vary is 1025 to 5000. The equally totally different working system has totally different ephemeral port ranges

12. What’s the sliding window protocol? 

Sliding window protocol is a way for controlling transmitted knowledge packets between two community computer systems the place dependable and sequential supply of information packets is required, corresponding to offered by the Transmission Management Protocol (TCP). 

Within the sliding window approach, every packet features a distinctive consecutive sequence quantity, which is utilized by the receiving laptop to put knowledge within the appropriate order. The target of the sliding window approach is to make use of the sequence numbers to keep away from duplicate knowledge and to request lacking knowledge

What is the sliding window protocol?

13. When do you get the “too many information open” error? 

Identical to File connection, Socket Connection additionally wants file descriptors, Since each machine has a restricted variety of file descriptors, it is attainable that they might run out of file descriptors. When it occurs, you will notice a “too many information open error. You possibly can verify what number of file descriptor per course of is allowed on UNIX primarily based system by executing ulimit -n command or just rely entries on /proc//fd/

14. What’s TIME_WAIT state in TCP protocol? When does a socket connection go to TIME_WAIT state? 

When one finish of TCP Connection closes it by making a system name, it goes into TIME_WAIT state. Since TCP packets can arrive within the incorrect order, the port should not be closed instantly to permit late packets to reach. That is why that finish of TCP connection goes into TIME_WAIT state. 

For instance, if a consumer closes a socket connection then it’ll go to TIME_WAIT state, equally, if the server closes the connection then you will notice TIME_WAIT there. You possibly can verify the standing of your TCP and UDP sockets by utilizing these networking instructions in UNIX.

15. What is going to occur when you’ve got too many socket connections within the TIME_WAIT state on the Server? 

When a socket connection or port goes into the TIME_WAIT state, it does not launch the file descriptor related to it. The file descriptor is just launched when the TIME_WAIT state is gone i.e. after some specified configured time. If too many connections are within the TIME_WAIT state then your Server might ran out of file descriptors and begin throwing “too many information open” error, and cease accepting new connections.

That is all about on this checklist of networking and socket programming interview questions and solutions. Although I’ve initially supposed this checklist for Java programmers it’s equally helpful for any programmer.

In truth, that is the naked minimal information of sockets and protocols each programmer ought to have. I’ve discovered that C and C++ programmers are higher at answering these questions than the typical Java programmers.

One purpose for this can be as a result of Java programmers have gotten so many helpful libraries like Apache MINA, which does all of the low-level work for them. Anyway, information of fundamentals is essential and all the things else is simply an excuse, however sooner or later, I additionally suggest utilizing tried and examined libraries like Apache MINA for manufacturing code.

Different Interview Query Articles You might prefer to discover

Thanks
for studying this text to date. All one of the best in your Java Developer interview. When you like these superior Java  IO, NIO, and Socket Programming questions for knowledgeable builders then please share with your pals and colleagues. In case you have any questions which do not know reply or any
doubt be happy to ask in feedback. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments