Linux Debugging Questions & Answers – Internet Domain Socket System Calls

This set of Linux Debugging questions and answers focuses on Internet Domain Socket System Calls.

1. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/types.h>
  3.    #include<sys/socket.h>
  4.  
  5.    int main()
  6.    {
  7.        struct sockaddr_in addr;
  8.        int fd;
  9.        fd = socket(AF_INET,SOCK_STREAM,0);
  10.        printf("%d\n",fd);
  11.        return 0;
  12.    }

a) -1
b) 3
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: The header file netinet/in.h is required to use the structure sockaddr_in.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:7:21: error: storage size of ‘addr’ isn’t known
[root@localhost sanfoundry]#

2. What is the output of this program?

advertisement
advertisement
  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<netinet/in.h>
  4.    #include<sys/types.h>
  5.    #include<sys/socket.h>
  6.  
  7.    int main()
  8.    {
  9.        int fd_server, fd_client, len, len_client;
  10.        struct sockaddr_in add_server, add_client;
  11.        char buff[10];
  12.        fd_server = socket(AF_INET,SOCK_STREAM,0);
  13.        if (fd_server == -1)
  14.        {
  15.            perror("fd_sock");
  16.            exit(1);   
  17.        }
  18.        len = sizeof(add_server);
  19.        len_client = sizeof(add_client);
  20.        if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0)
  21.            perror("bind");
  22.        fd_client = accept(fd_server,(struct sockaddr*)&add_client,len_client);
  23.        if(fd_client == -1)
  24.            perror("accept");
  25.        read(fd_client,buff,10);
  26.        return 0;
  27.    }

a) segmentation fault
b) error at the time of compilation
c) syntax error
d) none of the mentioned
View Answer

Answer: b
Explanation: The third argument of the accept is the type of pointer.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:26:39: warning: passing argument 3 of ‘accept’ makes pointer from integer without a cast [enabled by default] /usr/include/sys/socket.h:214:12: note: expected ‘socklen_t * __restrict__’ but argument is of type ‘int’
[root@localhost sanfoundry]#
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. This program can send the request to

  1.    #include<stdio.h>
  2.    #include<netinet/in.h>
  3.    #include<sys/types.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        int fd_client,fd, len;
  9.        struct sockaddr_in add_server;
  10.        fd_client = socket(AF_INET,SOCK_STREAM,0);
  11.        if (fd_client == -1)
  12.        {
  13.            perror("fd_sock");
  14.            exit(1);
  15.        }
  16.        add_server.sin_family = AF_INET;
  17.        add_server.sin_port = ntohs(4001);
  18.        add_server.sin_addr.s_addr = inet_addr("193.39.0.4");
  19.        len = sizeof(add_server);
  20.        fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
  21.        if(fd == -1)
  22.            perror("connect");
  23.        return 0;
  24.    }

a) the system having IP address 193.39.0.4
b) any system present in the network
c) any system of the private network
d) none of the mentioned
View Answer

Answer: a
Explanation: The IP address is mentioned in the proper element of the structure sockaddr_in
advertisement

4. This program is valid for

advertisement
  1.    #include<stdio.h>
  2.    #include<netinet/in.h>
  3.    #include<sys/types.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        int fd_client,fd, len;
  9.        struct sockaddr_in add_server;
  10.        fd_client = socket(AF_INET,SOCK_STREAM,0);
  11.        if (fd_client == -1)
  12.        {
  13.            perror("fd_sock");
  14.            exit(1);
  15.        }
  16.        add_server.sin_family = AF_INET;
  17.        add_server.sin_port = ntohs(4001);
  18.        add_server.sin_addr.s_addr = inet_addr("144.29.8.2");
  19.        len = sizeof(add_server);
  20.        fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
  21.        return 0;
  22.    }

a) IPv4
b) IPv6
c) both IPv4 and IPv6
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

5. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<sys/socket.h>
  3.    int main()
  4.    { 
  5.        int ret;
  6.        ret = shutdown(0,0);
  7.        printf("%d\n",ret);
  8.        return 0;
  9.    }

a) 0
b) -1
c) can not be determined
d) none of the mentioned
View Answer

Answer: b
Explanation: The shutdown() is used to close a socket and the first argument in shutdown() is socket.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
-1
[root@localhost sanfoundry]#

6. What is the problem with this server program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<netinet/in.h>
  4.    #include<sys/types.h>
  5.    #include<sys/socket.h>
  6.  
  7.    int main()
  8.    {
  9.        int fd_server, fd_client, len;
  10.        struct sockaddr_in add_server;
  11.        fd_server = socket(AF_INET,SOCK_STREAM,0);
  12.        if (fd_server == -1)
  13.        {
  14.            perror("fd_sock");
  15.            exit(1);
  16.        }
  17.        add_server.sin_family = AF_INET;
  18.        add_server.sin_port = htons(4001);
  19.        add_server.sin_addr.s_addr = inet_addr("122.23.1.1");
  20.        len = sizeof(add_server);
  21.        if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0)
  22.            perror("bind");
  23.        if(listen(fd_server,5) != 0)
  24.            perror("listen");
  25.        fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len);
  26.        if(fd_client == -1)
  27.            perror("accept");
  28.        return 0;
  29.    }

a) it can not accept the request of any client
b) it will give the segmentation fault
c) there is no problem with this program
d) none of the mentioned
View Answer

