Any socket programmers out there?
A (simple) question, C++ code:
1. create a socket
int const sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2. bind to it
unsigned int const port = 1234;
unsigned long const listenaddr = INADDR_ANY;
sockaddr_in listensock = { AF_INET, htons(listenport), { htonl(listenaddr) } };
if (bind(sockfd, &listensock, sizeof(listensock)) != -1)
...
3. listen for incoming connection
if (listen(sockfd, 1) != -1)
...
4. accept connection
sockaddr_in clientsock;
socklen_t csl = sizeof(clientsock);
int const peerfd = ::accept(sockfd, reinterpret_cast<sockaddr*>(&clientsock),
&csl);
if (peerfd != -1)
... talk to the connection ...
5. close connection
close(peerfd);
close(sockfd);
The problem:
The above sequence works fine the first time the program is
run. An immediate second invocation fails at step 2, i.e.
cannot bind to 0.0.0.0:1234.
But, if I wait 2 minutes or so, run the program again,
the whole process works fine.
Me thinks that the two close() calls are not enough to
free up the port (1234), but the kernel eventual times out
the connection and resets the port .... any ideas?
Thanks in advance,
Rick W
--
Richard Welykochy || Praxis Services Pty Ltd
--
SLUG - Sydney Linux Users Group Mailing List - http://www.slug.org.au
To unsubscribe send email to [EMAIL PROTECTED] with
unsubscribe in the text