15 Coffee Nio, Socket, Too Networking Interview Questions Answers

Advertisement

Masukkan script iklan 970x90px

15 Coffee Nio, Socket, Too Networking Interview Questions Answers

Jumat, 08 Januari 2021

Networking as well as Socket Programming is i of the of import expanse of Java programming language, peculiarly for those programmers, who are working inward customer server based applications. Knowledge of of import protocols e.g. TCP as well as UDP inward particular is real important, peculiarly if you lot are inward describe of piece of employment concern of writing high frequency trading application, which communicate via FIX Protocol or native central protocol. In this article, nosotros volition some of the frequently asked questions on networking as well as socket programming, mostly based closed to TCP IP protocol. This article is kinda lite on NIO though, as it doesn't include questions from multiplexing, selectors, ByteBuffer and FileChannel but it does include classical questions similar difference betwixt IO as well as NIO. Main focus of this postal service is to brand Java developer familiar alongside depression score parts e.g. how TCP as well as UDP protocol works, socket options as well as writing multi-threaded servers inward Java. Questions discussed hither is non actually tied upward alongside Java programming language, as well as tin live used inward whatever programming language, which allows programmers to write client-server applications. By the way, If you lot are going for interview on Investment banks for centre Java developer role, you lot improve prepare good on Java NIO, Socket Programming, TCP, UDP as well as Networking along alongside other pop topics e.g. multi-threadingCollections API and Garbage Collection tuning. You tin also contribute whatever question, which is asked to you lot or related to socket programming as well as networking as well as tin live useful for Java interviews.



Java Networking as well as Socket Programming Questions Answers

Here is my listing of xv interview questions related to networking basics, mesh protocol as well as socket programming inward Java. Though it doesn't comprise basic questions shape API e.g. Server, ServerSocket, but it focus on high score concept of writing scalable server inward Java using NIO selectors as well as how to implement that using threads, in that place limitations as well as issues etc. I volition in all probability add together few to a greater extent than questions based on some best practices spell writing socket based application inward Java. If you lot know a proficient interrogation on this topic, experience costless to suggest.



1) Difference betwixt TCP as well as UDP protocol?
There are many differences betwixt TCP (Transmission command Protocol) as well as UDP (User Datagram Protocol), but principal is TCP is connexion oriented, spell UDP is connexion less. This way TCP provides guaranteed delivery of messages inward the company they are sent, spell UDP doesn't supply whatever delivery guarantee. Because of this guarantee, TCP is slower than UDP, as it needs to perform to a greater extent than work. TCP is best suited for message, which you lot can't afford to loss, e.g. company as well as merchandise messages inward electronic trading, wire transfer inward banking as well as finance etc. UDP is to a greater extent than suited for media transmission, where loss of i packet, known as datagrams is affordable as well as doesn't impact character of service. This respond is plenty for most of the interviews, but you lot demand to live to a greater extent than detailed when you lot are interviewing as Java developer for high frequency trading desk. Some of the points which many candidate forget to refer is nearly order and data boundary. In TCP, messages are guaranteed to live delivered inward the same company as they are sent but information boundary is non preserved, which way multiple messages tin live combined as well as sent together, or receiver may have i role of the message inward i packet as well as other role of the message inward adjacent packet. Though application volition have total message as well as inward the same order. TCP protocol volition do assembling of message for you. On the other hand, UDP sends total message inward a datagram packet, if clients receives the packet it is guaranteed that it volition larn the total message, but in that place is no guarantee that packet volition come upward inward same company they are sent. In short, you lot must refer next differences betwixt TCP as well as UDP protocol spell answering during interview :

  • TCP is guaranteed delivery, UDP is non guaranteed.
  • TCP guarantees company of messages, UDP doesn't.
  • Data boundary is non preserved inward TCP, but UDP preserves it.
  • TCP is slower compared to UDP.

for to a greater extent than detailed answer, run into my postal service 9 differences betwixt TCP as well as UDP protocol.


2) How does TCP handshake works?
Three messages are exchanged as role of TCP head-shake e.g. Initiator sends SYN,  upon receiving this Listener sends SYN-ACK, as well as lastly initiator replied alongside ACK, at this betoken TCP connexion is moved to ESTABLISHED state. This procedure is easily understandable yesteryear looking at next diagram.

 Networking as well as Socket Programming is i of the of import expanse of Java programming langua xv Java NIO, Socket, as well as Networking Interview Questions Answers








