Unix Programming - How do you close a port immediately?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2005 > How do you close a port immediately?





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 How do you close a port immediately?
Steven

2005-12-19, 7:49 am

Hi,

Please pardon me If Im posting at the wrong board. Kindly redirect me
to the appropriate board, please.

I'm playing around with sockets at the moment. Currently reading up
some online guides and experimenting on my own.

I'm testing a simple stream server using port 9999.

After the server is launched, it will be there sitting like a duck
until it receives something. Usually if I need to make amendments to
the code, i will press ctrl+c to terminate it. After termination , the
port 9999 is still active and will be in TIME_WAIT status.

Because during this TIME_WAIT period, I can still launch the simple
stream server. But netstat shows that port 9999 will still be in
TIME_WAIT period instead of LISTEN. The simple stream client will
encounter perror of "Connection Refused" during this TIME_WAIT period.

Any advice will be appreciated. Or any good reading materials to
suggest?

I managed to borrow a copy of Unix Network programming vol 1 from my
local library earlier. I guess it's a little heavy for a casual kiddie
like me. It uses it's own wrapper functions though. I prefer it not to
use the wrapper functions...

Thanks and Regards,
Steven.

Steffen

2005-12-19, 6:02 pm

Hi,

> I'm playing around with sockets at the moment. Currently reading up
> some online guides and experimenting on my own.
>
> After the server is launched, it will be there sitting like a duck
> until it receives something. Usually if I need to make amendments to
> the code, i will press ctrl+c to terminate it. After termination , the
> port 9999 is still active and will be in TIME_WAIT status.
>
> Because during this TIME_WAIT period, I can still launch the simple
> stream server. But netstat shows that port 9999 will still be in
> TIME_WAIT period instead of LISTEN. The simple stream client will
> encounter perror of "Connection Refused" during this TIME_WAIT period.


you can install a handler for the SIGINT signal that is invoked by
ctrl+c and shut down your sockets properly.

Steffen

Lew Pitcher

2005-12-19, 6:02 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steffen wrote:
> Hi,
>
>
>
> you can install a handler for the SIGINT signal that is invoked by
> ctrl+c and shut down your sockets properly.


The OP should also use the SO_REUSEADDR socket() option (in the first
instance of the server) to permit the restarted (second instance) server to
claim the port that's in TIME_WAIT status.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDpsKwagVFX4UWr64RAqPjAKDpZpQ1QWoB
QBZ1LIpUb5lbEFb2DgCfSske
chfAKEfjst/ptYweFLraeyk=
=chPS
-----END PGP SIGNATURE-----
Steven

2005-12-19, 6:02 pm

Hi everyone,

Thanks for the replies.

I will be printing this thread out.

I just realised the technical aspects are going way beyond my level at
the moment.

I have to learn to crawl before I learn how to run =p

Lew Pitcher

2005-12-19, 6:02 pm

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Steven wrote:
> Hi everyone,
>
> Thanks for the replies.
>
> I will be printing this thread out.
>
> I just realised the technical aspects are going way beyond my level at
> the moment.
>
> I have to learn to crawl before I learn how to run =p


A suggestion, if I may...

Get yourself a copy of "Unix Network programming - Volume 1: Networking APIs" by
W. Richard Stevens (ISBN 0-13-490012-X, Prentice Hall). It is one of the best
teaching tools for Unix networking APIs like sockets.

That will help you navigate the technical aspects a bit better.

HTH
- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDpu3lagVFX4UWr64RAiqGAJ40McFz3HtA
mnIaqfsK+Q5A89IdgACg2NC2
qPLykbkBqNZ6En/gvU80S6w=
=pqXW
-----END PGP SIGNATURE-----
Steven

2005-12-19, 6:02 pm

No worries , Lew.

When I began posting in this newsgroup...pretty recently (as of today),
I am prepared to get flamed, tossed, burnt, abused.

Usually these professional / veteran groups do not tolerate new kiddies
/ novice / newbie questions.

But I'm pleasantly surprised to find that the replies are very friendly
and helpful.

Yes. I have a copy on my desk now.

drhowarddrfine

2005-12-20, 2:50 am

Lew Pitcher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Steven wrote:
>
>
>
> A suggestion, if I may...
>
> Get yourself a copy of "Unix Network programming - Volume 1: Networking APIs" by
> W. Richard Stevens (ISBN 0-13-490012-X, Prentice Hall). It is one of the best
> teaching tools for Unix networking APIs like sockets.
>
> That will help you navigate the technical aspects a bit better.
>
> HTH
> - --
> Lew Pitcher
> IT Specialist, Enterprise Data Systems,
> Enterprise Technology Solutions, TD Bank Financial Group
>
> (Opinions expressed are my own, not my employers')
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.2.4 (MingW32)
>
> iD8DBQFDpu3lagVFX4UWr64RAiqGAJ40McFz3HtA
mnIaqfsK+Q5A89IdgACg2NC2
> qPLykbkBqNZ6En/gvU80S6w=
> =pqXW
> -----END PGP SIGNATURE-----

About Stevens book. I'm only up to page 80 but find that it tends to
start with a list of references, then a bunch of definitions which seem
to come out of the blue, then he finally tells you how what it all works
together. This all seems backward because he throws terms at you before
you really know what he's talking about. Is this book like this the
whole way through? If I didn't already have some understanding of
things I would be totally frustrated with it instead of only partly
frustrated.
Daniel C. Bastos

2005-12-24, 5:56 pm

In article <3ALpf.13625$Eu3.1212@fe07.lga>,
drhowarddrfine wrote:

[...]

> About Stevens book. I'm only up to page 80 but find that it tends to
> start with a list of references, then a bunch of definitions which seem
> to come out of the blue, then he finally tells you how what it all works
> together. This all seems backward because he throws terms at you before
> you really know what he's talking about. Is this book like this the
> whole way through? If I didn't already have some understanding of
> things I would be totally frustrated with it instead of only partly
> frustrated.


I've heard comments such as ``too slow, it's not a reference.'' A book
author cannot satisfy everyone. You may be lacking background, I
suppose --- but that's not exactly a problem. Imagine you know very
little about computers; the book will be nonsense. Imagine you know
too much about computers; the book will be trivial.

My suggestion is to don't stress yourself out. If you're reading on
subject X on book X_b and they mention the concept C_b which seems to
be important enough in order to understand subject X, then I will look
up books which explicitly talk about C_b.

It may mean leaving X aside long enough to frustrate you; patience may
help you there. As you read on C_b on other books, keeping what you
know about X in mind helps to connect both things.

By the way, if you'd like to post on the things which you read and you
didn't understand, go ahead; I believe a lot of people will answer you
clarifying --- some may confuse you; that's the USENET.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com