Forum OpenACS Q&A: Threads and sockets in OpenACS
I'm quite new to programming with sockets and have a few questions. I wrote an OpenACS program to connect to a server using the TCL "socket", "fconfigure", "read" etc commands and it works well for my needs. However, after writing my program I just noticed that AOLserver has an ns_sock* API that looks very powerful http://aolserver.com/man/4.0/tcl/ns_sock.html - my question is should I be using this rather than plain TCL sockets? What advantages will it give me over TCL sockets?
Secondly, I want to run my proc in the background so that the adp is not hanging while waiting for the socket to finish its business. Traditionally I would have used ad_schedule_proc -once to run a proc in the background. However this has this disadvantage that you lose your ad_conn connection. Is there an alternative way to do this? Is ns_thread the way to achieve this?
Any other tips on programming sockets with AOLserver/OpenACS?
thanks for any help
If you want a good idea of the AOLserver ns_sock+ functions, look at how they are used in ns_https for some ideas.
I have some old web pages of these ns_sock+ commands which have examples that were removed from the version you reference above:
Another more complete example using some of these commands is here:
I'm not sure what you are trying to do with the scheduled proc, or how that relates with ns_conn, but if you want to run something after the client connection finishes, you can schedule a trace filter, or use ns_atclose, which requires an ns_conn. ns_atclose is guaranteed to run regardless of any errors prior to the connection close. If you are trying to run code during the client connection, you should look at ns_proxy which is designed for this type of thing.
many thanks for your reply - that was very useful. There is no obvious advantage to me in using the ns_sock+ commands, which has put my mind at rest on that issue.
On the second question, what I was trying to do was find a way to run a proc that required a connection (i.e. to lookup the user_id etc) without actually holding up the browser. I ended up rewriting the proc to be independent of the connection (just passing in the user_id etc). I can see now I was being a bit dim, expecting to both have a connection and not have a connection at the same time! I'm happy now with my solution but it was useful to know about ns_atclose and ns_proxy.
Although I'm still not totally clear on when one would use ns_thread.
thanks again Tom