Debugging Question: Download Linux Debugging PDF

What is the output of this program no 15?
#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("googlen");
return 0;
}
a) this program will print the string "google"
b) segmentation fault
c) error
d) none of the mentioned

Tweet Share WhatsApp

Answer:

c) error
Explanation:
The syntax of the connect() is wrong. connect() should be used in client program only.
Ouptut:
[root@localhost google]# 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 google]#

Download Linux Debugging PDF Read All 80 Linux Debugging Questions
Previous QuestionNext Question
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("googlen");
return 0;
}
a) the listen() is missing
b) the connect() is missing
c) the read() and write() are missing
d) none of the mentioned
What is the output of this program no 16?
#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("Sanfoudnryn");
return 0;
}
a) error
b) "google"
c) segmentation fault
d) none of the mentioned