Dannon
Dannon

Reputation: 2216

Copy a socket over to another socket, is that same socket still active? java

If I have a socket variable named 'sock' and lets say down the line for some reason you have to create a new socket, lets say its called 'sock2', and you set 'sock2' to equal 'sock'. And then you throw away the variable 'sock'. Will the connection still work, will 'sock2' still be able to communicate with the other end of the socket that was originally in 'sock'?

Upvotes: 3

Views: 3168

Answers (2)

Stephen C
Stephen C

Reputation: 718946

Basically you are describing something like this

public void test (...) {
    Socket sock2;
    {
        Socket sock = // open socket
        sock2 = sock;
        // now 'sock' does out of scope; i.e. you "throw it away"
    }

    // use 'sock2'
}

Yes it will work just fine. Both sock and sock2 are reference variables (as are ALL variables in Java that are not primitive types!). The assignment sock2 = sock; just assigns a reference ... so that sock2 now points to the same Socket that sock does, and you can use it to talk to the remote host you were originally talking to via sock.


OK, so what about the case where sock2 previously pointed to s different Socket.

public void test (...) {
    Socket sock2 = // open socket to A
    {
        Socket sock = // open socket to B
        sock2 = sock;
        // now 'sock' does out of scope; i.e. you "throw it away"
    }

    // use 'sock2'
}

It still works, as before. For the same reasons.

The only catch this that you have dropped the reference to the socket connected to A. If you no longer have the reference to the A socket, your application cannot use it. (This doesn't affect your ability to talk to B ... 'cos you still have that reference ... in sock2).

So what happens now? Well, we now have a situation where Java Socket object for A is no longer available for the code to use. (In Java parlance, the object is "unreachable".) This means that it is eligible for garbage collection. But it does not mean that it will be immediately garbage collected. In fact, it could be seconds ... or days ... before the garbage collector runs. In the mean time, there may still be an open connection to some remote host tying down resources locally and remotely. This is a resource leak, and it could have serious consequences. (When the GC does eventually reclaim the socket, the finalizer for the implementation object will close the underlying connection and release any resources. But the damage could already have been done.)

The solution is to make sure that you always close() any Socket object that your code creates in a timely fashion ... and don't allow the references to just "drop on the floor".

Upvotes: 2

jma127
jma127

Reputation: 1432

Since there is still a reference to the object that was originally sock (in the form of sock2), the Java garbage collection will not dispose of it, and you can still use sock2 as if it were sock.

Upvotes: 5

Related Questions