15 Coffee Nio, Socket, As Well As Networking Interview Questions Answers

Networking in addition to Socket Programming is ane of the of import surface area of Java programming language, specially for those programmers, who are working inwards customer server based applications. Knowledge of of import protocols e.g. TCP in addition to UDP inwards particular is rattling important, specially if you lot are inwards describe of piece of employment organisation of writing high frequency trading application, which communicate via FIX Protocol or native telephone substitution protocol. In this article, nosotros volition some of the frequently asked questions on networking in addition to socket programming, mostly based some 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 in addition to NIO. Main focus of this post service is to brand Java developer familiar alongside depression score parts e.g. how TCP in addition to UDP protocol works, socket options in addition to writing multi-threaded servers inwards Java. Questions discussed hither is non actually tied upwardly alongside Java programming language, in addition to tin hold out used inwards 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 substance Java developer role, you lot ameliorate prepare good on Java NIO, Socket Programming, TCP, UDP in addition to 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 in addition to networking in addition to tin hold out useful for Java interviews.



Java Networking in addition to Socket Programming Questions Answers

Here is my listing of fifteen interview questions related to networking basics, mesh protocol in addition to socket programming inwards 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 inwards Java using NIO selectors in addition to how to implement that using threads, at that spot limitations in addition to issues etc. I volition likely add together few to a greater extent than questions based on some best practices piece writing socket based application inwards Java. If you lot know a skillful enquiry on this topic, experience gratis to suggest.



1) Difference betwixt TCP in addition to UDP protocol?
There are many differences betwixt TCP (Transmission command Protocol) in addition to UDP (User Datagram Protocol), but primary is TCP is connectedness oriented, piece UDP is connectedness less. This way TCP provides guaranteed delivery of messages inwards the companionship they are sent, piece UDP doesn't furnish 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. companionship in addition to merchandise messages inwards electronic trading, wire transfer inwards banking in addition to finance etc. UDP is to a greater extent than suited for media transmission, where loss of ane packet, known as datagrams is affordable in addition to doesn't touching character of service. This reply is plenty for most of the interviews, but you lot demand to hold out 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 cite is most order and data boundary. In TCP, messages are guaranteed to hold out delivered inwards the same companionship as they are sent but information boundary is non preserved, which way multiple messages tin hold out combined in addition to sent together, or receiver may have ane business office of the message inwards ane packet in addition to other business office of the message inwards side past times side packet. Though application volition have total message in addition to inwards the same order. TCP protocol volition do assembling of message for you. On the other hand, UDP sends total message inwards a datagram packet, if clients receives the packet it is guaranteed that it volition larn the total message, but at that spot is no guarantee that packet volition come upwardly inwards same companionship they are sent. In short, you lot must cite next differences betwixt TCP in addition to UDP protocol piece answering during interview :

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

for to a greater extent than detailed answer, run across my post service 9 differences betwixt TCP in addition to UDP protocol.


2) How does TCP handshake works?
Three messages are exchanged as business office of TCP head-shake e.g. Initiator sends SYN,  upon receiving this Listener sends SYN-ACK, in addition to in conclusion initiator replied alongside ACK, at this indicate TCP connectedness is moved to ESTABLISHED state. This procedure is easily understandable past times looking at next diagram.

 Networking in addition to Socket Programming is ane of the of import surface area of Java programming langua fifteen Java NIO, Socket, in addition to Networking Interview Questions Answers








3) How do you lot implement reliable transmission inwards UDP protocol?
This is ordinarily follow-up of previous interview question. Though UDP doesn't furnish delivery guarantee at protocol level, you lot tin innovate your ain logic to keep reliable messaging e.g. past times introducing sequence numbers in addition to retransmission. If receiver expose that it has missed a sequence number, it tin enquire 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 in addition to provides reliability guarantee past times using sequence number in addition to retransmission.


