I have a Method to handle client data. I currently have this:
private void HandleClientData(TcpClient c)
{
byte[] bytes = new byte[256];
string data = null;
TcpClient client = c;
c.Close();
client.ReceiveTimeout = 10000;
}
Now, does the line: TcpClient client = c;
cause a loss of the client, even if a new TcpClient
is created using c
.
I have read somewhere that all references all share an underlying socket
, however, would reinitialising it like this allow the user to still send data through or would the line: c.Close();
completely end the connection?
Please note: I do not currently have code to test this myself, as it has not been written yet, this question will also help me to understand more TcpClient
handling in further projects. Thanks.
TcpClient client = c;
This line doesn't create a copy of c
, it is a new reference to the very same thing c
is. There is still only 1 TcpClient instance.
Therefore, when you do:
c.Close();
It closes the reference to c
(and therefore client
is also closed as they are the same thing).
They don't just share the same 'socket', they are exactly the same thing with 2 different variable names.