Keep a socket declared

sales

New member
Joined
Apr 9, 2011
Messages
1
Programming Experience
Beginner
Hi, how do I keep a Socket declared forever so i can call it in other methods?

All the tutorials i've read just have a server, client, server sends client message, client disconnects. What if I want to remain connected? I've tried making the sockets global but that didn't work somehow. Any tips? :) and please excuse my ignorance as i am quite the beginner.
 
You don't declare Sockets. You declare variables. If you declare a variable inside a method then it only exists for the method call. If you declare a member variable then it exists for the life of the object it's declared in. A variable is just a variable though. It might refer to a Socket object or it might be Nothing. It might refer to multiple Socket objects at different times. When you create a Socket object you will generally assign it to a variable. You can then access that Socket object via that variable. As long as the variable exists, you can access the Socket object it refers to. If you assign a Socket to a local variable, you can no longer access the Socket object once the method completes because the variable no longer exists. The Socket object does, but you have no way to access it. If you want to be able to access an object, Socket or otherwise, in multiple methods then, generally, you should assign it to a member variable. If thjat's what you're doing and it's not working then you're doing something wrong. Unless we can see what you're doing, we can't tell you what you're doing wrong.
 
Back
Top