4) What is Network Byte Order? How does 2 host communicate if they have got unlike byte-ordering?
There are 2 ways to shop 2 bytes inwards memory, piffling endian (least meaning byte at the starting address) in addition to large endian (most meaning byte at the starting address). They are collectively known as host byte order. For example, an Intel processor stores the 32-bit integer as iv consecutive bytes inwards retentiveness inwards the companionship 1-2-3-4, where 1 is the most meaning byte. IBM PowerPC processors would shop the integer inwards the byte companionship 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 in addition to they have got unlike byte ordering, they are converted to network byte companionship earlier sending or later receiving. Therefore, a piffling endian micro-controller sending to a UDP/IP network must swap the companionship inwards which bytes appear inside multi byte values earlier the values are sent onto the network, in addition to must swap the companionship inwards which bytes appear inwards multi byte values received from the network earlier the values are used. In short, you lot tin also say network byte companionship is criterion of storing byte during transmission, in addition to it uses large endian byte ordering mechanism.


5) What is Nagle's algorithm?
If interviewer is testing your cognition of TCP/IP protocol than it's rattling rare for him non to enquire this question. Nagle's algorithm is way of improving performance of TCP/IP protocol in addition to networks past times reducing number of TCP packets that needs to hold out sent over network. It industrial plant past times buffering pocket-size packets until buffer reaches Maximum Segment Size. Since pocket-size packets, which contains solely 1 or 2 bytes of data, has to a greater extent than overhead inwards damage of TCP header, which is of xl bytes. These pocket-size packets tin also leads to congestion inwards tiresome network. Nagle's algorithm tries to improve efficiency of TCP protocol past times buffering them, to ship a larger packet. Also Nagle's algorithm has negative resultant on non pocket-size writes, thence if you lot are writing large information on packets than it's ameliorate to disable Nagle's algorithm. In general, Nagle's algorithm is a defense forcefulness against careless application, which sends lots of pocket-size packets to network, but it volition non do goodness or have got a negative resultant on good written application, which properly takes help of buffering.


6) What is TCP_NODELAY?
TCP_NODELAY is an choice to disable Nagle's algorithm, provided past times diverse TCP implementations. Since Nagle's algorithm performs badly alongside TCP delayed acknowledgement algorithm, it's ameliorate 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 instant write has reached the destination. If latency is to a greater extent than concern over bandwidth usage e.g. inwards a network based multi-player game, user wants to run across activity from other histrion immediately, it's ameliorate to bypass Nagle's delay past times using TCP_NODELAY flag.


7) What is multicasting or multicast transmission? Which Protocol is to a greater extent than often than non used for multicast? TCP or UDP?
Multi-casting or multicast transmission is ane to many distribution, where message is delivered to a grouping of subscribers simultaneously inwards a unmarried transmission from publisher. Copies of messages are automatically created inwards other network elements e.g. Routers, but solely when the topology of network requires it. Tibco Rendezvous supports multicast transmission. Multi-casting tin solely hold out implemented using UDP, because it sends total information as datagram package, which tin hold out replicated in addition to 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, in addition to messages may hold out lost or delivered out of order. Reliable multicast protocols such as Pragmatic General Multicast (PGM) have got been developed to add together loss detection in addition to retransmission on top of IP multicast. IP multicast is widely deployed inwards enterprises, commercial stock exchanges, in addition to multimedia content delivery networks. H5N1 mutual firm usage of IP multicast is for IPTV applications


8) What is divergence betwixt Topic in addition to Queue inwards JMS?
Main divergence betwixt Topic in addition to Queue inwards Java Messaging Service comes when nosotros have got multiple consumers to consumer messages. If nosotros set-up multiple listener thread to eat messages from Queue, each messages volition hold out dispatched to solely ane thread in addition to non all thread. On the other mitt inwards illustration of Topic each subscriber gets it's ain re-create of message.


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