3) How do you lot implement reliable transmission inward UDP protocol?
This is unremarkably follow-up of previous interview question. Though UDP doesn't supply delivery guarantee at protocol level, you lot tin innovate your ain logic to hold reliable messaging e.g. yesteryear introducing sequence numbers as well as retransmission. If receiver let on that it has missed a sequence number, it tin inquire for replay of that message from Server. TRDP protocol, which is used Tibco Rendezvous (a pop high speed messaging middle-ware) uses UDP for faster messaging as well as provides reliability guarantee yesteryear using sequence number as well as retransmission.


4) What is Network Byte Order? How does 2 host communicate if they remove hold dissimilar byte-ordering?
There are 2 ways to shop 2 bytes inward memory, footling endian (least pregnant byte at the starting address) as well as large endian (most pregnant byte at the starting address). They are collectively known as host byte order. For example, an Intel processor stores the 32-bit integer as 4 consecutive bytes inward retention inward the company 1-2-3-4, where 1 is the most pregnant byte. IBM PowerPC processors would shop the integer inward the byte company 4-3-2-1. Networking protocols such as TCP are based on a specific network byte order, which uses big-endian byte ordering. If 2 machines are communicating alongside each other as well as they remove hold dissimilar byte ordering, they are converted to network byte company earlier sending or later receiving. Therefore, a footling endian micro-controller sending to a UDP/IP network must swap the company inward which bytes appear inside multi byte values earlier the values are sent onto the network, as well as must swap the company inward which bytes appear inward multi byte values received from the network earlier the values are used. In short, you lot tin also say network byte company is measure of storing byte during transmission, as well as it uses large endian byte ordering mechanism.


5) What is Nagle's algorithm?
If interviewer is testing your noesis of TCP/IP protocol than it's real rare for him non to inquire this question. Nagle's algorithm is way of improving performance of TCP/IP protocol as well as networks yesteryear reducing number of TCP packets that needs to live sent over network. It industrial plant yesteryear buffering modest packets until buffer reaches Maximum Segment Size. Since modest packets, which contains solely 1 or 2 bytes of data, has to a greater extent than overhead inward price of TCP header, which is of xl bytes. These modest packets tin also leads to congestion inward tiresome network. Nagle's algorithm tries to improve efficiency of TCP protocol yesteryear buffering them, to ship a larger packet. Also Nagle's algorithm has negative upshot on non modest writes, hence if you lot are writing large information on packets than it's improve to disable Nagle's algorithm. In general, Nagle's algorithm is a defense against careless application, which sends lots of modest packets to network, but it volition non do goodness or remove hold a negative upshot on good written application, which properly takes tending of buffering.


6) What is TCP_NODELAY?
TCP_NODELAY is an selection to disable Nagle's algorithm, provided yesteryear diverse TCP implementations. Since Nagle's algorithm performs badly alongside TCP delayed acknowledgement algorithm, it's improve to disable Nagle's when you lot are doing write-write-read operation. Where a read later 2 successive write on socket may larn delayed up-to 500 millisecond, until the minute write has reached the destination. If latency is to a greater extent than concern over bandwidth usage e.g. inward a network based multi-player game, user wants to run into activity from other role instrumentalist immediately, it's improve to bypass Nagle's delay yesteryear using TCP_NODELAY flag.


7) What is multicasting or multicast transmission? Which Protocol is by as well as large used for multicast? TCP or UDP?
Multi-casting or multicast transmission is i to many distribution, where message is delivered to a grouping of subscribers simultaneously inward a unmarried transmission from publisher. Copies of messages are automatically created inward other network elements e.g. Routers, but solely when the topology of network requires it. Tibco Rendezvous supports multicast transmission. Multi-casting tin solely live implemented using UDP, because it sends total information as datagram package, which tin live replicated as well as delivered to other subscribers. Since TCP is a point-to-point protocol, it tin non deliver messages to multiple subscriber, until it has link betwixt each of them. Though, UDP is non reliable, as well as messages may live lost or delivered out of order. Reliable multicast protocols such as Pragmatic General Multicast (PGM) remove hold been developed to add together loss detection as well as retransmission on top of IP multicast. IP multicast is widely deployed inward enterprises, commercial stock exchanges, as well as multimedia content delivery networks. H5N1 mutual venture usage of IP multicast is for IPTV applications


8) What is divergence betwixt Topic as well as Queue inward JMS?
Main divergence betwixt Topic as well as Queue inward Java Messaging Service comes when nosotros remove hold multiple consumers to consumer messages. If nosotros set-up multiple listener thread to swallow messages from Queue, each messages volition live dispatched to solely i thread as well as non all thread. On the other manus inward instance of Topic each subscriber gets it's ain re-create of message.


