What is the output of this program? [code] #include<stdio.h> #include<stdlib.h> #include<netinet/in.h> #include<sys/types.h>

Mỹ Duyên

New member
What is the output of this program?
Code:
   #include<stdio.h>
   #include<stdlib.h>
   #include<netinet/in.h>
   #include<sys/types.h>
   #include<sys/socket.h>
   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 the mentioned
 
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 123hoidap]# gcc -o san san.c
[root@localhost 123hoidap]# ./san
accept: Success
listen: Bad file descriptor
[root@localhost 123hoidap]#
 

Tìm kiếm

Back
Top