-1

Basically, I want a 'listen' thread to create a vector of sockets that the main thread can access. My listening method is this

ServerSocket listener = new ServerSocket(port);
while (connections.size() < 9) {
    connections.add(listener.accept());
    System.out.println("Connected Person");
}

But I think because java works on passing by reference, it's only pushing a reference on the vector, so when a second connection comes in, now connections[0] and connetions[1] are the same sockets, right?

Furthermore, when this thread dies, the listener variable dies, so then the vector is full of empty socket references. I want to be able to copy the socket into the vector, however every guide says I have to add the copy function to the object, but I dont know how to when im using a pre-defined ServerSocket type.

Edit: this is for a P2P system where each user knows of each other user. Additionally, my 'send' function loops through the vetor, sending to each socket into the vector. Then reading reads from every socket in vector. The error is the sockets are not receiving/sending

Frogglet
  • 45
  • 6
  • 2
    You're adding them just right. The only problem is that most people spawn a thread to handle each new Socket. – NomadMaker Nov 07 '20 at 02:34
  • 1
    I don't see a problem here. The accept() method blocks and returns a new client which is a new socket object so there are no problems with references – Sync it Nov 07 '20 at 04:15

1 Answers1

2

But I think because java works on passing by reference it's only pushing a reference on the vector, so when a second connection comes in, now connections[0] and connections[1] are the same sockets, right?

  1. Java is NOT pass-by-reference1. It is pass-by-value. See Is Java "pass-by-reference" or "pass-by-value"?.

    In this case, the value being passed (actually returned) is a reference to a Socket object.

  2. Each time the accept() method returns, it will return a reference to a different Socket object. Therefore, connections.get(0) and connections.get(1) will refer to different Socket objects.

Furthermore, when this thread dies, the listener variable dies, so then the vector is full of empty socket references.

Nope. The only way that a Vector can be full of "empty" (i.e. null) Socket references is if something explicitly put them there. Your code doesn't do this. Your code is putting non-null references into connections. And they will stay there even if the respective Socket objects are closed. (If you want to get rid of them, you need to remove them from the Vector.

I want to be able to copy the socket into the vector ...

That's not how Vector works. A Vector holds references to objects, not objects.

And besides it is NOT POSSIBLE to copy a Socket object. They are not copyable.

I think you need to go back to basics and understand the distinction between objects and references in Java. 'Cos what you are saying doesn't make a lot of sense from the Java perspective.

Edit: this is for a P2P system where each user knows of each other user. Additionally, my 'send' function loops through the vetor, sending to each socket into the vector. Then reading reads from every socket in vector. The error is the sockets are not receiving/sending.

That could be caused by lots of things, and I don't think we can help you without seeing a minimal reproducible example.


1 - If you see some article that claiming that Java is pass-by-reference, ignore it. Either the author don't understand Java, or they don't understand what "pass-by-reference" really means. (Or they are arguing that "pass-by-reference" should mean something different to what it actually means ... which is a terribly bad idea. It "alternative truth" nonsense ... like when some Microsoft folks argued that C is an OO language!)

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
  • Thanks so much for this. This was the first Java program I've made for an assignment, but since posting it I did more research and found out most of the info you said. I think my main source of confusion was that, while java is pass by value, since I was mostly using object holders for primatives (Integer, Boolean), it was the address value being sent, not the object. I've got it all working now, thanks :D – Frogglet Nov 08 '20 at 10:09