9) What is divergence betwixt IO as well as NIO?
Main divergence betwixt NIO as well as IO is that NIO provides asynchronous, non blocking IO, which is critical to write faster as well as scalable networking systems. While most of utility from IO classes are blocking as well as slow. NIO convey wages of asynchronous organisation calls inward UNIX systems such as select() organisation telephone telephone for network sockets. Using select(), an application tin monitor several resources at the same fourth dimension as well as tin also poll for network activity without blocking. The select() organisation telephone telephone identifies if information is pending or not, as well as then read() or write() may live used knowing that they volition consummate immediately.


10) How do you lot write multi-threaded server inward Java?
H5N1 multi-threaded server is the i which tin server multiple clients without blocking. Java provides first-class back upward to developer such server. Prior to Java 1.4,  you tin write multi-threaded server using traditional socket IO as well as threads. This had severe limitation on scalability, because  it creates novel thread for each connexion as well as you lot tin solely do a fixed number of threads, depending upon machine's as well as platform's capability. Though this blueprint tin live improved yesteryear using thread pools as well as worker threads, it nevertheless a resources intensive design. After JDK 1.4 as well as NIO's introduction, writing scalable as well as multi-threaded server larn flake easier. You tin easily do it inward unmarried thread yesteryear using Selector, which takes wages of asynchronous as well as non-blocking IO model of Java NIO.


11) What is ephemeral port?
In TCP/IP connexion unremarkably contains 4 things, Server IP, Server port, Client IP as well as Client Port. Out of these four, iii are good known inward most of the time, what is non known is customer port, this is where ephemeral ports comes into picture. ephemeral ports are dynamic port assigned yesteryear your machine's IP stack, from a specified range, known as ephemeral port range, when a customer connexion explicitly doesn't specify a port number. These are brusk lived, temporary port, which tin live reused i time connexion is closed, but most of IP software, doesn't reuse ephemeral port, until whole attain is exhausted. Similar to TCP, UDP protocol also uses ephemeral port, spell sending datagram . In Linux ephemeral port attain is from 32768 to 61000, spell inward windows default ephemeral port attain is 1025 to 5000. Similarly dissimilar operating organisation has dissimilar ephemeral port ranges


12) What is sliding window protocol?
Sliding window protocol is a technique for controlling transmitted information packets betwixt 2 network computers where reliable as well as sequential delivery of information packets is required, such as provided yesteryear Transmission Control Protocol (TCP). In the sliding window technique, each packet includes a unique consecutive sequence number, which is used yesteryear the receiving reckoner to house information inward the right order. The objective of the sliding window technique is to usage the sequence numbers to avoid duplicate information as well as to asking missing data


13) When do you lot larn "too many files open" error?
Just similar File connection, Socket Connection also needs file descriptors, Since every machine has express number of file descriptors, it's possible that they may ran out of file descriptors. When it happen, you lot volition run into "too many files open" error. You tin banking company fit how many file descriptor per procedure is allowed on UNIX based organisation yesteryear executing ulimit -n command or but count entries on /proc//fd/


14) What is TIME_WAIT state inward TCP protocol? When does a socket connexion goes to TIME_WAIT state?
When i destination of TCP Connection closes it yesteryear making organisation call, it goes into TIME_WAIT state. Since TCP packets tin brand it inward incorrect order, the port must non live closed at i time to allow belatedly packets to arrive. That's why that destination of TCP connexion goes into TIME_WAIT state. For example, if customer closes a socket connexion than it volition decease to TIME_WAIT state, similarly if server closes connexion than you lot volition run into TIME_WAIT there. You tin banking company fit condition of your TCP as well as UDP sockets yesteryear using these networking commands inward UNIX.


15) What volition hand if you lot remove hold also many socket connections inward TIME_WAIT state on Server?
When a socket connexion or port goes into TIME_WAIT state, it doesn't unloose file descriptor associated alongside it. File descriptor is solely released when TIME_WAIT state is gone i.e. later some specified configured time. If also many connections are inward TIME_WAIT state than your Server may ran out of file descriptors as well as starting fourth dimension throwing "too many files open" error, as well as halt accepting novel connections.


That's all nearly inward this listing of networking as well as socket programming interview questions as well as answers. Though I remove hold originally intended this listing for Java programmers it is as useful for whatever programmer. In fact, this is bare minimum noesis of sockets as well as protocols every programmer should have. I remove hold establish that C as well as C++ programmers are improve answering these questions than an average Java programmer. One argue of this may live because Java programmers has got hence many useful library e.g. Apache MINA, which does all the depression score travel for them. Anyway, noesis of fundamentals is real of import as well as everything else is exactly an excuse, but at same betoken I also recommend using tried as well as tested libraries similar Apache MINA for production code.


Further Reading
The Complete Java MasterClass
Java Network Programming, (4th Addition) yesteryear Harold, Elliotte Rusty
TCP/IP as well as Networking Fundamentals for information technology Pros