Unix socket Interview Questions & Answers

Unix socket Interview Questions

Are you in search for Unix socket jobs? Then you are at the right place. There is no need to search for jobs or Interview Questions on Unix socket in different sites, here in Wisdomjobs jobs we have provide you with the complete details about the Unix socket Interview Questions and Answers along with the jobs. If you are familiar with the Unix socket then there are various leading companies that offer job position like C/c, Socket Programming, C++ Developer - Linux/Socket Programming, Multi-threading/ipc/sockets, C Linux Multithreading Socket Programming, Technical lead (c/c++, sockets, Multi-threading, Tcp/ip) and also offer many other job roles too. We’ve framed multiple Unix socket job interview questions and answers for your reference in our www.wisdomjobs.com. Following interview questions and answers site for quick win in job hunt.

Unix Socket Interview Questions And Answers

Unix socket Interview Questions
    1. Question 1. How Do Sockets Work?

      Answer :

      The implementation is left up to the vendor of your particular unix, but from the point of view of the programmer, connection-oriented sockets work a lot like files, or pipes. The most noticeable difference, once you have your file descriptor is that read() or write() calls may actually read or write fewer bytes than requested. If this happens, then you will have to make a second call for the rest of the data. There are examples of this in the source code that accompanies the faq.

    2. Question 2. What Is With The Second Parameter In Bind()?

      Answer :

      The man page shows it as "struct sockaddr *my_addr". The sockaddr struct though is just a place holder for the structure it really wants. You have to pass different structures depending on what kind of socket you have. For an AF_INET socket, you need the sockaddr_in structure.

      It has three fields of interest:


      Set this to AF_INET.


      The network byte-ordered 16 bit port number


      The host's ip number.

      This is a struct in_addr,

      which contains only one field, 

      s_addr which is a u_long.

    3. Question 3. If Bind() Fails, What Should I Do With The Socket Descriptor?

      Answer :

      If you are exiting, I have been assured by Andrew that all unixes will close open file descriptors on exit. If you are not exiting though, you can just close it with a regular close() call.

    4. Question 4. When Should I Use Shutdown()?

      Answer :

      shutdown() is useful for delineating when you are done providing a request to a server using TCP. A typical use is to send a request to a server followed by a shutdown(). The server will read your request followed by an EOF (read of 0 on most unix implementations). This tells the server that it has your full request. You then go read blocked on the socket. The server will process your request and send the necessary data back to you followed by a close. When you have finished reading all of the response to your request you will read an EOF thus signifying that you have the whole response. It should be noted the TTCP (TCP for Transactions -- see R. Steven's home page) provides for a better method of tcp transaction management.

    5. Question 5. What Do We Expect?

      Answer :

      The child detects an EOF from its stdin, it closes the socket (assuming connection break) and exits. The server in its turn detects EOF, closes connection and exits. The parent detects EOF, makes the wait() system call and exits. What do we see instead? The socket instance in the parent process is still opened for writing and reading, though the parent never writes. The server never detects EOF and waits for more data from the client forever. The parent never sees the connection is closed and hangs forever and the server hangs too. Unexpected deadlock!

      You should change the client fragment as follows:

      if( fork() ) { 

      /* The child */

      while( gets(buffer) }



      /* Break the connection

      for writing, The server will detect EOF now.

      Note: reading from the socket is still allowed.

      The server may send some more data

      after receiving EOF, why not? */



    6. Question 6. What Are Sockets?

      Answer :

      Sockets are just like "worm holes" in science fiction. When things go into one end, they (should) come out of the other. Different kinds of sockets have different properties. Sockets are either connection- oriented or connectionless. Connection-oriented sockets allow for data to flow back and forth as needed, while connectionless sockets (also known as datagram sockets) allow only one message at a time to be transmitted, without an open connection. There are also different socket families. The two most common are AF_INET for internet connections, and AF_UNIX for unix IPC (interprocess communication). As stated earlier, this FAQ deals only with AF_INET sockets.

    7. Question 7. How Can I Tell When A Socket Is Closed On The Other End?

      Answer :

      If the peer calls close() or exits, without having messed with SO_LINGER, then our calls to read() should return 0. It is less clear what happens to write() calls in this case; I would expect EPIPE, not on the next call, but the one after.

      If the peer reboots, or sets l_onoff = 1,l_linger = 0 and then closes, then we should get ECONNRESET (eventually) from read(), or EPIPE from write().

      When write() returns EPIPE, it also raises the SIGPIPE signal - you never see the EPIPE error unless you handle or ignore the signal.

      If the peer remains unreachable, we should get some other error.

      Don't think that write() can legitimately return 0. read() should return 0 on receipt of a FIN from the peer, and on all following calls.

      So yes, you must expect read() to return 0.

      As an example, suppose you are receiving a file down a TCP link; you might handle the return from read() like this:

      rc = read(sock,buf,sizeof(buf));

      if (rc > 0)



      /* error checking on file omitted */


      else if (rc == 0)




      /* file received successfully */


      else /* rc < 0 */


      /* close file and delete it, since data 

      is not complete report error, or whatever */


    8. Question 8. How Do I Get The Port Number For A Given Service?

      Answer :

      Use the getservbyname() routine. This will return a pointer to a servent structure. You are interested in the s_port field, which contains the port number, with correct byte ordering (so you don't need to call htons() on it). Here is a sample routine.

      /* Take a service name, and a service type, and return a port number. If the service name is not found, it tries it as a decimal number. The number returned is byte ordered for the network. */

      int atoport(char *service, char *proto)


      int port;

      long int lport;

      struct servent *serv;

      char *errpos;

      /* First try to read it from /etc/services */

      serv = getservbyname(service, proto);

      if (serv != NULL)

      port = serv->s_port;

      else { 

      /* Not in services, maybe a number? */

      lport = strtol(service,&errpos,0);

      if ( (errpos[0] != 0) || (lport < 1) 

      || (lport > 5000) )

      return -1; 

      /* Invalid port address */

      port = htons(lport);


      return port;


    9. Question 9. How Do I Properly Close A Socket?

      Answer :

      This question is usually asked by people who try close(), because they have seen that that is what they are supposed to do, and then run netstat and see that their socket is still active. Yes, close() is the correct method. To read about the TIME_WAIT state, and why it is important, refer to ``2.7 Please explain the TIME_WAIT state.''.

    10. Question 10. What Is The Difference Between Close() And Shutdown()?

      Answer :

      Generally the difference between close() and shutdown() is: close() closes the socket id for the process but the connection is still opened if another process shares this socket id. The connection stays opened both for read and write, and sometimes this is very important. shutdown() breaks the connection for all processes sharing the socket id. Those who try to read will detect EOF, and those who try to write will reseive SIGPIPE, possibly delayed while the kernel socket buffer will be filled. Additionally, shutdown() has a second argument which denotes how to close the connection: 0 means to disable further reading, 1 to disable writing and 2 disables both.

    11. Question 11. Explain The Time_wait State?

      Answer :

      Remember that TCP guarantees all data transmitted will be delivered, if at all possible. When you close a socket, the server goes into a TIME_WAIT state, just to be really really sure that all the data has gone through. When a socket is closed, both sides agree by sending messages to each other that they will send no more data. This, it seemed to me was good enough, and after the handshaking is done, the socket should be closed. The problem is two-fold. First, there is no way to be sure that the last ack was communicated successfully. Second, there may be "wandering duplicates" left on the net that must be dealt with if they are delivered.

    12. Question 12. How Can I Put A Timeout On Connect()?

      Answer :

      First, create the socket and put it into non-blocking mode, then call connect().

      There are three possibilities:

      • connect succeeds: the connection has been successfully made (this usually only happens when connecting to the same machine)
      • connect fails: obvious
      • connect returns:-1/EINPROGRESS. The connection attempt has begun, but not yet completed.

      If the connection succeeds:

      • The socket will select() as writable (and will also select as readable if data arrives)

      If the connection fails:

      The socket will select as readable and writable, but either a read or write will return the error code from the connection attempt. Also, you can use getsockopt(SO_ERROR) to get the error status - but be careful; some systems return the error code in the result parameter of getsockopt(), but others (incorrectly) cause the getsockopt call itself to fail with the stored value as the error.

    13. Question 13. Why Does The Sockets Buffer Fill Up Sooner Than Expected?

      Answer :

      • In the traditional BSD socket implementation, sockets that are atomic such as UDP keep received data in lists of mbufs. An mbuf is a fixed size buffer that is shared by various protocol stacks. When you set your receive buffer size, the protocol stack keeps track of how many bytes of mbuf space are on the receive buffer, not the number of actual bytes. This approach is used because the resource you are controlling is really how many mbufs are used, not how many bytes are being held in the socket buffer. (A socket buffer isn't really a buffer in the traditional sense, but a list of mbufs).
      • For example: Lets assume your UNIX has a small mbuf size of 256 bytes. If your receive socket buffer is set to 4096, you can fit 16 mbufs on the socket buffer. If you receive 16 UDP packets that are 10 bytes each, your socket buffer is full, and you have 160 bytes of data. If you receive 16 UDP packets that are 200 bytes each, your socket buffer is also full, but contains 3200 bytes of data. FIONREAD returns the total number of bytes, not the number of messages or bytes of mbufs. Because of this, it is not a good indicator of how full your receive buffer is.
      • Additionaly, if you receive UDP messages that are 260 bytes, you use up two mbufs, and can only recieve 8 packets before your socket buffer is full. In this case, only 2080 bytes of the 4096 are held in the socket buffer.
      • This example is greatly simplified, and the real socket buffer algorithm also takes into account some other parameters. Note that some older socket implementations use a 128 byte mbuf.

    14. Question 14. How Often Should I Re-transmit Un-acknowledged Messages?

      Answer :

      The simplest thing to do is simply pick a fairly small delay such as one second and stick with it. The problem is that this can congest your network with useless traffic if there is a problem on the lan or on the other machine, and this added traffic may only serve to make the problem worse.

    15. Question 15. How Can I Be Sure That A Udp Message Is Received?

      Answer :

      You have to design your protocol to expect a confirmation back from the destination when a message is received. Of course is the confirmation is sent by UDP, then it too is unreliable and may not make it back to the sender. If the sender does not get confirmation back by a certain time, it will have to re-transmit the message, maybe more than once. Now the receiver has a problem because it may have already received the message, so some way of dropping duplicates is required. Most protocols use a message numbering scheme so that the receiver can tell that it has already processed this message and return another confirmation. Confirmations will also have to reference the message number so that the sender can tell which message is being confirmed.

    16. Question 16. Of The Socket? Does Doing A Connect() Call Affect The Receive Behavior?

      Answer :

      • Yes, in two ways. First, only datagrams from your "connected peer" are returned. All others arriving at your port are not delivered to you.
      • But most importantly, a UDP socket must be connected to receive ICMP errors.

    17. Question 17. When Should I Use Udp Instead Of Tcp?

      Answer :

      • UDP is good for sending messages from one system to another when the order isn't important and you don't need all of the messages to get to the other machine. This is why I've only used UDP once to write the example code for the faq. Usually TCP is a better solution. It saves you having to write code to ensure that messages make it to the desired destination, or to ensure the message ordering. Keep in mind that every additional line of code you add to your project in another line that could contain a potentially expensive bug.
      • If you find that TCP is too slow for your needs you may be able to get better performance with UDP so long as you are willing to sacrifice message order and/or reliability.
      • UDP must be used to multicast messages to more than one other machine at the same time. With TCP an application would have to open separate connections to each of the destination machines and send the message once to each target machine. This limits your application to only communicate with machines that it already knows about.

    18. Question 18. How Can I Read Only One Character At A Time?

      Answer :

      This question is usually asked by people who are testing their server with telnet, and want it to process their keystrokes one character at a time. The correct technique is to use a psuedo terminal (pty). More on that in a minute.

      You can have your server send a sequence of control characters: 0xff 0xfb 0x01 0xff 0xfb 0x03 0xff 0xfd 0x0f3, which translates to IAC WILL ECHO IAC WILL SUPPRESS-GO-AHEAD IAC DO SUPPRESS-GO-AHEAD. For more information on what this means, check out std8, std28 and std29.

      Roger also gave the following tips:

      • This code will suppress echo, so you'll have to send the characters the user types back to the client if you want the user to see them.
      • Carriage returns will be followed by a null character, so you'll have to expect them.
      • If you get a 0xff, it will be followed by two more characters. These are telnet escapes.

      Use of a pty would also be the correct way to execute a child process and pass the i/o to a socket.

    19. Question 19. What Is The Difference Between So_reuseaddr And So_reuseport?

      Answer :

      SO_REUSEADDR allows your server to bind to an address which is in a TIME_WAIT state. It does not allow more than one server to bind to the same address. It was mentioned that use of this flag can create a security risk because another server can bind to a the same port, by binding to a specific address as opposed to INADDR_ANY. The SO_REUSEPORT flag allows multiple processes to bind to the same address provided all of them use the SO_REUSEPORT option.

      This is a newer flag that appeared in the 4.4BSD multicasting code (although that code was from elsewhere, so I am not sure just who invented the new SO_REUSEPORT flag).

      What this flag lets you do is rebind a port that is already in use, but only if all users of the port specify the flag. I believe the intent is for multicasting apps, since if you're running the same app on a host, all need to bind the same port. But the flag may have other uses. For example the following is from a post in February.

      SO_REUSEPORT is also useful for eliminating the try-10-times-to-bind hack in ftpd's data connection setup routine. Without SO_REUSEPORT, only one ftpd thread can bind to TCP (lhost, lport, INADDR_ANY, 0) in preparation for connecting back to the client. Under conditions of heavy load, there are more threads colliding here than the try-10-times hack can accomodate. With SO_REUSEPORT, things work nicely and the hack becomes unnecessary.

      I have also heard that DEC OSF supports the flag. Also note that under 4.4BSD, if you are binding a multicast address, then SO_REUSEADDR is condisered the same as SO_REUSEPORT (p. 731 of "TCP/IP Illustrated, Volume 2"). I think under Solaris you just replace SO_REUSEPORT with SO_REUSEADDR.

      From a later Stevens posting, with minor editing:

      Basically SO_REUSEPORT is a BSD'ism that arose when multicasting was added, even thought it was not used in the original Steve Deering code. I believe some BSD-derived systems may also include it (OSF, now Digital Unix, perhaps?). SO_REUSEPORT lets you bind the same address *and* port, but only if all the binders have specified it. But when binding a multicast address (its main use), SO_REUSEADDR is considered identical to SO_REUSEPORT (p. 731, "TCP/IP Illustrated, Volume 2"). So for portability of multicasting applications I always use SO_REUSEADDR.

    20. Question 20. How Do I Get My Server To Find Out The Clients Address / Host- Name?

      Answer :

      After accept()ing a connection, use getpeername() to get the address of the client. The client's address is of course, also returned on the accept(), but it is essential to initialise the address-length parameter before the accept call for this will work. int t;

      int len;

      struct sockaddr_in sin;

      struct hostent *host;

      len = sizeof sin;

      if (getpeername(t, (struct sockaddr *)

      &sin, &len) < 0)


      else {

      if ((host = gethostbyaddr((char *)

      &sin.sin_addr,sizeof sin.sin_addr,

      AF_INET)) == NULL)


      else printf("remote host is '%s'n", 



    21. Question 21. What Exactly Does So_keepalive Do?

      Answer :

      The SO_KEEPALIVE option causes a packet (called a 'keepalive probe') to be sent to the remote system if a long time (by default, more than 2 hours) passes with no other data being sent or received. This packet is designed to provoke an ACK response from the peer. This enables detection of a peer which has become unreachable (e.g. powered off or disconnected from the net).

      Note that the figure of 2 hours comes from RFC1122, "Requirements for Internet Hosts". The precise value should be configurable, but I've often found this to be difficult. The only implementation I know of that allows the keepalive interval to be set per-connection is SVR4.2.

    22. Question 22. What Exactly Does So_reuseaddr Do?

      Answer :

      This socket option tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely.

      It has been pointed out that "A socket is a 5 tuple (proto, local addr, local port, remote addr, remote port). SO_REUSEADDR just says that you can reuse local addresses. The 5 tuple still must be unique!" by Michael Hunter (mphunter@qnx.com). This is true, and this is why it is very unlikely that unexpected data will ever be seen by your server. The danger is that such a 5 tuple is still floating around on the net, and while it is bouncing around, a new connection from the same client, on the same system, happens to get the same remote port. This is explained by Richard Stevens in ``2.7 Please explain the TIME_WAIT state.''.

    23. Question 23. How Can I Make My Server A Daemon?

      Answer :

      There are two approaches you can take here. The first is to use inetd to do all the hard work for you. The second is to do all the hard work yourself. If you use inetd, you simply use stdin, stdout, or stderr for your socket. (These three are all created with dup() from the real socket) You can use these as you would a socket in your code. The inetd process will even close the socket for you when you are done.

      #include <stdio.h>

      #include <stdlib.h>

      #include <ctype.h>

      #include <unistd.h>

      #include <fcntl.h>

      #include <signal.h>

      #include <sys/wait.h>

      /* Global variables */

      volatile sig_atomic_t keep_going = 1;

      /* controls program termination */

      /* Function prototypes: */

      void termination_handler (int signum);

      /* clean up before termination */


      main (void)



      if (chdir (HOME_DIR)) 

      /* change to directory containing data

      files */


      fprintf (stderr, "`%s': ", HOME_DIR);

      perror (NULL);

      exit (1);


      /* Become a daemon: */

      switch (fork ())


      case -1: 

      /* can't fork */

      perror ("fork()");

      exit (3);

      case 0: 

      /* child, process becomes a daemon: */

      close (STDIN_FILENO);

      close (STDOUT_FILENO);

      close (STDERR_FILENO);

      if (setsid () == -1) 

      /* request a new session (job control) */


      exit (4);




      /* parent returns to calling process: */

      return 0;


      /* Establish signal handler to 

      clean up before termination: */

      if (signal (SIGTERM, termination_handler) 

      == SIG_IGN)

      signal (SIGTERM, SIG_IGN);

      signal (SIGINT, SIG_IGN);

      signal (SIGHUP, SIG_IGN);

      /* Main program loop */

      while (keep_going)




      return 0;



      termination_handler (int signum)


      keep_going = 0;

      signal (signum, termination_handler);


    24. Question 24. How Come I Get Address Already In Use From Bind()?

      Answer :

      You get this when the address is already in use. (Oh, you figured that much out?) The most common reason for this is that you have stopped your server, and then re-started it right away. The sockets that were used by the first incarnation of the server are still active. This is further explained in ``2.7 Please explain the TIME_WAIT state.'', and ``2.5 How do I properly close a socket''.

    25. Question 25. Why Do I Get Connection Refused When The Server Is Not Running?

      Answer :

      The connect() call will only block while it is waiting to establish a connection. When there is no server waiting at the other end, it gets notified that the connection can not be established, and gives up with the error message you see. This is a good thing, since if it were not the case clients might wait for ever for a service which just doesn't exist. Users would think that they were only waiting for the connection to be established, and then after a while give up, muttering something about crummy software under their breath.

    26. Question 26. How Can I Set The Timeout For The Connect() System Call?

      Answer :

      • Normally you cannot change this. Solaris does let you do this, on a per-kernel basis with the ndd tcp_ip_abort_cinterval parameter.
      • The easiest way to shorten the connect time is with an alarm() around the call to connect(). A harder way is to use select(), after setting the socket nonblocking. Also notice that you can only shorten the connect time, there's normally no way to lengthen it.
      • First, create the socket and put it into non-blocking mode, then call connect().

      There are three possibilities:

      • connect succeeds: the connection has been successfully made (this usually only happens when connecting to the same machine)
      • connect fails: obvious
      • connect returns: -1/EINPROGRESS. The connection attempt has begun, but not yet completed.

      If the connection succeeds:

      The socket will select() as writable (and will also select as readable if data arrives)

      If the connection fails:

      The socket will select as readable and writable, but either a read or write will return the error code from the connection attempt. Also, you can use getsockopt(SO_ERROR) to get the error status - but be careful; some systems return the error code in the result parameter of getsockopt, but others (incorrectly) cause the getsockopt call itself to fail with the stored value as the error.

    27. Question 27. Why Does Connect() Succeed Even Before My Server Did An Accept()?

      Answer :

      Once you have done a listen() call on your socket, the kernel is primed to accept connections on it. The usual UNIX implementation of this works by immediately completing the SYN handshake for any incoming valid SYN segments (connection attempts), creating the socket for the new connection, and keeping this new socket on an internal queue ready for the accept() call. So the socket is fully open before the accept is done.

      The other factor in this is the 'backlog' parameter for listen(); that defines how many of these completed connections can be queued at one time. If the specified number is exceeded, then new incoming connects are simply ignored (which causes them to be retried).

    28. Question 28. How Do I Convert A String Into An Internet Address?

      Answer :

      If you are reading a host's address from the command line, you may not know if you have an aaa.bbb.ccc.ddd style address, or a host.domain.com style address. What I do with these, is first try to use it as a aaa.bbb.ccc.ddd type address, and if that fails, then do a name lookup on it. Here is an example.

      /* Converts ascii text to in_addr struct.

      /* NULL is returned if the

      address can not be found. */

      struct in_addr *atoaddr(char *address) {

      struct hostent *host;

      static struct in_addr saddr;

      /* First try it as aaa.bbb.ccc.ddd. */

      saddr.s_addr = inet_addr(address);

      if (saddr.s_addr != -1) {

      return &saddr;


      host = gethostbyname(address);

      if (host != NULL) {

      return (struct in_addr *) *host->h_addr_list;


      return NULL;


    29. Question 29. What Are Socket Exceptions? What Is Out-of-band Data?

      Answer :

      Unlike exceptions in C++, socket exceptions do not indicate that an error has occured. Socket exceptions usually refer to the notification that out-of-band data has arrived. Out-of-band data (called "urgent data" in TCP) looks to the application like a separate stream of data from the main data stream. This can be useful for separating two different kinds of data. Note that just because it is called "urgent data" does not mean that it will be delivered any faster, or with higher priorety than data in the in-band data stream. Also beware that unlike the main data stream, the out-of-bound data may be lost if your application can't keep up with it.

    30. Question 30. Why Do I Keep Getting Eintr From The Socket Calls?

      Answer :

      This isn't really so much an error as an exit condition. It means that the call was interrupted by a signal. Any call that might block should be wrapped in a loop that checkes for EINTR, as is done in the example code .

    31. Question 31. Is There Any Advantage To Handling The Signal, Rather Than Just Ignoring It And Checking For The Epipe Error? Are There Any Useful Parameters Passed To The Signal Catching Function?

      Answer :

      • See that send()/write() can generate SIGPIPE. Is there any advantage to handling the signal, rather than just ignoring it and checking for the EPIPE error. Are there any useful parameters passed to the signal catching function.
      • In general, the only parameter passed to a signal handler is the signal number that caused it to be invoked. Some systems have optional additional parameters, but they are no use to you in this case.
      • My advice is to just ignore SIGPIPE as you suggest. That's what I do in just about all of my socket code; errno values are easier to handle than signals (in fact, the first revision of the FAQ failed to mention SIGPIPE in that context; I'd got so used to ignoring it...)
      • There is one situation where you should not ignore SIGPIPE; if you are going to exec() another program with stdout redirected to a socket. In this case it is probably wise to set SIGPIPE to SIG_DFL before doing the exec().

    32. Question 32. How Do I Send [this] Over A Socket?

      Answer :

      Anything other than single bytes of data will probably get mangled unless you take care. For integer values you can use htons() and friends, and strings are really just a bunch of single bytes, so those should be OK. Be careful not to send a pointer to a string though, since the pointer will be meaningless on another machine. If you need to send a struct, you should write sendthisstruct() and readthisstruct() functions for it that do all the work of taking the structure apart on one side, and putting it back together on the other. If you need to send floats, you may have a lot of work ahead of you. You should read RFC 1014 which is about portable ways of getting data from one machine to another (thanks to Andrew Gabriel for pointing this out).

    33. Question 33. How Come Select Says There Is Data, But Read Returns Zero?

      Answer :

      The data that causes select to return is the EOF because the other side has closed the connection. This causes read to return zero.

    34. Question 34. How Can I Force A Socket To Send The Data In Its Buffer?

      Answer :

      • You can't force it. Period. TCP makes up its own mind as to when it can send data. Now, normally when you call write() on a TCP socket, TCP will indeed send a segment, but there's no guarantee and no way to force this. There are lots of reasons why TCP will not send a segment: a closed window and the Nagle algorithm are two things to come immediately to mind.
      • Setting this only disables one of the many tests, the Nagle algorithm. But if the original poster's problem is this, then setting this socket option will help.
      • A quick glance at tcp_output() shows around 11 tests TCP has to make as to whether to send a segment or not.
      • As you've surmised, I've never had any problem with disabling Nagle's algorithm. Its basically a buffering method; there's a fixed overhead for all packets, no matter how small. Hence, Nagle's algorithm collects small packets together (no more than .2sec delay) and thereby reduces the amount of overhead bytes being transferred. This approach works well for rcp, for example: the .2 second delay isn't humanly noticeable, and multiple users have their small packets more efficiently transferred. Helps in university settings where most folks using the network are using standard tools such as rcp and ftp, and programs such as telnet may use it, too.
      • However, Nagle's algorithm is pure havoc for real-time control and not much better for keystroke interactive applications (control-C, anyone?). It has seemed to me that the types of new programs using sockets that people write usually do have problems with small packet delays. One way to bypass Nagle's algorithm selectively is to use "out-of-band" messaging, but that is limited in its content and has other effects (such as a loss of sequentiality) (by the way, out-of- band is often used for that ctrl-C, too).
      • So to sum it all up, if you are having trouble and need to flush the socket, setting the TCP_NODELAY option will usually solve the problem. If it doesn't, you will have to use out-of-band messaging, but according to Andrew, "out-of-band data has its own problems, and I don't think it works well as a solution to buffering delays (haven't tried it though). It is not 'expedited data' in the sense that exists in some other protocols; it is transmitted in-stream, but with a pointer to indicate where it is."

    35. Question 35. What Are The Pros/cons Of Select(), Non-blocking I/o And Sigio?

      Answer :

      Using non-blocking I/O means that you have to poll sockets to see if there is data to be read from them. Polling should usually be avoided since it uses more CPU time than other techniques.

      Using SIGIO allows your application to do what it does and have the operating system tell it (with a signal) that there is data waiting for it on a socket. The only drawback to this soltion is that it can be confusing, and if you are dealing with multiple sockets you will have to do a select() anyway to find out which one(s) is ready to be read.

      Using select() is great if your application has to accept data from more than one socket at a time since it will block until any one of a number of sockets is ready with data. One other advantage to select() is that you can set a time-out value after which control will be returned to you whether any of the sockets have data for you or not.

    36. Question 36. Why Does It Take So Long To Detect That The Peer Died?

      Answer :

      • Because by default, no packets are sent on the TCP connection unless there is data to send or acknowledge.
      • So, if you are simply waiting for data from the peer, there is no way to tell if the peer has silently gone away, or just isn't ready to send any more data yet. This can be a problem (especially if the peer is a PC, and the user just hits the Big Switch...).
      • One solution is to use the SO_KEEPALIVE option. This option enables periodic probing of the connection to ensure that the peer is still present. BE WARNED: the default timeout for this option is AT LEAST 2 HOURS. This timeout can often be altered (in a system-dependent fashion) but not normally on a per-connection basis (AFAIK).
      • RFC1122 specifies that this timeout (if it exists) must be configurable. On the majority of Unix variants, this configuration may only be done globally, affecting all TCP connections which have keepalive enabled. The method of changing the value, moreover, is often difficult and/or poorly documented, and in any case is different for just about every version in existence.
      • If you must change the value, look for something resembling tcp_keepidle in your kernel configuration or network options configuration.
      • If you're sending to the peer, though, you have some better guarantees; since sending data implies receiving ACKs from the peer, then you will know after the retransmit timeout whether the peer is still alive. But the retransmit timeout is designed to allow for various contingencies, with the intention that TCP connections are not dropped simply as a result of minor network upsets. So you should still expect a delay of several minutes before getting notification of the failure.
      • The approach taken by most application protocols currently in use on the Internet (e.g. FTP, SMTP etc.) is to implement read timeouts on the server end; the server simply gives up on the client if no requests are received in a given time period (often of the order of 15 minutes).

      Protocols where the connection is maintained even if idle for long periods have two choices:

      1. use SO_KEEPALIVE
      2. use a higher-level keepalive mechanism (such as sending a null request to the server every so often).

    37. Question 37. Why Do I Get Eproto From Read()?

      Answer :

      EPROTO means that the protocol encountered an unrecoverable error for that endpoint. EPROTO is one of those catch-all error codes used by STREAMS-based drivers when a better code isn't available.

      Not quite to do with EPROTO from read(), but I found out once that on some STREAMS-based implementations, EPROTO could be returned by accept() if the incoming connection was reset before the accept completes.

      On some other implementations, accept seemed to be capable of blocking if this occured. This is important, since if select() said the listening socket was readable, then you would normally expect not to block in the accept() call. The fix is, of course, to set nonblocking mode on the listening socket if you are going to use select() on it.

    38. Question 38. Where Can A Get A Library For Programming Sockets?

      Answer :

      There is the Simple Sockets Library by Charles E. Campbell, Jr. PhD. and Terry McRoberts. The file is called ssl.tar.gz, and you can download it from this faq's home page. For c++ there is the Socket++ library which is on ftp://ftp.virginia.edu/pub/socket++-1.10.tar.gz. There is also C++ Wrappers. The file is called ftp://ftp.huji.ac.il/pub/languages/C++/. Thanks to Bill McKinnon for tracking it down for me! From http://www.cs.wustl.edu/~schmidt you should be able to find the ACE toolkit. PING Software Group has some libraries that include a sockets interface among other things. You can find them at http://love.geology.yale.edu/~markl/ping.

    39. Question 39. Whats The Difference Between Select() And Poll()?

      Answer :

      The basic difference is that select()'s fd_set is a bit mask and therefore has some fixed size. It would be possible for the kernel to not limit this size when the kernel is compiled, allowing the application to define FD_SETSIZE to whatever it wants (as the comments in the system header imply today) but it takes more work. 4.4BSD's kernel and the Solaris library function both have this limit. But I see that BSD/OS 2.1 has now been coded to avoid this limit, so it's doable, just a small matter of programming. :-) Someone should file a Solaris bug report on this, and see if it ever gets fixed.

      With poll(), however, the user must allocate an array of pollfd structures, and pass the number of entries in this array, so there's no fundamental limit. As Casper notes, fewer systems have poll() than select, so the latter is more portable. Also, with original implementations (SVR3) you could not set the descriptor to -1 to tell the kernel to ignore an entry in the pollfd structure, which made it hard to remove entries from the array; SVR4 gets around this. Personally, I always use select() and rarely poll(), because I port my code to BSD environments too. Someone could write an implementation of poll() that uses select(), for these environments, but I've never seen one. Both select() and poll() are being standardized by POSIX 1003.1g.

    40. Question 40. How Do I Use Tcp_no Delay?

      Answer :

      First off, be sure you really want to use it in the first place. It will disable the Nagle algorithm (see ``2.11 How can I force a socket to send the data in its buffer?''), which will cause network traffic to increase, with smaller than needed packets wasting bandwidth. Also, from what I have been able to tell, the speed increase is very small, so you should probably do it without TCP_NODELAY first, and only turn it on if there is a problem.

      Here is a code example, 

      with a warning about using it 

      int flag = 1;

      int result = setsockopt(sock,

      /* socket affected */


      /* set option at TCP level */


      /* name of option */

      (char *) &flag,

      /* the cast is historical cruft */


      /* length of option value */

      if (result < 0)

      ... handle the error ...

      TCP_NODELAY is for a specific purpose; to disable the Nagle buffering algorithm. It should only be set for applications that send frequent small bursts of information without getting an immediate response, where timely delivery of data is required (the canonical example is mouse movements).

    41. Question 41. What Is The Difference Between Read() And Recv()?

      Answer :

      read() is equivalent to recv() with a flags parameter of 0. Other values for the flags parameter change the behaviour of recv(). Similarly, write() is equivalent to send() with flags == 0.

      It is unlikely that send()/recv() would be dropped; perhaps someone with a copy of the POSIX drafts for socket calls can check...

      Portability note: non-unix systems may not allow read()/write() on sockets, but recv()/send() are usually ok. This is true on Windows and OS/2, for example.

    42. Question 42. After The Chroot(), Calls To Socket() Are Failing. Why?

      Answer :

      • On systems where sockets are implemented on top of Streams (e.g. all SysV-based systems, presumably including Solaris), the socket() function will actually be opening certain special files in /dev. You will need to create a /dev directory under your fake root and populate it with the required device nodes (only).
      • Your system documentation may or may not specify exactly which device nodes are required; suggested checking the man page for ftpd, which should list the files you need to copy and devices you need to create in the chroot'd environment.)
      • A less-obvious issue with chroot() is if you call syslog(), as many daemons do; syslog() opens (depending on the system) either a UDP socket, a FIFO or a Unix-domain socket. So if you use it after a chroot() call, make sure that you call openlog() before the chroot.

    43. Question 43. When Will My Application Receive Sigpipe?

      Answer :

      Very simple: with TCP you get SIGPIPE if your end of the connection has received an RST from the other end. What this also means is that if you were using select instead of write, the select would have indicated the socket as being readable, since the RST is there for you to read (read will return an error with errno set to ECONNRESET).

      Basically an RST is TCP's response to some packet that it doesn't expect and has no other way of dealing with. A common case is when the peer closes the connection (sending you a FIN) but you ignore it because you're writing and not reading. (You should be using select.) So you write to a connection that has been closed by the other end and the other end's TCP responds with an RST.

    44. Question 44. How Can I Find The Full Hostname (fqdn) Of The System I Am Running On?

      Answer :

      Some systems set the hostname to the FQDN and others set it to just the unqualified host name. I know the current BIND FAQ recommends the FQDN, but most Solaris systems, for example, tend to use only the unqualified host name.

      Regardless, the way around this is to first get the host's name (perhaps an FQDN, perhaps unaualified). Most systems support the Posix way to do this using uname(), but older BSD systems only provide gethostname(). Call gethostbyname() to find your IP address. Then take the IP address and call gethostbyaddr(). The h_name member of the hostent{} should then be your FQDN.

    45. Question 45. How Can My Client Work Through A Firewall/proxy Server?

      Answer :

      If you are running through separate proxies for each service, you shouldn't need to do anything. If you are working through socked, you will need to "sissify" your application.

    46. Question 46. Why Do I Sometimes Lose A Servers Address When Using More Than One Server?

      Answer :

      Take a careful look at struct hostent. Notice that almost everything in it is a pointer? All these pointers will refer to statically allocated data.

      For example, if you do:

      struct hostent *host = gethostbyname(hostname);

      then (as you should know) a subsequent call to gethostbyname() will overwrite the structure pointed to by 'host'.

      But if you do:

      struct hostent myhost;

      struct hostent *hostptr = gethostbyname(hostname);

      if (hostptr) myhost = *host;

      to make a copy of the hostent before it gets overwritten, then it still gets clobbered by a subsequent call to gethostbyname(), since although myhost won't get overwritten, all the data it is pointing to will be.

      You can get round this by doing a proper 'deep copy' of the hostent structure, but this is tedious. My recommendation would be to extract the needed fields of the hostent and store them in your own way.

      It might be nice if you mention MT safe libraries provide complimentary functions for multithreaded programming. On the solaris machine I'm typing at, we have gethostbyname and gethostbyname_r (_r for reentrant). The main difference is, you provide the storage for the hostent struct so you always have a local copy and not just a pointer to the static copy.

    47. Question 47. System Choose One For Me On The Connect() Call? Should I Bind() A Port Number In My Client Program, Or Let The?

      Answer :

      Let the system choose your client's port number: 

      • The exception to this, is if the server has been written to be picky about what client ports it will allow connections from. Rlogind and rshd are the classic examples. This is usually part of a Unix-specific (and rather weak) authentication scheme; the intent is that the server allows connections only from processes with root privilege. (The weakness in the scheme is that many O/Ss (e.g. MS-DOS) allow anyone to bind any port.)
      • The rresvport() routine exists to help out clients that are using this scheme. It basically does the equivalent of socket() + bind(), choosing a port number in the range 512..1023.
      • If the server is not fussy about the client's port number, then don't try and assign it yourself in the client, just let connect() pick it for you.
      • If, in a client, you use the naive scheme of starting at a fixed port number and calling bind() on consecutive values until it works, then you buy yourself a whole lot of trouble:
      • The problem is if the server end of your connection does an active close. (E.G. client sends 'QUIT' command to server, server responds by closing the connection). That leaves the client end of the connection in CLOSED state, and the server end in TIME_WAIT state. So after the client exits, there is no trace of the connection on the client end.
      • Now run the client again. It will pick the same port number, since as far as it can see, it's free. But as soon as it calls connect(), the server finds that you are trying to duplicate an existing connection (although one in TIME_WAIT). It is perfectly entitled to refuse to do this, so you get, I suspect, ECONNREFUSED from connect(). (Some systems may sometimes allow the connection anyway, but you can't rely on it.)
      • This problem is especially dangerous because it doesn't show up unless the client and server are on different machines. (If they are the same machine, then the client won't pick the same port number as
      • before). So you can get bitten well into the development cycle (if you do what I suspect most people do, and test client & server on the same box initially).
      • Even if your protocol has the client closing first, there are still ways to produce this problem (e.g. kill the server).

    48. Question 48. Over The Socket? Is There A Way To Have A Dynamic Buffer? What Does One Do When One Does Not Know How Much Information Is Coming?

      Answer :

      When the size of the incoming data is unknown, you can either make the size of the buffer as big as the largest possible (or likely) buffer, or you can re-size the buffer on the fly during your read. When you malloc() a large buffer, most (if not all) varients of unix will only allocate address space, but not physical pages of ram. As more and more of the buffer is used, the kernel allocates physical memory. This means that malloc'ing a large buffer will not waste resources unless that memory is used, and so it is perfectly acceptable to ask for a meg of ram when you expect only a few K.

      On the other hand, a more elegant solution that does not depend on the inner workings of the kernel is to use realloc() to expand the buffer as required in say 4K chunks (since 4K is the size of a page of ram on most systems). I may add something like this to sockhelp.c in the example code one day.

    49. Question 49. Why Do Not My Sockets Close?

      Answer :

      When you issue the close() system call, you are closing your interface to the socket, not the socket itself. It is up to the kernel to close the socket. Sometimes, for really technical reasons, the socket is kept alive for a few minutes after you close it. It is normal, for example for the socket to go into a TIME_WAIT state, on the server side, for a few minutes. People have reported ranges from 20 seconds to 4 minutes to me. The official standard says that it should be 4 minutes. On my Linux system it is about 2 minutes. This is explained in great detail in ``2.7 Please explain the TIME_WAIT state.''.

    50. Question 50. How Can I Listen On More Than One Port At A Time?

      Answer :

      The best way to do this is with the select() call. This tells the kernel to let you know when a socket is available for use. You can have one process do i/o with multiple sockets with this call.

      If you want to wait for a connect on sockets 4, 6 and 10 you might execute the following code snippet:

      fd_set socklist;

      FD_ZERO(&socklist); /* Always clear the structure first. */

      FD_SET(4, &socklist);

      FD_SET(6, &socklist);

      FD_SET(10, &socklist);

      if (select(11, NULL, &socklist, NULL, NULL) < 0)


      The kernel will notify us as soon as a file descriptor which is less than 11 (the first parameter to select()), and is a member of our socklist becomes available for writing. See the man page on select() for more details.

    51. Question 51. What Exactly Does So_linger Do?

      Answer :

      On some unixes this does nothing. On others, it instructs the kernel to abort tcp connections instead of closing them properly. This can be dangerous.

    52. Question 52. How Can I Bind() To A Port Number < 1024?

      Answer :

      • The restriction on access to ports < 1024 is part of a (fairly weak) security scheme particular to UNIX. The intention is that servers (for example rlogind, rshd) can check the port number of the client, and if it is < 1024, assume the request has been properly authorized at the client end.
      • The practical upshot of this, is that binding a port number < 1024 is reserved to processes having an effective UID == root.
      • This can, occasionally, itself present a security problem, e.g. when a server process needs to bind a well-known port, but does not itself need root access (news servers, for example). This is often solved by creating a small program which simply binds the socket, then restores the real userid and exec()s the real server. This program can then be made setuid root.

    53. Question 53. How Should I Choose A Port Number For My Server?

      Answer :

      The list of registered port assignments can be found in STD 2 or RFC 1700. Choose one that isn't already registered, and isn't in /etc/services on your system. It is also a good idea to let users customize the port number in case of conflicts with other un- registered port numbers in other servers. The best way of doing this is hardcoding a service name, and using getservbyname() to lookup the actual port number. This method allows users to change the port your server binds to by simply editing the /etc/services file.

    54. Question 54. How Can I Write A Multi-homed Server?

      Answer :

      • I want to run a server on a multi-homed host. The host is part of two networks and has two ethernet cards. I want to run a server on this machine, binding to a pre-determined port number. I want clients on either subnet to be able to send broadcast packates to the port and have the server receive them.
      • Your first question in this scenario is, do you need to know which subnet the packet came from? I'm not at all sure that this can be reliably determined in all cases.
      • If you don't really care, then all you need is one socket bound to INADDR_ANY. That simplifies things greatly.
      • If you do care, then you have to bind multiple sockets. You are obviously attempting to do this in your code as posted, so I'll assume you do.
      • I was hoping that something like the following would work. Will it? This is on Sparcs running Solaris 2.4/2.5.
      • I don't have access to Solaris, but I'll comment based on my experience with other Unixes.
      • What you are doing is attempting to bind all the current hosts unicast addresses as listed in hosts/NIS/DNS. This may or may not reflect reality, but much more importantly, neglects the broadcast addresses. It seems to be the case in the majority of implementations that a socket bound to a unicast address will not see incoming packets with broadcast addresses as their destinations.
      • The approach I've taken is to use SIOCGIFCONF to retrieve the list of active network interfaces, and SIOCGIFFLAGS and SIOCGIFBRDADDR to identify broadcastable interfaces and get the broadcast addresses. Then I bind to each unicast address, each broadcast address, and to INADDR_ANY as well. That last is necessary to catch packets that are on the wire with INADDR_BROADCAST in the destination. (SO_REUSEADDR is necessary to bind INADDR_ANY as well as the specific addresses.)

      This gives me very nearly what I want. The wrinkles are:

      • I don't assume that getting a packet through a particular socket necessarily means that it actually arrived on that interface.
      • I can't tell anything about which subnet a packet originated on if its destination was INADDR_BROADCAST.
      • On some stacks, apparently only those with multicast support, I get duplicate incoming messages on the INADDR_ANY socket.

    55. Question 55. What Is The Difference Between Connected And Unconnected Sockets?

      Answer :

      If a UDP socket is unconnected, which is the normal state after a bind() call, then send() or write() are not allowed, since no destination address is available; only send to() can be used to send data.

      Calling connect() on the socket simply records the specified address and port number as being the desired communications partner. That means that send() or write() are now allowed; they use the destination address and port given on the connect call as the destination of the packet.

    56. Question 56. How Can I Read Icmp Errors From Connected Udp Sockets?

      Answer :

      If the target machine discards the message because there is no process reading on the requested port number, it sends an ICMP message to your machine which will cause the next system call on the socket to return ECONNREFUSED. Since delivery of ICMP messages is not guarenteed you may not recieve this notification on the first transaction.

    57. Question 57. How Can I Be Sure That Udp Messages Are Received In Order?

      Answer :

      You can't. What you can do is make sure that messages are processed in order by using a numbering system as mentioned in ``5.5 How can I be sure that a UDP message is received?''. If you need your messages to be received and be received in order you should really consider switching to TCP. It is unlikely that you will be able to do a better job implementing this sort of protocol than the TCP people already have, without a significant investment of time.

    58. Question 58. How Come Only The First Part Of My Data Gram Is Getting Through?

      Answer :

      This has to do with the maximum size of a datagram on the two machines involved. This depends on the sytems involved, and the MTU (Maximum Transmission Unit). According to "UNIX Network Programming", all TCP/IP implementations must support a minimum IP datagram size of 576 bytes, regardless of the MTU. Assuming a 20 byte IP header and 8 byte UDP header, this leaves 548 bytes as a safe maximum size for UDP messages. The maximum size is 65516 bytes. Some platforms support IP fragmentation which will allow datagrams to be broken up (because of MTU values) and then re-assembled on the other end, but not all implementations support this.

      Another issue is fragmentation. If a datagram is sent which is too large for the network interface it is sent through, then the sending host will fragment it into smaller packets which are reassembled by the receiving host. Also, if there are intervening routers, then they may also need to fragment the packet(s), which greatly increases the chances of losing one or more fragments (which causes the entire datagram to be dropped). Thus, large UDP datagrams should be avoided for applications that are likely to operate over routed nets or the Internet proper.

    59. Question 59. What Is Lilo?

      Answer :

      LILO is Linux Loader is a boot loader for Linux. It is used to load Linux into the memory and start the Operating system. LILO can be configured to boot other operating systems as well. LILO is customizable, which means that if the default configuration is not correct, it can be changed. Config file for LILO is lilo.conf.

    60. Question 60. What Is Mac, And How Is It Different From Pc? Which One Is Better For Professional Users, And Why?

      Answer :

      • Mac OS is the trademarked name for a series of graphical user interface-based operating systems developed by Apple Inc. (formerly Apple Computer, Inc.) for their Macintosh line of computer systems. 
      • PC is Personal computer, a computer whose original sales price, size, and capabilities make it useful for individuals. 
      • Mac OS works on PC. So both are different.. BOth are counter part of each other. Mac is Software and PC is hardware.

    61. Question 61. What Are Raw Sockets?

      Answer :

      A raw socket is a socket that allows access to packet headers on incoming and outgoing packets. Raw sockets always receive the packets with the packet header included as opposed to non-raw sockets which strip the header and only receive the packet payload.

Popular Interview Questions

All Interview Questions

Unix Socket Practice Test

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Unix socket Tutorial