简体   繁体   English

vb.net 2010套接字突然停止?

[英]vb.net 2010 Sockets suddenly stopping?

I am currently creating a multi-client server application in vb.net 2010. I am using sockets and network streams to read the data. 我目前正在vb.net 2010中创建一个多客户端服务器应用程序。我正在使用套接字和网络流来读取数据。

it's hard to explain what is happening and posting code probably won't do much good being the scale of my project. 很难解释正在发生什么,发布代码可能对我的项目规模没有多大帮助。 However, i'll just give a run through of what i am doing and then what happens and hopefully someone can help me, or give me some ideas on what could be happening :). 但是,我只简单介绍一下我在做什么,然后发生什么,并希望有人可以帮助我,或者就可能发生的事情给我一些想法:)。

I start up the server and client and they both connect and listen asynchronously. 我启动服务器和客户端,它们都连接并异步监听。 In my client, i put in the login information and send it to the server using the network stream. 在我的客户端中,我输入了登录信息,并使用网络流将其发送到服务器。 The server reads everything fine, starts listening again, handles the data, sends account information to client, says the client is connected, and then randomly closes once it's done handling the data and exits the function. 服务器会正​​常读取所有内容,再次开始侦听,处理数据,将帐户信息发送给客户端,说客户端已连接,然后在处理完数据后随机关闭并退出功能。 While this is happening, the client is receiving the data, starts listening again, handles the data, says the socket is connected, and then once everything is done, randomly closes after the function is done. 发生这种情况时,客户端将接收数据,再次开始侦听,处理数据,说套接字已连接,然后在完成所有操作后,在功能完成后随机关闭。

Now I have already made sure that other code wasn't affecting the sockets, and commented everything out that wasn't needed and would allow each the server and client to keep looping. 现在,我已经确保其他代码不会影响套接字,并注释掉了不需要的所有内容,并使服务器和客户端保持循环。 (creating a game so there's a serverloop and gameloop) It just seems like it does all the work and then exits out the reading sub, and just stops listening for more to read. (创建一个游戏,因此有一个serverloop和gameloop)看起来好像它完成了所有工作,然后退出了子阅读,然后停止了听更多的阅读。

If anyone has any idea's it would be greatly appreciated. 如果有人有任何想法,将不胜感激。 Thanks in advance! 提前致谢!

The code is the same on each side: 两侧的代码相同:

Receiving data: 接收数据:

    Private Sub ReceiveOneByte(ByVal ar As IAsyncResult)
    Dim netStream As NetworkStream
    Dim InData() As Byte
    Dim DataLen(3) As Byte
    Dim inLength As Integer
    Dim Buffer As New clsBuffer
    Dim MsgType As Integer
    Dim NewData() As Byte
    Dim Empty() As Byte

    SyncLock Client_TCP.Client
        Try
            netStream = New NetworkStream(Client_TCP.Client)
        Catch ex As Exception
        End Try
        ' gets length of data comming in
        Try
            netStream.Read(DataLen, 0, 4)
        Catch ex As Exception
        End Try
        inLength = BitConverter.ToInt32(DataLen, 0)

        ' gets data comming in
        ReDim InData(inLength)
        netStream.Read(InData, 0, inLength)
        netStream.Flush()
    End SyncLock

    Buffer.WriteBytes(InData)
    MsgType = Buffer.ReadLong

    If InData.Length > (InData.Length - 4) Then
        ReDim NewData(InData.Length - 5)
        Array.ConstrainedCopy(InData, 4, NewData, 0, NewData.Length - 1)
        HandleDataSub(MsgType)(0, NewData)
    Else
        HandleDataSub(MsgType)(0, Empty)
    End If

    Client_TCP.GetStream.BeginRead(Readbyte, 0, 1, New AsyncCallback(AddressOf ReceiveOneByte), Nothing)

End Sub

To Send Data: 发送数据:

    Public Sub Send(ByVal Data() As Byte)
    Dim netStream As NetworkStream
    Dim len As Integer = Data.Length
    Dim DataLen() As Byte = BitConverter.GetBytes(len)
    Dim bit(0) As Byte
    bit(0) = 1
    SyncLock Client_TCP.Client
        netStream = New NetworkStream(Client_TCP.Client)
        netStream.Write(bit, 0, 1)
        netStream.Write(DataLen, 0, 4)
        netStream.Write(Data, 0, Data.Length)
        'netStream.Flush()
    End SyncLock
End Sub

When you use the IAsyncResult Begin*(...) pattern, it is required that you call the End*(...) method. 当您使用IAsyncResult Begin*(...)模式时, 要求您调用End*(...)方法。 This is the place where it will tell you about any problems it had. 在这里,它将告诉您所遇到的任何问题。 It is probably trying really hard to tell you what the problem is, but you aren't checking. 可能真的很难告诉您问题出在哪里,但您没有检查。 Incidentally, in this particular case this is also where you can check the number of bytes returned - it could be 0 if the stream terminated unexpectedly. 顺便说一句,在这种特殊情况下,您还可以在其中检查返回的字节数-如果流意外终止,则它可以为0。 So the first thing to add at the top of your method is (pseudocode... ok, who am I kidding... C#): 因此,要在方法顶部添加的第一件事是(伪代码...好吧,我在跟谁开玩笑... C#):

try {
    int bytesRead = Client_TCP.GetStream.EndRead(ar);
    if(bytesRead < 1) {
        // EOF; do something
    } else if(bytesRead == 1) {
        // this is your expected scenario: your code here
    } else {
        // well that's just crazy talk!
    }
} catch (Exception ex) {
    // log this exception; shouldn't throw, because you are on a worker
    // thread here; you *might* want to clean up, though
}

This will probably get you started on diagnosing the problem. 这可能会帮助您开始诊断问题。 It should also be noted that this is not the most efficient way to use a socket - but it should work, and I don't want to confuse things by start talking about alternative async APIs etc. 还应该注意的是,这不是使用套接字的最有效方法,但是它应该可以工作,并且我不想通过谈论替代异步API等来混淆事物。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM