Running on QNX 4.25
I am writing a tcp server and having a problem.
here is the basic code which is standard in any similar app.
after opening the socket and binding and starting a listener
while(1)
{
// printf(“got a connection”);
mylen=sizeof(myname);
sockalt=accept(sock,(struct sockaddr *)&myname,&mylen);
if(sockalt>=0) // make sure got a connection
{
// fork a child to handle the new connection
if((tid=fork()) == 0) // I am the child
{
close(sock); // child closed parrent socket
process_inbound(sockalt);
}
close(sockalt); //parent closes child socket
}
else
perror(“accept error”);
}
The code works fine for the first connection. however the next time
accept returns it returns the same sockalt id as the previous time.
ie.
first time sock=9
accept returns sockalt=10
process forks and parent closes sockalt (child should still have file
descriptor 10 open)
Accept returns sockalt=10 again when a new connection is made now the 2
child processes are accessing the same file descriptor.
any help would be appreciated
Fred,
What you are seeing is normal and correct. The scope of an ‘fd’ is
per-process - once you
fork fd10 in process “a” is different than fd10 in process “b”.
The process doing accept()s will always use the next-available fd - once you
close 10 (which
is correct) it will be used next time.
If you need a unique identifer for these the only safe one is the source
address:port.
Jay
“Fred Allen” <FredAllen@fortna.com> wrote in message
news:3BE1C5B2.B2851FD@fortna.com…
Running on QNX 4.25
I am writing a tcp server and having a problem.
here is the basic code which is standard in any similar app.
after opening the socket and binding and starting a listener
while(1)
{
// printf(“got a connection”);
mylen=sizeof(myname);
sockalt=accept(sock,(struct sockaddr *)&myname,&mylen);
if(sockalt>=0) // make sure got a connection
{
// fork a child to handle the new connection
if((tid=fork()) == 0) // I am the child
{
close(sock); // child closed parrent socket
process_inbound(sockalt);
}
close(sockalt); //parent closes child socket
}
else
perror(“accept error”);
}
The code works fine for the first connection. however the next time
accept returns it returns the same sockalt id as the previous time.
ie.
first time sock=9
accept returns sockalt=10
process forks and parent closes sockalt (child should still have file
descriptor 10 open)
Accept returns sockalt=10 again when a new connection is made now the 2
child processes are accessing the same file descriptor.
any help would be appreciated