accept sys call error message !!!!

accept sys call error message !!!!

Post by rohi » Mon, 29 Jul 2002 15:37:02



hi,
  i have written a simple server client.though the server works
perfectly ok it continuously gives this error message which iam not
able to track where.

err meaase : accept: Bad file descriptor

iam posting the code(relevent parts) for the server here.

        sockfd = socket(AF_INET,SOCK_STREAM,0);
        if(sockfd == -1)
        {
                perror("socket");
                exit(errno);
        }
        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(PORT);
        my_addr.sin_addr.s_addr = INADDR_ANY;

        if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct
sockaddr)) == -1){
                perror("bind");
                close(sockfd);
                exit(errno);
        }

        listen(sockfd,5);
        sin_size = sizeof(struct sockaddr);

        while(1)
        {
                if((newsockfd = accept(sockfd,(struct sockaddr
*)&their_addr,&sin_size)) < 0)
                {
                        perror("accept");
                        close(sockfd);
                        exit(errno);
                }

                child = fork();
                if(child == 0)
                {
                        close(sockfd);
                        nbytes = read(newsockfd,buf,sizeof(buf));
                        if(nbytes == -1)
                        {
                                perror("read");
                                exit(errno);
                         }
                         wbytes=write(newsockfd,buf,numbytes);
                          if(wbytes == -1)
                          {
                                perror("read");
                                exit(errno);
                           }

                close(newsockfd);
                }
         close(newsockfd);
        }
close(sockfd);
return 0;

Quote:}

 
 
 

accept sys call error message !!!!

Post by Kasper Dupon » Mon, 29 Jul 2002 16:23:54



> hi,
>   i have written a simple server client.though the server works
> perfectly ok it continuously gives this error message which iam not
> able to track where.

> err meaase : accept: Bad file descriptor

After communicating with the client, the child continues
through the loop and calls accept. So you actually have
both parent and child calling accept. Since the child
has already closed the filedescriptor, the child will
get EBADF from the accept call. At this point the child
will terminate, and the parent will continue completely
unaffected by the child.

Quote:

> iam posting the code(relevent parts) for the server here.

>         sockfd = socket(AF_INET,SOCK_STREAM,0);
>         if(sockfd == -1)
>         {
>                 perror("socket");
>                 exit(errno);
>         }
>         my_addr.sin_family = AF_INET;
>         my_addr.sin_port = htons(PORT);
>         my_addr.sin_addr.s_addr = INADDR_ANY;

>         if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct
> sockaddr)) == -1){
>                 perror("bind");
>                 close(sockfd);
>                 exit(errno);
>         }

>         listen(sockfd,5);
>         sin_size = sizeof(struct sockaddr);

>         while(1)
>         {
>                 if((newsockfd = accept(sockfd,(struct sockaddr
> *)&their_addr,&sin_size)) < 0)
>                 {
>                         perror("accept");
>                         close(sockfd);
>                         exit(errno);
>                 }

>                 child = fork();
>                 if(child == 0)
>                 {
>                         close(sockfd);
>                         nbytes = read(newsockfd,buf,sizeof(buf));
>                         if(nbytes == -1)
>                         {
>                                 perror("read");
>                                 exit(errno);
>                          }
>                          wbytes=write(newsockfd,buf,numbytes);
>                           if(wbytes == -1)
>                           {
>                                 perror("read");
>                                 exit(errno);
>                            }

>                 close(newsockfd);

You probably want to call _exit(0); here.

Quote:>                 }
>          close(newsockfd);
>         }
> close(sockfd);
> return 0;
> }

--
Kasper Dupont -- der bruger for meget tid p? usenet.



 
 
 

accept sys call error message !!!!

Post by nrk » Mon, 29 Jul 2002 16:56:53



> hi,
>   i have written a simple server client.though the server works
> perfectly ok it continuously gives this error message which iam not
> able to track where.

> err meaase : accept: Bad file descriptor

> iam posting the code(relevent parts) for the server here.

>    sockfd = socket(AF_INET,SOCK_STREAM,0);
>         if(sockfd == -1)
>         {
>                 perror("socket");
>                 exit(errno);
>         }
>    my_addr.sin_family = AF_INET;
>         my_addr.sin_port = htons(PORT);
>         my_addr.sin_addr.s_addr = INADDR_ANY;

>         if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct
> sockaddr)) == -1){
>                 perror("bind");
>                 close(sockfd);
>                 exit(errno);
>         }

>         listen(sockfd,5);
>         sin_size = sizeof(struct sockaddr);

>    while(1)
>         {
>                 if((newsockfd = accept(sockfd,(struct sockaddr
> *)&their_addr,&sin_size)) < 0)
>                 {
>                         perror("accept");
>                         close(sockfd);
>                         exit(errno);
>                 }

>                 child = fork();
>                 if(child == 0)
>                 {
>                         close(sockfd);

Child closes the listening socket - Correct.

Quote:>                         nbytes = read(newsockfd,buf,sizeof(buf));
>                         if(nbytes == -1)
>                         {
>                                 perror("read");
>                                 exit(errno);
>                     }
>                          wbytes=write(newsockfd,buf,numbytes);
>                           if(wbytes == -1)
>                           {
>                                 perror("read");
>                                 exit(errno);
>                       }

>            close(newsockfd);

Child closes connected socket - Correct.
However, think of what the child will do: Since you haven't asked the
child process to exit (using either exit(0) or a return 0), child
process will continue onward.

Quote:>                 }
>          close(newsockfd);

Child closes an already closed descriptor - Not ok.  Child goes back to
top of loop and calls accept on a closed descriptor, causing a failure.
  You have correctly tested and handled the failure, though the child
process will close the descriptor sockfd, which has already been closed
by it before.

Quote:>         }
> close(sockfd);
> return 0;
> }

HTH,
nrk.
 
 
 

accept sys call error message !!!!

Post by Eric Worral » Mon, 29 Jul 2002 18:47:44


You are not exiting the child process. The child is looping back to the
accept statement and attempting an accept with a closed sockfd. Try the
following:

                 if(child == 0)
                 {
                         close(sockfd);
                         nbytes = read(newsockfd,buf,sizeof(buf));
                         if(nbytes == -1)
                         {
                                 perror("read");
                                 exit(errno);
                          }
                          wbytes=write(newsockfd,buf,numbytes);
                           if(wbytes == -1)
                           {
                                 perror("read");
                                 exit(errno);
                            }

                 close(newsockfd);
                 exit(0); // **** exit the child
                 }

Eric Worrall


> hi,
>   i have written a simple server client.though the server works
> perfectly ok it continuously gives this error message which iam not
> able to track where.

> err meaase : accept: Bad file descriptor

> iam posting the code(relevent parts) for the server here.

>         sockfd = socket(AF_INET,SOCK_STREAM,0);
>         if(sockfd == -1)
>         {
>                 perror("socket");
>                 exit(errno);
>         }
>         my_addr.sin_family = AF_INET;
>         my_addr.sin_port = htons(PORT);
>         my_addr.sin_addr.s_addr = INADDR_ANY;

>         if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct
> sockaddr)) == -1){
>                 perror("bind");
>                 close(sockfd);
>                 exit(errno);
>         }

>         listen(sockfd,5);
>         sin_size = sizeof(struct sockaddr);

>         while(1)
>         {
>                 if((newsockfd = accept(sockfd,(struct sockaddr
> *)&their_addr,&sin_size)) < 0)
>                 {
>                         perror("accept");
>                         close(sockfd);
>                         exit(errno);
>                 }

>                 child = fork();
>                 if(child == 0)
>                 {
>                         close(sockfd);
>                         nbytes = read(newsockfd,buf,sizeof(buf));
>                         if(nbytes == -1)
>                         {
>                                 perror("read");
>                                 exit(errno);
>                          }
>                          wbytes=write(newsockfd,buf,numbytes);
>                           if(wbytes == -1)
>                           {
>                                 perror("read");
>                                 exit(errno);
>                            }

>                 close(newsockfd);
>                 }
>          close(newsockfd);
>         }
> close(sockfd);
> return 0;
> }

--
You have just received an Etech Solution
For all your Linux requirements contact

 
 
 

accept sys call error message !!!!

Post by rohi » Tue, 30 Jul 2002 14:00:24


hi,
   thanks for the knowledge.i was in a oblivion that only that code
that is inside the child == 0 if condition would be executed by the
child and the rest by the parent.but then if i dont do exit from the
child process it continues on and executes the calls meant for the
parent(in my case).

thank u all
rohit

 
 
 

1. ENXIO error form accept() call

Hi group,
I have written a server program that listens on a certain port,
then accepts incoming connections (revealed by select()) with the
accept() call. Then, for each connected client, using the same select()
call I monitor for incoming requests from clients, and serve the answer
back for each request.
I have also written a test program that launches 100 clients; each of
them connects to the server and then sends 50 requests, delaying
each of them by a random time (0 ... 1 sec).
The problem is that SOMETIMES (e.g. for some clients) the connection
is not accepted by the server, and errno set by accept() is ENXIO
(that means "No such device or address").

Why does this happens ?

Thank you very much
Alessio

**** Posted from RemarQ - http://www.remarq.com - Discussions Start Here (tm) ****

2. sound blaster live! open source driver

3. EFAULT error in accept socket call

4. iptables...help

5. Error in accept() system call

6. driver linux per scheda wireless u.s. robotics USR5410

7. ERROR LOG MESSAGE: accept: Connection reset by peer

8. How long before it's ready for prime time?

9. ERROR MESSAGE: accept:Invalid Argument

10. Error message continuous error messages

11. comp.sys.mac.misc comp.sys.mac.system comp.sys.mac.apps

12. comp.sys.hp comp.sys.sgi comp.sys.sun comp.unix.ultrix comp.unix.wizards

13. Recognized and Accepted Sys Admin Certifications