1. What is the output of this program?
#include<stdio.h>
int main()
{
int fd_socket;
fd_socket = socket(AF_UNIX,SOCK_STREAM,0);
printf("%d\n",fd_socket);
return 0;
}
a) -1
b) 0
c) any integer value
d) none of the mentioned
View Answer
Explanation: To use socket(), the header files sys/types.h and sys/socket.h are required.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:6:21: error: ‘AF_UNIX’ undeclared (first use in this function)
san.c:6:21: note: each undeclared identifier is reported only once for each function it appears in
san.c:6:29: error: ‘SOCK_STREAM’ undeclared (first use in this function)
[root@localhost sanfoundry]#
2. In this program, the third argument of the socket() is used for _____ potocol.
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
int main()
{
int fd_socket;
if(socket(AF_UNIX,SOCK_STREAM,0) == -1)
perror("socket");
return 0;
}
a) TCP/IP
b) UDP
c) both TCP/IP and UDP
d) none of mentioned
View Answer
Explanation: None.
3. By this program the soket “san_sock” will create
#include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
int main()
{
struct sockaddr_un add_server;
int fd_server;
fd_server = socket(AF_UNIX,SOCK_STREAM,0);
if(fd_server == -1)
perror("socket");
add_server.sun_family = AF_UNIX;
strcpy(add_server.sun_path,"san_sock");
if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
perror("bind");
return 0;
}
a) in the /tmp directory
b) in the /usr directory
c) in the present working directory
d) none of the mentioned
View Answer
Explanation: None.
Output:
[root@localhost sanfoundry]# ls
san.c
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
[root@localhost sanfoundry]# ls
san san.c san_sock
[root@localhost sanfoundry]#
4. What is the length of of the queue for pending connections in this program?
#include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
int main()
{
struct sockaddr_un add_server;
int fd_server;
fd_server = socket(AF_UNIX,SOCK_STREAM,0);
if(fd_server == -1)
perror("socket");
add_server.sun_family = AF_UNIX;
strcpy(add_server.sun_path,"server_sock2");
if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
perror("bind");
if( listen(fd_server,3) != 0)
perror("listen");
return 0;
}
a) 0
b) 1
c) 2
d) 3
View Answer
Explanation: The second argument of listen() specifies the length for the queue for pending connections.
5. What is the output of the program?
#include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
int main()
{
struct sockaddr_un add_server, add_client;
int fd_server, fd_client;
int len;
char ch;
fd_server = socket(AF_UNIX,SOCK_STREAM,0);
if(fd_server == -1)
perror("socket");
add_server.sun_family = AF_UNIX;
strcpy(add_server.sun_path,"san_sock");
if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
perror("bind");
if( listen(fd_server,3) != 0)
perror("listen");
len = sizeof(add_client);
fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len);
printf("Sanfoundry\n");
return 0;
}
a) the program will print the string “Sanfoundry”
b) the process will remain block
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: There is no peding request in the queue for listening socket “san_sock”.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
^Z
[4]+ Stopped ./san
[root@localhost sanfoundry]#
6. What is the output of this program?
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
int main()
{
int fd;
fd = socket(AF_UNIX,SOCK_STREAM,0);
printf("%d\n",fd);
return 0;
}
a) 0
b) 1
c) 2
d) 3
View Answer
Explanation: The socket() returns the lowest available file descriptor and in this program i.e. 3.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
3
[root@localhost sanfoundry]#
7. What is the output of this program?
#include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
#include<errno.h>
int main()
{
struct sockaddr_un addr;
int fd;
fd = socket(AF_UNIX,SOCK_STREAM,0);
if (fd == -1)
perror("socket");
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path,"san_sock");
if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1)
printf("Sanfoudnry\n");
return 0;
}
a) this program will print the string “Sanfoundry”
b) this program will not print the string “Sanfoundry”
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: The first argument of the bind() is not a valid file descriptor in this program.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
[root@localhost sanfoundry]# ./san
Sanfoudnry
[root@localhost sanfoundry]#
8. What this program is not able to connect with any client program?
#include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
int main()
{
struct sockaddr_un add_server, add_client;
int fd_server, fd_client;
int len;
char ch;
fd_server = socket(AF_UNIX,SOCK_STREAM,0);
if(fd_server == -1)
perror("socket");
add_server.sun_family = AF_UNIX;
strcpy(add_server.sun_path,"san_sock");
if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
perror("bind");
len = sizeof(add_client);
fd_client = accept(fd_server,(struct sockaddr*)&add_client,&len);
printf("Sanfoundry\n");
return 0;
}
a) the listen() is missing
b) the connect() is missing
c) the read() and write() are missing
d) none of the mentioned
View Answer
Explanation: None.
9. What is the output of this program?
#include<stdio.h>
#include<sys/types.h>
#include<sys/un.h>
#include<sys/socket.h>
int main()
{
struct sockaddr_un add_server, add_client;
int fd_server, fd_client;
int len;
char ch;
fd_server = socket(AF_UNIX,SOCK_STREAM,0);
if(fd_server == -1)
perror("socket");
add_server.sun_family = AF_UNIX;
strcpy(add_server.sun_path,"san_sock");
if( bind(fd_server,(struct sockaddr*)&add_server,sizeof(add_server)) != 0)
perror("bind");
len = sizeof(add_client);
fd_client = connect(fd_server,(struct sockaddr*)&add_client,&len);
printf("Sanfoundry\n");
return 0;
}
a) this program will print the string “Sanfoundry”
b) segmentation fault
c) error
d) none of the mentioned
View Answer
Explanation: The syntax of the connect() is wrong. connect() should be used in client program only.
Ouptut:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:20:46: warning: passing argument 3 of ‘connect’ makes integer from pointer without a cast [enabled by default] /usr/include/sys/socket.h:129:12: note: expected ‘socklen_t’ but argument is of type ‘int *’
[root@localhost sanfoundry]#
10. What is the output of this program?
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<errno.h>
int main()
{
struct sockaddr_in addr;
int fd;
fd = socket(AF_UNIX,SOCK_STREAM,0);
if (fd == -1)
perror("socket");
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path,"san_sock");
if (bind(4,(struct sockaddr*)&addr,sizeof(addr)) == -1)
printf("Sanfoudnry\n");
return 0;
}
a) error
b) “Sanfoundry”
c) segmentation fault
d) none of the mentioned
View Answer
Explanation: The structure used for AF_UNIX if sockaddr_un.
Output:
[root@localhost sanfoundry]# gcc -o san san.c
san.c: In function ‘main’:
san.c:14:6: error: ‘struct sockaddr_in’ has no member named ‘sun_family’
san.c:15:2: warning: incompatible implicit declaration of built-in function ‘strcpy’ [enabled by default] san.c:15:13: error: ‘struct sockaddr_in’ has no member named ‘sun_path’
[root@localhost sanfoundry]#
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.
- Apply for Programming Internship
- Practice Programming MCQs
- Buy Information Technology Books
- Apply for Linux Internship
- Buy Linux Books