For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > May 2006 > Rejecting a connection









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Rejecting a connection
aamesster@googlemail.com

2006-05-30, 7:07 pm

Hi all,

I've been wondering how to reject a connection? Instead of accept() I
would like to "decline()" a connection. The only way of rejecting
connections I've thought about is to accept() and then close()
immediatelly. But would the other end actually receive the accept and
then the connection would be dropped?

Regards,
David

Eric Sosman

2006-05-30, 7:07 pm



aamesster@googlemail.com wrote On 05/30/06 10:58,:
> Hi all,
>
> I've been wondering how to reject a connection? Instead of accept() I
> would like to "decline()" a connection. The only way of rejecting
> connections I've thought about is to accept() and then close()
> immediatelly. But would the other end actually receive the accept and
> then the connection would be dropped?


The other end never "receives the accept" because even
in normal operation, accept() doesn't transmit anything.
The socket returned by accept() has already completed its
three-way handshake and is in the connected state; the fact
that accept() returns is just bookkeeping on the listening
side and does not affect the connection at all.

When you close the connection, the other end's TCP/IP
stack will learn about it fairly promptly in the course
of events for a normal shutdown. The application on the
other end probably won't learn about the disconnect until
it tries to send or receive something.

As for "rejecting" connections -- well, the simplest
way is never to listen() in the first place. But assuming
that you want to permit some connections while forbidding
others, it follows that you must listen() and accept().
After accept() returns you can make your decision and either
communicate or close.

--
Eric.Sosman@sun.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com