10) How do you lot write multi-threaded server inwards Java?
H5N1 multi-threaded server is the ane which tin server multiple clients without blocking. Java provides splendid back upwardly to developer such server. Prior to Java 1.4,  you tin write multi-threaded server using traditional socket IO in addition to threads. This had severe limitation on scalability, because  it creates novel thread for each connectedness in addition to you lot tin solely do a fixed number of threads, depending upon machine's in addition to platform's capability. Though this pattern tin hold out improved past times using thread pools in addition to worker threads, it all the same a resources intensive design. After JDK 1.4 in addition to NIO's introduction, writing scalable in addition to multi-threaded server cash inwards one's chips chip easier. You tin easily do it inwards unmarried thread past times using Selector, which takes wages of asynchronous in addition to non-blocking IO model of Java NIO.


11) What is ephemeral port?
In TCP/IP connectedness ordinarily contains iv things, Server IP, Server port, Client IP in addition to Client Port. Out of these four, three are good known inwards 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 past times your machine's IP stack, from a specified range, known as ephemeral port range, when a customer connectedness explicitly doesn't specify a port number. These are curt lived, temporary port, which tin hold out reused ane time connectedness 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, piece sending datagram . In Linux ephemeral port attain is from 32768 to 61000, piece inwards windows default ephemeral port attain is 1025 to 5000. Similarly unlike operating organisation has unlike 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 in addition to sequential delivery of information packets is required, such as provided past times Transmission Control Protocol (TCP). In the sliding window technique, each packet includes a unique consecutive sequence number, which is used past times the receiving figurer to house information inwards the right order. The objective of the sliding window technique is to usage the sequence numbers to avoid duplicate information in addition to 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 across "too many files open" error. You tin banking concern jibe how many file descriptor per procedure is allowed on UNIX based organisation past times executing ulimit -n command or merely count entries on /proc//fd/


14) What is TIME_WAIT nation inwards TCP protocol? When does a socket connectedness goes to TIME_WAIT state?
When ane halt of TCP Connection closes it past times making organisation call, it goes into TIME_WAIT state. Since TCP packets tin larn inwards in incorrect order, the port must non hold out unopen directly to allow belatedly packets to arrive. That's why that halt of TCP connectedness goes into TIME_WAIT state. For example, if customer closes a socket connectedness than it volition cash inwards one's chips to TIME_WAIT state, similarly if server closes connectedness than you lot volition run across TIME_WAIT there. You tin banking concern jibe condition of your TCP in addition to UDP sockets past times using these networking commands inwards UNIX.


15) What volition locomote on if you lot have got besides many socket connections inwards TIME_WAIT nation on Server?
When a socket connectedness or port goes into TIME_WAIT state, it doesn't unloose file descriptor associated alongside it. File descriptor is solely released when TIME_WAIT nation is gone i.e. later some specified configured time. If besides many connections are inwards TIME_WAIT nation than your Server may ran out of file descriptors in addition to showtime throwing "too many files open" error, in addition to halt accepting novel connections.


That's all most inwards this listing of networking in addition to socket programming interview questions in addition to answers. Though I have got originally intended this listing for Java programmers it is as useful for whatever programmer. In fact, this is bare minimum cognition of sockets in addition to protocols every programmer should have. I have got establish that C in addition to C++ programmers are ameliorate answering these questions than an average Java programmer. One argue of this may hold out because Java programmers has got thence many useful library e.g. Apache MINA, which does all the depression score run for them. Anyway, cognition of fundamentals is rattling of import in addition to everything else is precisely an excuse, but at same indicate I also recommend using tried in addition to tested libraries similar Apache MINA for production code.


Further Reading
The Complete Java MasterClass
Java Network Programming, (4th Addition) past times Harold, Elliotte Rusty
TCP/IP in addition to Networking Fundamentals for information technology Pros


Sumber https://javarevisited.blogspot.com/

0 Response to "15 Coffee Nio, Socket, As Well As Networking Interview Questions Answers"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel