How to use sockets correctly? - Unix
This is a discussion on How to use sockets correctly? - Unix ; Hopefully this is a good place to ask this question. I was wanting
some advice on how best to use unix c sockets.
I'm writing a simple http client, so currently I create a socket with
the socket() method, and ...
-
How to use sockets correctly?
Hopefully this is a good place to ask this question. I was wanting
some advice on how best to use unix c sockets.
I'm writing a simple http client, so currently I create a socket with
the socket() method, and then call connect() to connect to the server
(I'm using a TCP stream, rather than UDP).
What I want to know is should I create a new socket using socket(),
every time I want to connect to a new server, or can I just close the
connection, and call connect() to connect to a new server?
For example. Currently I create the socket, and connect to www.google.com.
I make a standard HTTP Get request for index.htm. This returns a 302
(Resource moved to www.google.co.uk).
Now to connect to www.google.co.uk, currently I'm closing my socket,
then I use socket() to create a new one, and finally call connect() to
connect to www.google.co.uk.
Is it possible to just close() the socket, and then call connect()
passing the credentials for www.google.co.uk ? I tried this, but it
seemed to fail to connect.
Originally I asummed that I should always pair a connect() with
close(), but perhaps I should pair socket() with close() ?
Thanks
-
Re: How to use sockets correctly?
On Nov 3, 1:57*pm, Dave wrote:
> What I want to know is should I create a new socket using socket(),
> every time I want to connect to a new server, or can I just close *the
> connection, and call connect() to connect to a new server?
You cannot "close the connection". You can shutdown the connection, in
which case the socket refers to the shutdown connection and cannot be
reconnected. Or you can close the socket, in which case it no longer
exists and cannot be connetced.
> For example. *Currently I create the socket, and connect towww.google.com.
> I make a standard HTTP Get request for index.htm. * This returns a 302
> (Resource moved towww.google.co.uk).
>
> Now to connect towww.google.co.uk, currently I'm closing my socket,
> then I use socket() to create a new one, and finally call connect() to
> connect towww.google.co.uk.
> Is it possible to just close() the socket, and then call connect()
> passing the credentials forwww.google.co.uk? I tried this, but it
> seemed to fail to connect.
No, because 'close' makes the file descriptor invalid since the socket
is now gone. There is nothing to 'connect'.
> Originally I asummed that I should always pair a connect() with
> close(), but perhaps I should pair socket() with close() ?
When you are finished with a socket and no longer need it, 'close' it.
If you fully 'shutdown' the connection, the socket you are left with
cannot be used for anything. All you can do is 'close' it.
DS
-
Re: How to use sockets correctly?
On 3 Nov, 22:03, David Schwartz wrote:
> On Nov 3, 1:57*pm, Dave wrote:
>
> > What I want to know is should I create a new socket using socket(),
> > every time I want to connect to a new server, or can I just close *the
> > connection, and call connect() to connect to a new server?
>
> You cannot "close the connection". You can shutdown the connection, in
> which case the socket refers to the shutdown connection and cannot be
> reconnected. Or you can close the socket, in which case it no longer
> exists and cannot be connetced.
>
> > For example. *Currently I create the socket, and connect towww.google..com.
> > I make a standard HTTP Get request for index.htm. * This returns a 302
> > (Resource moved towww.google.co.uk).
>
> > Now to connect towww.google.co.uk, currently I'm closing my socket,
> > then I use socket() to create a new one, and finally call connect() to
> > connect towww.google.co.uk.
> > Is it possible to just close() the socket, and then call connect()
> > passing the credentials forwww.google.co.uk?I tried this, but it
> > seemed to fail to connect.
>
> No, because 'close' makes the file descriptor invalid since the socket
> is now gone. There is nothing to 'connect'.
>
> > Originally I asummed that I should always pair a connect() with
> > close(), but perhaps I should pair socket() with close() ?
>
> When you are finished with a socket and no longer need it, 'close' it.
> If you fully 'shutdown' the connection, the socket you are left with
> cannot be used for anything. All you can do is 'close' it.
>
> DS
Thanks, that is the clarification that I needed.