Linux Debugging Questions & Answers – Internet Domain Socket System Calls

1.

What is the output of this program?


   #include
   #include
   #include
 
   int main()
   {
       struct sockaddr_in addr;
       int fd;
       fd = socket(AF_INET,SOCK_STREAM,0);
       printf("%d\n",fd);
       return 0;
   }

   A.) -1
   B.) 3
   C.) error
   D.) none of these

Answer: Option 'C'

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?


   #include
   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_server, fd_client, len, len_client;
       struct sockaddr_in add_server, add_client;
       char buff[10];
       fd_server = socket(AF_INET,SOCK_STREAM,0);
       if (fd_server == -1)
       {
           perror("fd_sock");
           exit(1);   
       }
       len = sizeof(add_server);
       len_client = sizeof(add_client);
       if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0)
           perror("bind");
       fd_client = accept(fd_server,(struct sockaddr*)&add_client,len_client);
       if(fd_client == -1)
           perror("accept");
       read(fd_client,buff,10);
       return 0;
   }

   A.) segmentation fault
   B.) error at the time of compilation
   C.) syntax error
   D.) none of these

Answer: Option 'B'

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]#

3.

This program can send the request to


   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_client,fd, len;
       struct sockaddr_in add_server;
       fd_client = socket(AF_INET,SOCK_STREAM,0);
       if (fd_client == -1)
       {
           perror("fd_sock");
           exit(1);
       }
       add_server.sin_family = AF_INET;
       add_server.sin_port = ntohs(4001);
       add_server.sin_addr.s_addr = inet_addr("193.39.0.4");
       len = sizeof(add_server);
       fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
       if(fd == -1)
           perror("connect");
       return 0;
   }

   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 these

Answer: Option 'A'

the system having IP address 193.39.0.4

4.

This program is valid for

   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_client,fd, len;
       struct sockaddr_in add_server;
       fd_client = socket(AF_INET,SOCK_STREAM,0);
       if (fd_client == -1)
       {
           perror("fd_sock");
           exit(1);
       }
       add_server.sin_family = AF_INET;
       add_server.sin_port = ntohs(4001);
       add_server.sin_addr.s_addr = inet_addr("144.29.8.2");
       len = sizeof(add_server);
       fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
       return 0;
   }

   A.) IPv4
   B.) IPv6
   C.) both IPv4 and IPv6
   D.) none of these

Answer: Option 'A'

IPv4

5.

What is the output of this program?

   #include
   #include
   int main()
   { 
       int ret;
       ret = shutdown(0,0);
       printf("%d\n",ret);
       return 0;
   }

   A.) 0
   B.) -1
   C.) can not be determined
   D.) none of these

Answer: Option 'B'

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?

   #include
   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_server, fd_client, len;
       struct sockaddr_in add_server;
       fd_server = socket(AF_INET,SOCK_STREAM,0);
       if (fd_server == -1)
       {
           perror("fd_sock");
           exit(1);
       }
       add_server.sin_family = AF_INET;
       add_server.sin_port = htons(4001);
       add_server.sin_addr.s_addr = inet_addr("122.23.1.1");
       len = sizeof(add_server);
       if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0)
           perror("bind");
       if(listen(fd_server,5) != 0)
           perror("listen");
       fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len);
       if(fd_client == -1)
           perror("accept");
       return 0;
   }

   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 these

Answer: Option 'A'

it can not accept the request of any client

7.

What is the output of this program?


   #include
   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_server, fd_client, len, len_client;
       struct sockaddr_in add_server;
       fd_server = socket(AF_INET,SOCK_STREAM,0);
       fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len);
       if(fd_client == -1)
           perror("accept");
       if(listen(fd_server,5) != 0)
           perror("listen");
       return 0;
   }

   A.) syntax error
   B.) error at the time of compilation
   C.) segmentation fault
   D.) none of these

Answer: Option 'D'

none of these

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?


   #include
   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_server, fd_client, len, len_client;
       struct sockaddr_in add_server;
       fd_server = socket(AF_INET,SOCK_STREAM,0);
       close(fd_server);
           perror("accept");
       if(listen(fd_server,5) != 0)
           perror("listen");
       fd_client = accept(fd_server,(struct sockaddr*)&add_server,&len);
           if(fd_client == -1)
       return 0;
   }

   A.) syntax error
   B.)

error at the time of compilation

   C.) segmentation fault
   D.) none of these

Answer: Option 'D'

none of these

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?


   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_client,fd, len;
       struct sockaddr_in add_server;
       fd_client = socket(AF_INET,SOCK_STREAM,0);
       if (fd_client == -1){
           perror("fd_sock");
           exit(1);
   }
   add_server.sin_family = AF_INET;
   add_server.sin_port = ntohs(4001);
   add_server.sin_addr.s_addr = inet_addr("127.0.0.1");
   len = sizeof(add_server);
   fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
   if(fd == -1)
       perror("connect");
       write(fd,"Hello\n",6);
       return 0;
   }

   A.) socket()
   B.) connect()
   C.) both socket() and connect()
   D.) none of these

Answer: Option 'A'

socket()

10.

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


   /*This is server.c*/ 
   #include
   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_server, fd_client, len, len_client;
       struct sockaddr_in add_server, add_client;
       char buff[10];
       fd_server = socket(AF_INET,SOCK_STREAM,0);
       if (fd_server == -1)
       {
           perror("fd_sock");
           exit(1);
       }
      add_server.sin_family = AF_INET;
      add_server.sin_port = htons(4001);
      add_server.sin_addr.s_addr = inet_addr("127.0.0.1");
      len = sizeof(add_server);
      len = sizeof(add_client);
      if( bind(fd_server,(struct sockaddr*)&add_server,len) != 0)
       perror("bind");
       if(listen(fd_server,5) != 0)
           perror("listen");
       fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len_client);
       if(fd_client == -1)
           perror("accept");
       read(fd_client,buff,10);
       return 0;
   }
   /*This is the client.c*/
   #include
   #include
   #include
   #include
 
   int main()
   {
       int fd_client,fd, len;
       struct sockaddr_in add_server;
       fd_client = socket(AF_INET,SOCK_STREAM,0);
       if (fd_client == -1)
       {
           perror("fd_sock");
           exit(1);
       }
       add_server.sin_family = AF_INET;
       add_server.sin_port = ntohs(4001);
       add_server.sin_addr.s_addr = inet_addr("127.0.0.1");
       len = sizeof(add_server);
       fd  = connect(fd_client,(struct sockaddr*)&add_server,len);
       if(fd == -1)
       perror("connect");
       write(fd,"Hello\n",6);
       return 0;
   }

   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 these

Answer: Option 'A'

the server will write back to the client whatever the clinet will write to the server


Linux Debugging Questions & Answers – Internet Domain Socket System Calls Download Pdf

Recent Posts