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

Networking as well as Socket Programming is i of the of import expanse of Java programming language, particularly for those programmers, who are working inwards customer server based applications. Knowledge of of import protocols e.g. TCP as well as UDP inwards particular is real important, particularly if you lot are inwards 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 or hence TCP IP protocol. This article is kinda calorie-free 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 post service is to brand Java developer familiar amongst depression degree parts e.g. how TCP as well as UDP protocol works, socket options as well as writing multi-threaded servers inwards Java. Questions discussed hither is non actually tied upwardly amongst Java programming language, as well as tin live 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 heart Java developer role, you lot improve prepare good on Java NIO, Socket Programming, TCP, UDP as well as Networking along amongst 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 fifteen interview questions related to networking basics, cyberspace protocol as well as socket programming inwards Java. Though it doesn't comprise basic questions cast API e.g. Server, ServerSocket, but it focus on high degree concept of writing scalable server inwards Java using NIO selectors as well as how to implement that using threads, at that spot 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 piece writing socket based application inwards Java. If you lot know a practiced inquiry on this topic, experience complimentary 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 chief is TCP is connexion oriented, piece UDP is connexion less. This agency TCP provides guaranteed delivery of messages inwards the gild they are sent, piece UDP doesn't render 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. gild as well as merchandise messages inwards electronic trading, wire transfer inwards 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 touching on character of service. This reply 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 advert is well-nigh order and data boundary. In TCP, messages are guaranteed to live delivered inwards the same gild as they are sent but information boundary is non preserved, which agency multiple messages tin live combined as well as sent together, or receiver may have i business office of the message inwards i packet as well as other business office of the message inwards adjacent packet. Though application volition have total message as well as 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 instruct the total message, but at that spot is no guarantee that packet volition come upwardly inwards same gild they are sent. In short, you lot must advert next differences betwixt TCP as well as UDP protocol piece answering during interview :

  • TCP is guaranteed delivery, UDP is non guaranteed.
  • TCP guarantees gild 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, encounter my post service 9 differences betwixt TCP as well as 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, as well as move initiator replied amongst ACK, at this indicate 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 fifteen Java NIO, Socket, as well as Networking Interview Questions Answers








3) How do you lot implement reliable transmission inwards UDP protocol?
This is unremarkably follow-up of previous interview question. Though UDP doesn't render 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 unwrap 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 ii host communicate if they have got dissimilar byte-ordering?
There are ii ways to shop ii bytes inwards memory, trivial 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 inwards retentiveness inwards the gild 1-2-3-4, where 1 is the most pregnant byte. IBM PowerPC processors would shop the integer inwards the byte gild 4-3-2-1. Networking protocols such as TCP are based on a specific network byte order, which uses big-endian byte ordering. If ii machines are communicating amongst each other as well as they have got dissimilar byte ordering, they are converted to network byte gild earlier sending or later on receiving. Therefore, a trivial endian micro-controller sending to a UDP/IP network must swap the gild inwards which bytes appear inside multi byte values earlier the values are sent onto the network, as well as must swap the gild 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 gild 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 works yesteryear buffering pocket-sized packets until buffer reaches Maximum Segment Size. Since pocket-sized packets, which contains exclusively 1 or 2 bytes of data, has to a greater extent than overhead inwards price of TCP header, which is of forty bytes. These pocket-sized packets tin also leads to congestion inwards deadening network. Nagle's algorithm tries to improve efficiency of TCP protocol yesteryear buffering them, to transportation a larger packet. Also Nagle's algorithm has negative consequence on non pocket-sized 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 strength against careless application, which sends lots of pocket-sized packets to network, but it volition non do goodness or have got a negative consequence on good written application, which properly takes assist of buffering.


6) What is TCP_NODELAY?
TCP_NODELAY is an pick to disable Nagle's algorithm, provided yesteryear diverse TCP implementations. Since Nagle's algorithm performs badly amongst TCP delayed acknowledgement algorithm, it's improve to disable Nagle's when you lot are doing write-write-read operation. Where a read later on ii successive write on socket may instruct 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. inwards a network based multi-player game, user wants to encounter activity from other histrion 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 inwards a unmarried transmission from publisher. Copies of messages are automatically created inwards other network elements e.g. Routers, but exclusively when the topology of network requires it. Tibco Rendezvous supports multicast transmission. Multi-casting tin exclusively 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) have got been developed to add together loss detection as well as retransmission on top of IP multicast. IP multicast is widely deployed inwards enterprises, commercial stock exchanges, as well as multimedia content delivery networks. Influenza A virus subtype H5N1 mutual company usage of IP multicast is for IPTV applications


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


9) What is departure betwixt IO as well as NIO?
Main departure 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 inwards 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 inwards Java?
Influenza A virus subtype H5N1 multi-threaded server is the i which tin server multiple clients without blocking. Java provides first-class back upwardly 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 exclusively 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 withal a resources intensive design. After JDK 1.4 as well as NIO's introduction, writing scalable as well as multi-threaded server croak flake easier. You tin easily do it inwards 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, 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 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 brusque lived, temporary port, which tin live reused i time connexion is closed, but most of IP software, doesn't reuse ephemeral port, until whole hit is exhausted. Similar to TCP, UDP protocol also uses ephemeral port, piece sending datagram . In Linux ephemeral port hit is from 32768 to 61000, piece inwards windows default ephemeral port hit 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 ii 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 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 as well as to asking missing data


13) When do you lot instruct "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 encounter "too many files open" error. You tin cheque how many file descriptor per procedure is allowed on UNIX based organisation yesteryear executing ulimit -n command or merely count entries on /proc//fd/


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


15) What volition come about if you lot have got besides many socket connections inwards TIME_WAIT state on Server?
When a socket connexion or port goes into TIME_WAIT state, it doesn't free file descriptor associated amongst it. File descriptor is exclusively released when TIME_WAIT state is gone i.e. later on some specified configured time. If besides many connections are inwards TIME_WAIT state than your Server may ran out of file descriptors as well as root throwing "too many files open" error, as well as halt accepting novel connections.


That's all well-nigh inwards this listing of networking as well as socket programming interview questions as well as 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 noesis of sockets as well as protocols every programmer should have. I have got works life 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 degree run for them. Anyway, noesis of fundamentals is real of import as well as everything else is precisely an excuse, but at same indicate 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

Komentar

Postingan populer dari blog ini

2 Ways To Banking Concern Tally If A String Is Rotation Of Other Inward Java?

How To Convert String To Integer To String Inward Coffee Amongst Example

How To Induce Chrome, Firefox Blurry, Over Bright, Fading Afterwards Windows Ten Update