Nathaniel Nathaniel - 2 years ago 114 Question

Why is my TCP connection closing?

I have developed an application for my game server. Now, in this application when I press "Connect" it sends data to my Game Server. And it does, but, the problem is, when it sends the connection it automatically closes it, and I don't want to close it because I have a socket plugin that kills the TCP connection after player leaves the game. Now on my localhost it works, the TCP doesn't "die" but on my vps it disconnects, can someone please check my code and see if I'm doing something wrong?

Dim p() As Process
Dim gta() As Process

p = Process.GetProcessesByName("samp")
gta = Process.GetProcessesByName("gta_sa")

If p.Count > 0 Then
MsgBox("Vec imate pokrenut SAMP - ugasite ga.")

ElseIf gta.Count > 0 Then
MsgBox("Vec imate pokrenut GTA San Andreas - ugasite ga.")


Dim prozess As Process = Process.GetProcessesByName("samp")(0)
Catch ex As Exception

End Try
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\SAMP", "PlayerName", TextBox1.Text)

Dim client As New TcpClient()
client.Connect("", 7775)
Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)

Dim stream As NetworkStream = client.GetStream()
stream.Write(sendBytes, 0, sendBytes.Length)


End If

End If


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim pd() As Process
pd = Process.GetProcessesByName("Aimbot By The2Gamers")
If pd.Count > 0 Then
Dim InternetExplorer() As Process = Process.GetProcessesByName("Aimbot By The2Gamers")
For Each Process As Process In InternetExplorer
End If
End Sub

Now this part down here is only server log from my vps:

[01:12:15] [LAUNCHER] Connection to Socket Server. | ClientID: [0] |
ClientIP: [] [01:12:15] Remote client [0] has sent:
Joan_Mackey [01:12:16] [LAUNCHER] Player Socket disconnected|
ClientID: [0] | PlayerID: [0]

Answer Source

You should move the Dim client As New TcpClient() initialization to class level. Class level means that you put it outside any method (that is, outside any Sub or Function).

As it stands, your TcpClient is collected by the Garbage Collector after you've exited whatever method you put it in. After you've passed End Sub the TcpClient is technically no longer used, because you cannot access that specific instance anymore. The Garbage Collector checks for this when it runs, and as your variable is no longer used it will be collected.

When the Garbage Collector collects an object it starts releasing the object's unused resources. If the object (or any child object) implements the IDisposable interface, the Garbage Collector will first call that object's Dispose() method.

TcpClients are built on top of the System.Net.Sockets.Socket class. The Socket class implements IDisposable which means it has got a Dispose() method. The Garbage Collector will call this method for the underlying socket of your TcpClient. When it is called, the socket will automatically shut itself down and tell the endpoint that it's disconnecting - and this is what you are experiencing.

So to summarize:

  1. The TcpClient is no longer used after you pass End Sub.

  2. When the Garbage Collector runs it will start releasing the TcpClient's resources, and call Dispose() on the underlying socket.

  3. The underlying socket will then tell the endpoint that it's disconnecting, and then it will terminate the connection.

  4. Now your connection to the server isn't open anymore.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download