Hi Guys.
Ive built an app In regular C# as an experiment that talks to a server using sockets. I want to be able to open a socket but keep it open throughout the life of the app (because i get a specific ID back from the server app that i need to re use. If the socket is closed then the ID will change etc.
Ive tried declaring the socket as a public etc but it doesn’t work and i get Null reference to the object errors (which makes sense).
Any snippets or help would be really appreciated by this code novice
Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)
code wise its at the office and i am at home now and as mentioned its a regular windows forms app for the moment.
Product wise the server app is a product called InfoCaster Manager (a content distribution and management system for Digital Signage)
Setup is Server app listens on port 9001 for a TCP socket connection. When a connection is opened it sends a bunch of XML data that includes a unique ID that i need to keep.
I then send back the Initialisation string (Again an XML string) and the server send me back a "reply Reply@ message (again more XML.
Now i can then ask for more info and send control messages (like telling a player to play different content packages) but it also pushes status info on the open socket which i want to subscribe to but to do that i need to keep the socket open otherwise i have to go through the re init process and request the status info rather than just receiving it.
Ultimately i want to port this over to a spider so i can create a little hardware based status monitor that give me a single view of the network of players status.
i guess i am really just looking for a generic process for keeping a particular socket open.
@ Architect I agree but the server app works this way and i have no input or control over it
@ mhectorgato that could work but still leaves me with my problem. i guess in its most basic form (and not forgetting that i am a novice with coding) is how could i declare the socket and make it available throughout my app. Everything I’ve tried so far doesn’t work as its either falling out of scope or becoming a null reference.
Would it work better if i put the socket into a thread and had a keep alive method in there?
(It’s hard to answer without understanding the architecture of the app)
I would imagine that a thread to handle the socket would work for you. You could fire events or call methods directly if data is sent over the wire to your device. A queue could be used to for sending data to the server.
But a socket shouldn’t fall out of scope become NULL on it’s own. A class level variable should normally do the trick.