15 Coffee Nio, Socket, In Addition To Networking Interview Questions Answers
Networking together with Socket Programming is ane of the of import surface area of Java programming language, peculiarly for those programmers, who are working inwards customer server based applications. Knowledge of of import protocols e.g. TCP together with UDP inwards item is really important, peculiarly 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 together with socket programming, mostly based precisely about 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 together with NIO. Main focus of this ship service is to brand Java developer familiar amongst depression degree parts e.g. how TCP together with UDP protocol works, socket options together with writing multi-threaded servers inwards Java. Questions discussed hither is non actually tied upwardly amongst Java programming language, together with 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 pith Java developer role, you lot improve prepare good on Java NIO, Socket Programming, TCP, UDP together with Networking along amongst other pop topics e.g. multi-threading, Collections API and Garbage Collection tuning. You tin also contribute whatever question, which is asked to you lot or related to socket programming together with networking together with tin live useful for Java interviews.
1) Difference betwixt TCP together with UDP protocol?
There are many differences betwixt TCP (Transmission command Protocol) together with UDP (User Datagram Protocol), but principal is TCP is connexion oriented, spell UDP is connexion less. This way TCP provides guaranteed delivery of messages inwards the fellowship 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. fellowship together with merchandise messages inwards electronic trading, wire transfer inwards banking together with finance etc. UDP is to a greater extent than suited for media transmission, where loss of ane packet, known as datagrams is affordable together with doesn't impact 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 refer is nearly order and data boundary. In TCP, messages are guaranteed to live delivered inwards the same fellowship as they are sent but information boundary is non preserved, which way multiple messages tin live combined together with sent together, or receiver may have ane business office of the message inwards ane packet together with other business office of the message inwards side past times side packet. Though application volition have total message together with 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 fellowship they are sent. In short, you lot must refer next differences betwixt TCP together with UDP protocol spell answering during interview :
for to a greater extent than detailed answer, run across my ship service 9 differences betwixt TCP together with 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, together with lastly initiator replied amongst ACK, at this scream for TCP connexion is moved to ESTABLISHED state. This procedure is easily understandable past times looking at next diagram.
3) How do you lot implement reliable transmission inwards UDP protocol?
This is normally follow-up of previous interview question. Though UDP doesn't supply delivery guarantee at protocol level, you lot tin innovate your ain logic to keep reliable messaging e.g. past times introducing sequence numbers together with retransmission. If receiver disclose 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 together with provides reliability guarantee past times using sequence number together with 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, picayune endian (least pregnant byte at the starting address) together with 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 retentivity inwards the fellowship 1-2-3-4, where 1 is the most pregnant byte. IBM PowerPC processors would shop the integer inwards the byte fellowship 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 together with they have got dissimilar byte ordering, they are converted to network byte fellowship earlier sending or afterward receiving. Therefore, a picayune endian micro-controller sending to a UDP/IP network must swap the fellowship inwards which bytes appear inside multi byte values earlier the values are sent onto the network, together with must swap the fellowship 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 fellowship is criterion of storing byte during transmission, together with 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 really rare for him non to enquire this question. Nagle's algorithm is way of improving performance of TCP/IP protocol together with networks past times reducing number of TCP packets that needs to live sent over network. It industrial plant past times buffering pocket-size packets until buffer reaches Maximum Segment Size. Since pocket-size packets, which contains alone 1 or 2 bytes of data, has to a greater extent than overhead inwards damage of TCP header, which is of forty bytes. These pocket-size packets tin also leads to congestion inwards deadening 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 consequence on non pocket-size writes, thus 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 pocket-size packets to network, but it volition non do goodness or have got a negative consequence on good written application, which properly takes attention 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 amongst TCP delayed acknowledgement algorithm, it's improve to disable Nagle's when you lot are doing write-write-read operation. Where a read afterward ii successive write on socket may larn delayed up-to 500 millisecond, until the 2nd 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 purpose instrumentalist immediately, it's improve 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 alone when the topology of network requires it. Tibco Rendezvous supports multicast transmission. Multi-casting tin alone live implemented using UDP, because it sends total information as datagram package, which tin live replicated together with 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, together with 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 together with retransmission on top of IP multicast. IP multicast is widely deployed inwards enterprises, commercial stock exchanges, together with multimedia content delivery networks. H5N1 mutual corporation utilization of IP multicast is for IPTV applications
8) What is departure betwixt Topic together with Queue inwards JMS?
Main departure betwixt Topic together with 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 live dispatched to alone ane thread together with non all thread. On the other mitt inwards instance of Topic each subscriber gets it's ain re-create of message.
9) What is departure betwixt IO together with NIO?
Main departure betwixt NIO together with IO is that NIO provides asynchronous, non blocking IO, which is critical to write faster together with scalable networking systems. While most of utility from IO classes are blocking together with slow. NIO convey wages of asynchronous organization calls inwards UNIX systems such as select() organization telephone phone for network sockets. Using select(), an application tin monitor several resources at the same fourth dimension together with tin also poll for network activity without blocking. The select() organization telephone phone identifies if information is pending or not, thus read() or write() may live 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 fantabulous back upwardly to developer such server. Prior to Java 1.4, you tin write multi-threaded server using traditional socket IO together with threads. This had severe limitation on scalability, because it creates novel thread for each connexion together with you lot tin alone do a fixed number of threads, depending upon machine's together with platform's capability. Though this pattern tin live improved past times using thread pools together with worker threads, it even thus a resources intensive design. After JDK 1.4 together with NIO's introduction, writing scalable together with multi-threaded server conk fleck easier. You tin easily do it inwards unmarried thread past times using Selector, which takes wages of asynchronous together with non-blocking IO model of Java NIO.
11) What is ephemeral port?
In TCP/IP connexion normally contains 4 things, Server IP, Server port, Client IP together with Client Port. Out of these four, iii 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 connexion explicitly doesn't specify a port number. These are curt lived, temporary port, which tin live reused ane 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 inwards windows default ephemeral port attain is 1025 to 5000. Similarly dissimilar operating organization 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 together with 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 estimator to house information inwards the right order. The objective of the sliding window technique is to utilization the sequence numbers to avoid duplicate information together with 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 cheque how many file descriptor per procedure is allowed on UNIX based organization past times executing ulimit -n command or but count entries on /proc//fd/
14) What is TIME_WAIT soil inwards TCP protocol? When does a socket connexion goes to TIME_WAIT state?
When ane halt of TCP Connection closes it past times making organization call, it goes into TIME_WAIT state. Since TCP packets tin larn inwards in incorrect order, the port must non live unopen right away 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 conk to TIME_WAIT state, similarly if server closes connexion than you lot volition run across TIME_WAIT there. You tin cheque condition of your TCP together with UDP sockets past times using these networking commands inwards UNIX.
15) What volition spill out if you lot have got as good many socket connections inwards TIME_WAIT soil on Server?
When a socket connexion or port goes into TIME_WAIT state, it doesn't unloose file descriptor associated amongst it. File descriptor is alone released when TIME_WAIT soil is gone i.e. afterward some specified configured time. If as good many connections are inwards TIME_WAIT soil than your Server may ran out of file descriptors together with inaugural off throwing "too many files open" error, together with halt accepting novel connections.
That's all nearly inwards this listing of networking together with socket programming interview questions together with 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 together with protocols every programmer should have. I have got constitute that C together with C++ programmers are improve answering these questions than an average Java programmer. One argue of this may live because Java programmers has got thus many useful library e.g. Apache MINA, which does all the depression degree travel for them. Anyway, noesis of fundamentals is really of import together with everything else is precisely an excuse, but at same scream for I also recommend using tried together with 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 together with Networking Fundamentals for information technology Pros
Java Networking together with Socket Programming Questions Answers
Here is my listing of fifteen interview questions related to networking basics, network protocol together with socket programming inwards Java. Though it doesn't incorporate basic questions shape API e.g. Server, ServerSocket, but it focus on high degree concept of writing scalable server inwards Java using NIO selectors together with how to implement that using threads, at that spot limitations together with 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 inwards Java. If you lot know a skilful inquiry on this topic, experience gratuitous to suggest.1) Difference betwixt TCP together with UDP protocol?
There are many differences betwixt TCP (Transmission command Protocol) together with UDP (User Datagram Protocol), but principal is TCP is connexion oriented, spell UDP is connexion less. This way TCP provides guaranteed delivery of messages inwards the fellowship 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. fellowship together with merchandise messages inwards electronic trading, wire transfer inwards banking together with finance etc. UDP is to a greater extent than suited for media transmission, where loss of ane packet, known as datagrams is affordable together with doesn't impact 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 refer is nearly order and data boundary. In TCP, messages are guaranteed to live delivered inwards the same fellowship as they are sent but information boundary is non preserved, which way multiple messages tin live combined together with sent together, or receiver may have ane business office of the message inwards ane packet together with other business office of the message inwards side past times side packet. Though application volition have total message together with 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 fellowship they are sent. In short, you lot must refer next differences betwixt TCP together with UDP protocol spell answering during interview :
- TCP is guaranteed delivery, UDP is non guaranteed.
- TCP guarantees fellowship 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 ship service 9 differences betwixt TCP together with 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, together with lastly initiator replied amongst ACK, at this scream for TCP connexion is moved to ESTABLISHED state. This procedure is easily understandable past times looking at next diagram.
3) How do you lot implement reliable transmission inwards UDP protocol?
This is normally follow-up of previous interview question. Though UDP doesn't supply delivery guarantee at protocol level, you lot tin innovate your ain logic to keep reliable messaging e.g. past times introducing sequence numbers together with retransmission. If receiver disclose 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 together with provides reliability guarantee past times using sequence number together with 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, picayune endian (least pregnant byte at the starting address) together with 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 retentivity inwards the fellowship 1-2-3-4, where 1 is the most pregnant byte. IBM PowerPC processors would shop the integer inwards the byte fellowship 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 together with they have got dissimilar byte ordering, they are converted to network byte fellowship earlier sending or afterward receiving. Therefore, a picayune endian micro-controller sending to a UDP/IP network must swap the fellowship inwards which bytes appear inside multi byte values earlier the values are sent onto the network, together with must swap the fellowship 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 fellowship is criterion of storing byte during transmission, together with 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 really rare for him non to enquire this question. Nagle's algorithm is way of improving performance of TCP/IP protocol together with networks past times reducing number of TCP packets that needs to live sent over network. It industrial plant past times buffering pocket-size packets until buffer reaches Maximum Segment Size. Since pocket-size packets, which contains alone 1 or 2 bytes of data, has to a greater extent than overhead inwards damage of TCP header, which is of forty bytes. These pocket-size packets tin also leads to congestion inwards deadening 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 consequence on non pocket-size writes, thus 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 pocket-size packets to network, but it volition non do goodness or have got a negative consequence on good written application, which properly takes attention 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 amongst TCP delayed acknowledgement algorithm, it's improve to disable Nagle's when you lot are doing write-write-read operation. Where a read afterward ii successive write on socket may larn delayed up-to 500 millisecond, until the 2nd 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 purpose instrumentalist immediately, it's improve 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 alone when the topology of network requires it. Tibco Rendezvous supports multicast transmission. Multi-casting tin alone live implemented using UDP, because it sends total information as datagram package, which tin live replicated together with 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, together with 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 together with retransmission on top of IP multicast. IP multicast is widely deployed inwards enterprises, commercial stock exchanges, together with multimedia content delivery networks. H5N1 mutual corporation utilization of IP multicast is for IPTV applications
8) What is departure betwixt Topic together with Queue inwards JMS?
Main departure betwixt Topic together with 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 live dispatched to alone ane thread together with non all thread. On the other mitt inwards instance of Topic each subscriber gets it's ain re-create of message.
9) What is departure betwixt IO together with NIO?
Main departure betwixt NIO together with IO is that NIO provides asynchronous, non blocking IO, which is critical to write faster together with scalable networking systems. While most of utility from IO classes are blocking together with slow. NIO convey wages of asynchronous organization calls inwards UNIX systems such as select() organization telephone phone for network sockets. Using select(), an application tin monitor several resources at the same fourth dimension together with tin also poll for network activity without blocking. The select() organization telephone phone identifies if information is pending or not, thus read() or write() may live 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 fantabulous back upwardly to developer such server. Prior to Java 1.4, you tin write multi-threaded server using traditional socket IO together with threads. This had severe limitation on scalability, because it creates novel thread for each connexion together with you lot tin alone do a fixed number of threads, depending upon machine's together with platform's capability. Though this pattern tin live improved past times using thread pools together with worker threads, it even thus a resources intensive design. After JDK 1.4 together with NIO's introduction, writing scalable together with multi-threaded server conk fleck easier. You tin easily do it inwards unmarried thread past times using Selector, which takes wages of asynchronous together with non-blocking IO model of Java NIO.
11) What is ephemeral port?
In TCP/IP connexion normally contains 4 things, Server IP, Server port, Client IP together with Client Port. Out of these four, iii 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 connexion explicitly doesn't specify a port number. These are curt lived, temporary port, which tin live reused ane 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 inwards windows default ephemeral port attain is 1025 to 5000. Similarly dissimilar operating organization 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 together with 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 estimator to house information inwards the right order. The objective of the sliding window technique is to utilization the sequence numbers to avoid duplicate information together with 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 cheque how many file descriptor per procedure is allowed on UNIX based organization past times executing ulimit -n command or but count entries on /proc/
14) What is TIME_WAIT soil inwards TCP protocol? When does a socket connexion goes to TIME_WAIT state?
When ane halt of TCP Connection closes it past times making organization call, it goes into TIME_WAIT state. Since TCP packets tin larn inwards in incorrect order, the port must non live unopen right away 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 conk to TIME_WAIT state, similarly if server closes connexion than you lot volition run across TIME_WAIT there. You tin cheque condition of your TCP together with UDP sockets past times using these networking commands inwards UNIX.
15) What volition spill out if you lot have got as good many socket connections inwards TIME_WAIT soil on Server?
When a socket connexion or port goes into TIME_WAIT state, it doesn't unloose file descriptor associated amongst it. File descriptor is alone released when TIME_WAIT soil is gone i.e. afterward some specified configured time. If as good many connections are inwards TIME_WAIT soil than your Server may ran out of file descriptors together with inaugural off throwing "too many files open" error, together with halt accepting novel connections.
That's all nearly inwards this listing of networking together with socket programming interview questions together with 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 together with protocols every programmer should have. I have got constitute that C together with C++ programmers are improve answering these questions than an average Java programmer. One argue of this may live because Java programmers has got thus many useful library e.g. Apache MINA, which does all the depression degree travel for them. Anyway, noesis of fundamentals is really of import together with everything else is precisely an excuse, but at same scream for I also recommend using tried together with 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 together with Networking Fundamentals for information technology Pros
Belum ada Komentar untuk "15 Coffee Nio, Socket, In Addition To Networking Interview Questions Answers"
Posting Komentar