Answer: a
Explanation: The address format of the client must match in the server.

7. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<netinet/in.h>
  4.    #include<sys/types.h>
  5.    #include<sys/socket.h>
  6.  
  7.    int main()
  8.    {
  9.        int fd_server, fd_client, len, len_client;
  10.        struct sockaddr_in add_server;
  11.        fd_server = socket(AF_INET,SOCK_STREAM,0);
  12.        fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len);
  13.        if(fd_client == -1)
  14.            perror("accept");
  15.        if(listen(fd_server,5) != 0)
  16.            perror("listen");
  17.        return 0;
  18.    }

a) syntax error
b) error at the time of compilation
c) segmentation fault
d) none of the mentioned
View Answer

Answer: d
Explanation: The listen() must always be used before accept().
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
accept: Invalid argument
[root@localhost sanfoundry]#

8. What is the output of this program?

  1.    #include<stdio.h>
  2.    #include<stdlib.h>
  3.    #include<netinet/in.h>
  4.    #include<sys/types.h>
  5.    #include<sys/socket.h>
  6.  
  7.    int main()
  8.    {
  9.        int fd_server, fd_client, len, len_client;
  10.        struct sockaddr_in add_server;
  11.        fd_server = socket(AF_INET,SOCK_STREAM,0);
  12.        close(fd_server);
  13.            perror("accept");
  14.        if(listen(fd_server,5) != 0)
  15.            perror("listen");
  16.        fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len);
  17.            if(fd_client == -1)
  18.        return 0;
  19.    }

a) syntax error
b) error at the time of compilation
c) segmentation fault
d) none of the mentioned
View Answer

Answer: d
Explanation: The program will not work properly because the file descriptor is not available in the for listen() and accept().
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
accept: Success
listen: Bad file descriptor
[root@localhost sanfoundry]#

9. On which system call, this program (process) waits until the server responds?

  1.    #include<stdio.h>
  2.    #include<netinet/in.h>
  3.    #include<sys/types.h>
  4.    #include<sys/socket.h>
  5.  
  6.    int main()
  7.    {
  8.        int fd_client,fd, len;
  9.        struct sockaddr_in add_server;
  10.        fd_client = socket(AF_INET,SOCK_STREAM,0);
  11.        if (fd_client == -1){
  12.            perror("fd_sock");
  13.            exit(1);
  14.    }
  15.    add_server.sin_family = AF_INET;
  16.    add_server.sin_port = ntohs(4001);
  17.    add_server.sin_addr.s_addr = inet_addr("127.0.0.1");
  18.    len = sizeof(add_server);
  19.    fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
  20.    if(fd == -1)
  21.        perror("connect");
  22.        write(fd,"Hello\n",6);
  23.        return 0;
  24.    }

a) socket()
b) connect()
c) both socket() and connect()
d) none of the mentioned
View Answer

Answer: a
Explanation: None.

10. What is the the response of this server for this client if both programs are running on the same system?

  1.    /*This is server.c*/ 
  2.    #include<stdio.h>
  3.    #include<stdlib.h>
  4.    #include<netinet/in.h>
  5.    #include<sys/types.h>
  6.    #include<sys/socket.h>
  7.  
  8.    int main()
  9.    {
  10.        int fd_server, fd_client, len, len_client;
  11.        struct sockaddr_in add_server, add_client;
  12.        char buff[10];
  13.        fd_server = socket(AF_INET,SOCK_STREAM,0);
  14.        if (fd_server == -1)
  15.        {
  16.            perror("fd_sock");
  17.            exit(1);
  18.        }
  19.       add_server.sin_family = AF_INET;
  20.       add_server.sin_port = htons(4001);
  21.       add_server.sin_addr.s_addr = inet_addr("127.0.0.1");
  22.       len = sizeof(add_server);
  23.       len = sizeof(add_client);
  24.       if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0)
  25.        perror("bind");
  26.        if(listen(fd_server,5) != 0)
  27.            perror("listen");
  28.        fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len_client);
  29.        if(fd_client == -1)
  30.            perror("accept");
  31.        read(fd_client,buff,10);
  32.        return 0;
  33.    }
  34.    /*This is the client.c*/
  35.    #include<stdio.h>
  36.    #include<netinet/in.h>
  37.    #include<sys/types.h>
  38.    #include<sys/socket.h>
  39.  
  40.    int main()
  41.    {
  42.        int fd_client,fd, len;
  43.        struct sockaddr_in add_server;
  44.        fd_client = socket(AF_INET,SOCK_STREAM,0);
  45.        if (fd_client == -1)
  46.        {
  47.            perror("fd_sock");
  48.            exit(1);
  49.        }
  50.        add_server.sin_family = AF_INET;
  51.        add_server.sin_port = ntohs(4001);
  52.        add_server.sin_addr.s_addr = inet_addr("127.0.0.1");
  53.        len = sizeof(add_server);
  54.        fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
  55.        if(fd == -1)
  56.        perror("connect");
  57.        write(fd,"Hello\n",6);
  58.        return 0;
  59.    }

a) the server will write back to the client whatever the clinet will write to the server
b) the client server communication will not work
c) the response can not be determined
d) none of the mentioned
View Answer

Answer: a
Explanation: The loopback address is used as IP address in both the programs.

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.
Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.