simple tcp socket programming
Roger Oberholtzer
roger
Mon May 17 11:59:43 PDT 2004
On Tue, 24 Feb 2004 10:06:59 -0600
"Vu Pham" <vu at sivell.com> wrote:
>
> ----- 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".
That too. But is is really the same underlying thing. We always enable
reusing ports.
--
+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer ? E-mail: roger at opq.se ?
? OPQ Systems AB ? WWW: http://www.opq.se/ ?
? Erik Dahlbergsgatan 41-43 ? Phone: Int + 46 8 314223 ?
? 115 34 Stockholm ? Mobile: Int + 46 733 621657 ?
? Sweden ? Fax: Int + 46 8 302602 ?
+????????????????????????????+???????????????????????????????+
More information about the Linux-users
mailing list