为什么我的 TCP 连接关闭? [英] Why is my TCP connection closing?

查看:57
本文介绍了为什么我的 TCP 连接关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的游戏服务器开发了一个应用程序.现在,在这个应用程序中,当我按下连接"时,它会将数据发送到我的游戏服务器.它确实如此,但是,问题是,当它发送连接时,它会自动关闭它,我不想关闭它,因为我有一个套接字插件,它会在玩家离开游戏后终止 TCP 连接.现在在我的本地主机上它可以工作,TCP 没有死",但在我的 vps 上它断开连接,有人可以检查我的代码,看看我做错了什么吗?

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

计时器:

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

现在这部分只是来自我的 vps 的服务器日志:

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

[01:12:15] [LAUNCHER] 连接到套接字服务器.|客户 ID:[0] |ClientIP: [92.241.153.29] [01:12:15] 远程客户端 [0] 已发送:Joan_Mackey [01:12:16] [LAUNCHER] 播放器插座断开|客户 ID:[0] |玩家 ID:[0]

[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]

推荐答案

您应该将 Dim client As New TcpClient() 初始化移动到类级别.类级别意味着您将它放在任何方法之外(即,在任何 SubFunction 之外).

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).

就目前而言,您的 TcpClient 在退出您放入的任何方法后由垃圾收集器收集.在您通过 End Sub 后,TcpClient 在技术上不再使用,因为您无法再访问该特定实例.垃圾收集器在运行时会对此进行检查,当您的变量不再使用时,它将被收集.

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.

当垃圾收集器收集一个对象时,它开始释放该对象未使用的资源.如果对象(或任何子对象)实现了 IDisposable 接口,垃圾收集器将首先调用该对象的 Dispose() 方法.

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 建立在 System.Net.Sockets.Socket 类之上.Socket 类实现了 IDisposable,这意味着 它有一个 Dispose() 方法.垃圾收集器将为您的 TcpClient 的底层套接字调用此方法.当它被调用时,套接字将自动关闭并告诉端点它正在断开连接 - 这就是您所经历的.

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.

总结一下:

  1. 通过End Sub后不再使用TcpClient.

当垃圾收集器运行时,它将开始释放TcpClient的资源,并在底层套接字上调用Dispose().

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.

这篇关于为什么我的 TCP 连接关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