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
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\SAMP", "PlayerName", TextBox1.Text)
Dim client As New TcpClient()
Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim stream As NetworkStream = client.GetStream()
stream.Write(sendBytes, 0, sendBytes.Length)
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
[01:12:15] [LAUNCHER] Connection to Socket Server. | ClientID:  |
ClientIP: [184.108.40.206] [01:12:15] Remote client  has sent:
Joan_Mackey [01:12:16] [LAUNCHER] Player Socket disconnected|
ClientID:  | PlayerID: 
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
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
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:
TcpClient is no longer used after you pass
When the Garbage Collector runs it will start releasing the
TcpClient's resources, and call
Dispose() on the underlying socket.
The underlying socket will then tell the endpoint that it's disconnecting, and then it will terminate the connection.
Now your connection to the server isn't open anymore.