f



why error: "accept: Invalid argument"

when doing fork in a loop:
[code]
        while(1) {
                tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
                if (tmp_sd == -1) {
                        perror("accept");
                        exit(0);
                }

                //check client ip

                //fork
                pid = fork();
                switch(pid) {
                        case -1:
                                perror("fork");
                                break;
                        case 0:
                                _exit(0);
                }

                close(tmp_sd);
        }
[/code]
If there's no fork, the code done well.
But if fork, when telnet, it has error: accept: Invalid argument
Thanks a lot.

0
yarco.w (60)
11/16/2005 4:47:05 AM
comp.lang.c 30657 articles. 4 followers. spinoza1111 (3246) is leader. Post Follow

12 Replies
252 Views

Similar Articles

[PageSpeed] 3

Yarco wrote:
> when doing fork in a loop:
> [code]
>         while(1) {
>                 tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
>                 if (tmp_sd == -1) {
>                         perror("accept");
>                         exit(0);
>                 }
> 
>                 //check client ip
> 
>                 //fork
>                 pid = fork();
>                 switch(pid) {
>                         case -1:
>                                 perror("fork");
>                                 break;
>                         case 0:
>                                 _exit(0);
>                 }
> 
>                 close(tmp_sd);
>         }
> [/code]
> If there's no fork, the code done well.
> But if fork, when telnet, it has error: accept: Invalid argument

Platform or compiler specific questions are best asked in a
newsgroup where they are topical.
There are no functions accept(), perror(), fork(), close(), and
_exit() in standard C, so I won't say anything about their use.
comp.unix.programmer might be a good place to ask.

Your switch definitely lacks a default, though, to ward against
unexpected pid values. printf() debug from there...


Cheers
  Michael
-- 
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
0
Michael.Mair (1492)
11/16/2005 5:02:15 AM
Thanks

0
yarco.w (60)
11/16/2005 5:05:42 AM
On Wed, 16 Nov 2005 06:02:15 +0100, Michael Mair wrote:

> Yarco wrote:
>> when doing fork in a loop:
>> [code]
>>         while(1) {
>>                 tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
>>                 if (tmp_sd == -1) {
>>                         perror("accept");
>>                         exit(0);
>>                 }
>> 
>>                 //check client ip
>> 
>>                 //fork
>>                 pid = fork();
>>                 switch(pid) {
>>                         case -1:
>>                                 perror("fork");
>>                                 break;
>>                         case 0:
>>                                 _exit(0);
>>                 }
>> 
>>                 close(tmp_sd);
>>         }
>> [/code]
>> If there's no fork, the code done well.
>> But if fork, when telnet, it has error: accept: Invalid argument
> 
> Platform or compiler specific questions are best asked in a
> newsgroup where they are topical.
> There are no functions accept(), perror(), fork(), close(), and
> _exit() in standard C, so I won't say anything about their use.
[snip]

Isn't perror() part of the standard?  It's declared in stdio.h.

Regards,
Gary

0
gbaydo (4)
11/16/2005 5:51:02 AM
Gary Baydo wrote:
 
> Isn't perror() part of the standard?  It's declared in stdio.h.

perror() is in the standard library.

-- 
pete
0
pfiland (6613)
11/16/2005 5:58:52 AM
Gary Baydo wrote:
> On Wed, 16 Nov 2005 06:02:15 +0100, Michael Mair wrote:
> 
> 
>>Yarco wrote:
>>
>>>when doing fork in a loop:
>>>[code]
>>>        while(1) {
>>>                tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
>>>                if (tmp_sd == -1) {
>>>                        perror("accept");
>>>                        exit(0);
>>>                }
>>>
>>>                //check client ip
>>>
>>>                //fork
>>>                pid = fork();
>>>                switch(pid) {
>>>                        case -1:
>>>                                perror("fork");
>>>                                break;
>>>                        case 0:
>>>                                _exit(0);
>>>                }
>>>
>>>                close(tmp_sd);
>>>        }
>>>[/code]
>>>If there's no fork, the code done well.
>>>But if fork, when telnet, it has error: accept: Invalid argument
>>
>>Platform or compiler specific questions are best asked in a
>>newsgroup where they are topical.
>>There are no functions accept(), perror(), fork(), close(), and
>>_exit() in standard C, so I won't say anything about their use.
> 
> [snip]
> 
> Isn't perror() part of the standard?  It's declared in stdio.h.

You are right, thanks for the correction. I got carried away
before breakfast... I really should enforce my "no newsgroups
before caffeine" policy.

Cheers
  Michael
-- 
E-Mail: Mine is an   /at/ gmx /dot/ de   address.
0
Michael.Mair (1492)
11/16/2005 6:04:03 AM
>tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
int
accept(int s,
       struct sockaddr * restrict addr,
       socklen_t * restrict addrlen);
///////////////////////////////////////
case 0: {
     (void)printf("test........?");
      _exit(0); 
}

0
auto.ptr (18)
11/16/2005 6:06:44 AM
>tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
int
accept(int s,
       struct sockaddr * restrict addr,
       socklen_t * restrict addrlen);
///////////////////////////////////////
case 0: {
     (void)printf("test........?");
      _exit(0); 
}

0
auto.ptr (18)
11/16/2005 6:06:53 AM
>tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
int
accept(int s,
       struct sockaddr * restrict addr,
       socklen_t * restrict addrlen);
///////////////////////////////////////
case 0: {
     (void)printf("test........?");
      _exit(0); 
}

0
auto.ptr (18)
11/16/2005 6:06:54 AM
>tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
int
accept(int s,
       struct sockaddr * restrict addr,
       socklen_t * restrict addrlen);
///////////////////////////////////////
case 0: {
     (void)printf("test........?");
      _exit(0); 
}

0
auto.ptr (18)
11/16/2005 6:06:58 AM
"Yarco" <yarco.w@gmail.com> writes:
> when doing fork in a loop:
> [code]
>         while(1) {
>                 tmp_sd = accept(sd, (struct sockaddr*)&tmp_sin, &len);
>                 if (tmp_sd == -1) {
>                         perror("accept");
>                         exit(0);
>                 }
>
>                 //check client ip
>
>                 //fork
>                 pid = fork();
>                 switch(pid) {
>                         case -1:
>                                 perror("fork");
>                                 break;
>                         case 0:
>                                 _exit(0);
>                 }
>
>                 close(tmp_sd);
>         }
> [/code]
> If there's no fork, the code done well.
> But if fork, when telnet, it has error: accept: Invalid argument

accept() and for() are not standard C functions.  (Neither are _exit()
and close().)

Try comp.unix.programmer.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
11/16/2005 6:41:39 AM
I've posted it in comp.unix.programmer. But nobody answer it.

my code is Ok when compiling.
But when i "telnet" the server, it gets error:
accept: Invalid argument.
Then quit.
But without "fork" process, i "telnet" the server, and the server is
still running.

0
yarco.w (60)
11/16/2005 7:31:16 AM
"Yarco" <yarco.w@gmail.com> writes:
> I've posted it in comp.unix.programmer. But nobody answer it.

You posted less than 4 hours ago.  Be patient.  It's still off-topic here.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.
0
kst-u (21963)
11/16/2005 8:37:32 AM
Reply: