simple tcp socket programming

Vu Pham vu
Mon May 17 11:59:43 PDT 2004


----- Original Message ----- 
From: "Roger Oberholtzer" <roger at opq.se>
To: <linux-users at linux-sxs.org>
Sent: Tuesday, February 24, 2004 9:47 AM
Subject: Re: simple tcp socket programming


> On Tue, 24 Feb 2004 23:39:45 +0800
> "M.W. Chang" <mwchang at i-cable.com> wrote:
>
> > >       server.sin_port = htons((short) port);
> > >
> > >       /* Set any socket/protocol options */
> > >       int on = 1;
> > >       setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on,
> > >       sizeof(on));
> > >>     if (bind(sock, (struct sockaddr *)&server, sizeof server) < 0) {
> >
> > Thank you. Worked like a charm.
>
> Great. BTW, the socket option for reusing the address may or may not be
> needed. If the client and server will be on the same machine it will be
> needed. So I usually just do it. However, if the two sides will always be
on
> a different machine, it may not be needed.
>

IMHO, SO_REUSEADDR allows the server to bind to that port ( listen ) again
immediately right after it stops. Without this option, if you kill the
server app, and run the server app again immediatete, it may not be able to
bind to that same port and gives the error "bind() error: Address already in
use".


Vu



More information about the Linux-users mailing list