Re: Are sockets thread safe




Maxim Yegorushkin wrote:
jainarunk@xxxxxxxxx wrote:


Phil,

The need arose, because I am writing a client for a web server, where
each thread in
the client wraps its own request for a paraticular web page from the
server and writes
to the same web server on the other end of the same socket. And of
course each
thread would be interested to read the web page for it sent the
request. I hope it
explains.

You need a dispatcher thread that serializes http requests from
different threads and writes them to the socket. This thread or another
one should also read all the http responses from the socket and forward
them to where these responses are waited for.

Phil, in one of his postings suggested that I have multiple sockets.
My reasoning is that
by having multiple sockets.

1. The user may potentially want to access hundreds of pages and
creating a socket
per page will be very costly and will put undue/unnecessary resource
utilization.

2. Some of the classes in my implementation are private and the 'user'
does not have
access to creatation

3. User is given the responisbility to create the thread and not the
client I am writing.
Because I do not know before hand what would user intend to do.

I will opt for a dispatcher thread solution, because it looks more
promising. However I
will need some guide lines as to how to implement a dispatcher thread,
what data
structures I will have to create, the communication channel between
individual threads
and the dispatcher thread, and the performance cost analysis if any.

Thanks for your support and enlightened discussion.

In any case, there should be no more than one thread simultaneously
reading/writing the same stream socket. Otherwise data get screwed up.

I was implementing locks and condition variables to ensure that data
does not
corrupted.

Nagrik

.



Relevant Pages

  • Re: Google and Python
    ... The idea is that the application is a web server. ... a process and "hand over" the client. ... It is trivial to pass a socket to a new thread or a forked child - you ... Fast cgi is a process which runs continuously which avoids startup ...
    (comp.lang.python)
  • RE: Does HttpListener have the same quirk as HttpWebRequest
    ... on "the number of actual sockets that ServicePoint is managing", ... the client requests can't be pipelined because they use the POST verb ... another request, the next request seems to re-use the same socket ...
    (microsoft.public.dotnet.framework)
  • Re: XML Web Service Client allows at maximum two asynch Requests
    ... This limitation is due to the HTTP specification that one client should have ... at most two concurrent connections to one web server. ... > sends the response. ... > Request 1 ...
    (microsoft.public.dotnet.framework.webservices)
  • RE: Asynchronous socket performance problem
    ... Why are you closing the socket after handling the request? ... your client is expecting that once a connection is made that it will be there ...
    (microsoft.public.dotnet.framework.performance)
  • Re: How can I extract the destination IP address from incoming req
    ... contains all the information of the source peer (the client), ... destiantion IP the request has been made by the client. ... << endl; ... //and then bind it to the socket address structures ...
    (microsoft.public.win32.programmer.networks)