Keeping a socket open question

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 :slight_smile:

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)

:slight_smile: 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.

You should not rely on a socket to be opened. Instead change your protocol to handle the situation of reconnects more effectivly.

[quote=“HughB”]i guess i am really just looking for a generic process for keeping a particular socket open.
[/quote]

Can you send a periodic keep alive packet?

@ Architect I agree but the server app works this way and i have no input or control over it :frowning:

@ 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?

Sockets work on .NET. :slight_smile:

If your socket reference is null, and you expect it not be be, then there is something wrong in your code.

There is nothing I can think of with Sockets that would cause a socket reference to spontaneously become null.

A sample of you is needed.

Ok thats good advice, thanks mike. On monday if i don’t get anywhere i will post some of the code.
Keep the suggestions coming :slight_smile:

@ HughB -

Shows us your client code please.

@ mhectorgato -

Ooops. Missed that.

:smiley:

(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.

[quote=“Mike”]A sample of you is needed.
[/quote]
Uh…

Uh…
[/quote]

A finger or toe will do. :slight_smile:

1 Like

A finger or toe will do. :slight_smile:
[/quote]

:smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

@ mhectorgato Thanks for the Advice :slight_smile: