Want to read socket data in ASP.NET

  • Thread starter Thread starter Steven Blair
  • Start date Start date
S

Steven Blair

I need to write an ASP.NET application which can do the following:

1. Create a socket which will stay alive and continously read data.
2. The data read needs to be displayed on the webpage.
3. As the data is received, it needs to be displayed as its received.

I have made an attempt, but hit a problem.

Here is a brief overview of the attempt:

1. Page_Load kicks off a thread for the socket handling. The socket is
static.
Its sits and reads data and caches the bytes read in an ArrayList
(also static).

2. When a postback is fired (I am using an Atlas timer to prevent a full
postback) I clear the Listbox component
for displaying the socket data and then bind the ArrayList to the
Listbox to display.

This does appear to work but at different intervals, I get a message box
box indicating unknown error.

If I take the code out for dsiplaying the Listbox, the data continues to
cache, but I cant think of a way of displaying.

Anyone know why this is happening, or suggest an alternative to my
problem?

Regards,

Steven
 
Steven,

ASP.NET is definitely not the place to do this, not only for security
reasons (the security model of ASP.NET doesn't really make it easy to open a
socket to listen to), but because of the nature of the web. You can't push
things to the browser unless a request is made, so having a socket sit on
your page waiting for requests is just a bad idea.

Rather, you should have a service that handles this aspect (the reading
of bytes off the socket) and writes the information to some shared resource
(file, database, whatever) which the ASP.NET app can then read from and push
to the page when requested.

Hope this helps.
 
I understand it is not ideal to have this in ASP.NET, but would be very
beneficial to the company to have this.
Security is not so much a problem. The proposed application can only
ever be run locally on our intranet and its not sensitive data being
viewed.
We already have a service which collects the messages, then sends it out
on a specified port which the application will connect to to view. (At
the moment its a windows program doing this job).
Having a shared file / database brings its own problems of how we track
which lines have been displayed and also, the main point of it having to
be real time messages being displayed (I don't think caching to a
database, then retrieving would be suitable)
 
I think you're going to have to identify the "unknown error." In addition, I
don't see any reason why the Socket or ArrayList needs to be static; this
could be causing some issue. But without specific information about the
problem occurring, we can only guess.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
This unknown error is causing me a lot of pain.
I even tried putting try catch all round my code to see if it would
break out and tell me the problem.

If my socket and ArrayList is not static, how would both object surive
after the postback has been finished and my page goes out of scope?

Maybe I am a little confused. Perhaps if I declared my socket within the
thread method, and the thread method could perhaps write out to the
Session.

On the next postback I could read the data from the Session to the
screen, then clear the Session value to continue (this would be locked
as I was doing this!)
 
There are lots of ways to avoid making your Socket class static. You can
store an instance in the Application Cache, create a Service, or use
Remoting.

Using a Thread is not going to be a good idea, if the Thread is spawned by a
Page class. Can you at least give us the text of the error you *do* see?
Also, how are you clearing this ArrayList after reading from it? It sounds
like you may possibly be experiencing a memory fragmentation issue. Memory
fragmentation occurs when an object remains in memory, but continually
de-allocates and re-allocates memory. Because the object itself is never
released, memory gets fragmented. You may find the following article
helpful: http://www.vsj.co.uk/dotnet/display.asp?id=284

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
Back
Top