C# - Passing TcpClient as parameter...

Associate
Joined
3 Dec 2006
Posts
594
When passing TcpClient as a parameter, does it pass it as a parameter just like any other variable/class, or does it do it by referencing? Let's say I pass a parameter to a new class in the new class Constructor and I declare a new variable of TcpClient which I assign the parameter to. At the end of this class (well, it's a Form, but anyway..) I perform TcpClient.Close() to close this new instantiation of TcpClient but it closes the other one too!

Here's a more detailed run-down of what's going on in the program:

Two clients contact each other, if both confirm the game, one sends confirmation to Server. Server gets both TcpClients from a hashtable, and then creates a new class with the two TcpClient's as parameters. The server also sends back a confirmation that things are ready for the clients.

The clients now create a new instantiation of the form with the TcpClient to the Server as a parameter.

Things run smoothly and fine the first time round, but when the two clients close down the forms and then try and re-initiate a game nothing happens. There's a way I can get the form to initiate (which means initial contact to the server works) by taking out TcpClient.Close() of the Form_onClosing() method in the client. The problem with this is that nothing else happens beyond the initial contact. The forms load, the server creates a new class but no further communication occurs which leads me to believe that there's something going wrong with either the streams or possibly one of the TcpClients... Any ideas please? I've been racking my brain on this for aaages!
 
Nope, doesn't have a Clone() method.

Could it be the way I'm assigning the TcpClient in the new class(es)?

I'm declaring it with "TcpClient tcpPlayer1;" (for example) and then assigning it in the constructor using:

tcpPlayer1 = pTcpClient1; [pTcpClient1 is the parameter TcpClient]

So basically I'm not using the "new" keyword at all. I would've thought, though, that it works, seeing as how it works for the first time...
 
Ok, thanks. Is there anyway to make a new one from "scratch" by using an existing TcpClient? Or can that only be done using Connect()?

And any ideas on why the second-time-round doesn't work?
 
Ok, I understand.

I've just been debugging this program, and it looks as if either the client or server is destroying the TcpClient to one another as the game closes... I really can't understand why though :/
 
Why would it do that if it's being passed as reference? Does look like it's along the right track though. I'm getting an IOException error in the server in the thread that receives game requests. It says:

"An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host"

which I guess suggests that the client is shutting down the connection. Maybe it is when it closes down the form but I really don't see WHY it'd do that...

edit: or a similar error with "System.InvalidOperationException: The operation is not allowed on non-connected sockets." at the same spot, when trying to get the stream from the TcpClient.
 
Last edited:
Back
Top Bottom