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.")
Else
Try
Dim prozess As Process = Process.GetProcessesByName("samp")(0)
prozess.Kill()
Catch ex As Exception
End Try
My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\SAMP", "PlayerName", TextBox1.Text)
Process.Start("samp://164.132.229.172:7777")
Dim client As New TcpClient()
client.Connect("164.132.229.172", 7775)
Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text)
Dim stream As NetworkStream = client.GetStream()
stream.Write(sendBytes, 0, sendBytes.Length)
Timer1.Start()
End If
End If
Timer:
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
Process.Kill()
Next
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: [92.241.153.29] [01:12:15] Remote client [0] has sent: Joan_Mackey [01:12:16] [LAUNCHER] Player Socket disconnected| ClientID: [0] | PlayerID: [0]
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:
The TcpClient
is no longer used after you pass End Sub
.
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.