System.dll中发生SocketException类型的第一次机会异常

时间:2013-07-29 07:48:47

标签: vb.net

这是我的班级。我正在做的是 - 我正在连接2​​个“会话”,一个连接到我的游戏客户端,一个连接到我的服务器,当我从客户端接收数据时 - 我将它发送到服务器,当我收到数据时从服务器 - 我发送到客户端。出于某种奇怪的原因,我在Visual Studio中收到错误。我担心这就是为什么我不从服务器接收任何数据。这是我的班级..我怎么能找到原因?

Imports MapleLib.PacketLib
Imports System.Threading
Imports System.Net.Sockets

Structure Headers
    Const Login_Appear = "37"
    Const World_Select = "1C"
    Const Channel_Select = "2F"
End Structure

Public Class InterceptedLinkedClient

    Private Block As Boolean = False
    Private CharID As Integer = -1
    Private Connected As Boolean = True
    Private GotEncryption As Boolean = False
    Private Mutex As Mutex = New Mutex
    Private Mutex2 As Mutex = New Mutex
    Private inSession As Session
    Private outSession As Session
    Private Port As UShort

    Public Sub New(Inside As Session, pIP As String, pPort As UShort)
        Port = pPort
        inSession = Inside

        AddHandler inSession.OnPacketReceived, AddressOf Inside_OnPacketRecived
        AddHandler inSession.OnClientDisconnected, AddressOf Inside_OnClientDisconnected
        'inSession.WaitForData()

        ConnectOut(pIP, pPort)
    End Sub

    Private Sub ConnectOut(IP As String, Port As Integer)
        Try
            Dim outSocket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
            outSocket.BeginConnect(IP, Port, New AsyncCallback(AddressOf OnOutConnectCallBack), outSocket)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            OutSession_OnClientDisconnected(Nothing)
        End Try
    End Sub

    Private Sub Inside_OnClientDisconnected(pSession As Session)
        If outSession IsNot Nothing Then
            outSession.Socket.Shutdown(SocketShutdown.Both)
        End If

        Connected = False
    End Sub

    Private Sub Inside_OnPacketRecived(pPacket As Byte())
        If Connected AndAlso Not Block Then
            Mutex.WaitOne()
            Try
                Dim Header As String = GetByteArray(pPacket).Substring(0, 2)
                Select Case Header
                    Case Headers.Login_Appear
                        SendInformation()

                        'Dummy packets (Used for testing).
                        'SendLoginAuth("Admin", "Admin")
                        'SendWorld("Scania")
                        'SendWorldEnd()
                        'Case Headers.World_Select
                        '    SendWorldSelect()
                        'Case Headers.Channel_Select
                        '    ChannelSelect()
                    Case Else
                        outSession.SendPacket(pPacket)
                End Select
            Finally
                Mutex.ReleaseMutex()
            End Try
        End If
    End Sub

    Private Function GetByteArray(pPacket As Byte(), Optional pEncrypt As Boolean = True) As String
        Dim ret As String = ""
        For Each b As Byte In pPacket
            ret &= String.Format("{0:X2} ", b)
        Next

        Return ret
    End Function

    Private Sub OnOutConnectCallBack(AR As IAsyncResult)
        Dim sock As Socket = DirectCast(AR.AsyncState, Socket)
        Try
            sock.EndConnect(AR)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Connected = False
            inSession.Socket.Shutdown(SocketShutdown.Both)
            Return
        End Try

        If outSession IsNot Nothing Then
            outSession.Socket.Close()
            outSession.Connected = False
        End If

        Dim session As Session = New Session(sock, SessionType.CLIENT_TO_SERVER)
        outSession = session
        AddHandler outSession.OnInitPacketReceived, AddressOf OutSession_OnInitPacketReceived
        AddHandler outSession.OnPacketReceived, AddressOf OutSession_OnPacketRecieved
        AddHandler outSession.OnClientDisconnected, AddressOf OutSession_OnClientDisconnected
        outSession.WaitForDataNoEncryption()
    End Sub

    Private Sub OutSession_OnClientDisconnected(pSession As Session)
        If Not Block Then
            inSession.Socket.Shutdown(SocketShutdown.Both)
            'Log("Out disconnected (" & Port & ").")
            Connected = False
        End If
    End Sub

    Private Sub OutSession_OnInitPacketReceived(pVersion As Short, pServerIdentifier As Byte, pStr As String)
        ' MessageBox.Show("Version: " & pVersion & ", Locale: " & pServerIdentifier & ".")
        SendHandshake()
    End Sub

    Private Sub OutSession_OnPacketRecieved(pPacket As Byte())
        MessageBox.Show("Got packet from server: " & GetByteArray(pPacket))
        If GotEncryption AndAlso Connected Then
            Mutex2.WaitOne()
            Try

                inSession.SendPacket(pPacket)
            Finally
                Mutex2.ReleaseMutex()
            End Try
        End If
    End Sub

    Private Sub SendHandshake()
        Dim Version As Short = frmMain.Maple_Version
        Dim rnd As Random = New Random()
        Dim RecvIV As MapleLib.MapleCryptoLib.MapleCrypto
        Dim SendIV As MapleLib.MapleCryptoLib.MapleCrypto
        RecvIV = New MapleLib.MapleCryptoLib.MapleCrypto(BitConverter.GetBytes(rnd.Next()), Version)
        SendIV = New MapleLib.MapleCryptoLib.MapleCrypto(BitConverter.GetBytes(rnd.Next()), Version)

        inSession.SIV = SendIV
        inSession.RIV = RecvIV

        Dim Packet As PacketWriter = New PacketWriter
        Packet.WriteShort(14)
        Packet.WriteShort(Version)
        Packet.WriteMapleString("1")
        Packet.WriteBytes(RecvIV.IV)
        Packet.WriteBytes(SendIV.IV)
        Packet.WriteByte(8)
        GotEncryption = True
        inSession.SendRawPacket(Packet.ToArray)
        'MessageBox.Show(Packet.ToArray.ToString)

        'Packet.WriteShort(13 + pStr.Length)
        'Packet.WriteShort(pVersion)
        'Packet.WriteMapleString(pStr)
        'Dim Buffer As Byte() = New Byte(4) {}
        'Dim Buffer2 As Byte() = New Byte(4) {}
        'Dim Random As Random = New Random
        'Random.NextBytes(Buffer)
        'Random.NextBytes(Buffer2)
        'inSession.RIV = New MapleLib.MapleCryptoLib.MapleCrypto(Buffer, pVersion)
        'inSession.SIV = New MapleLib.MapleCryptoLib.MapleCrypto(Buffer2, pVersion)
        'Packet.WriteBytes(Buffer)
        'Packet.WriteBytes(Buffer2)
        'Packet.WriteByte(pServerIdentifier)
        'GotEncryption = True
        'inSession.SendRawPacket(Packet.ToArray)
    End Sub

    Public Sub SendInformation()
        Dim Packet As PacketWriter = New PacketWriter
        'Packet.WriteShort(&H1)
        Packet.WriteShort(&H2D)
        'Packet.WriteMapleString(frmMain.Username_)

        outSession.SendPacket(Packet.ToArray)
    End Sub

    Public Sub SendLoginAuth(Username As String, Password As String)
        Dim Packet As PacketWriter = New PacketWriter
        Packet.WriteShort(&H0)

        Packet.WriteInt(0)
        Packet.WriteShort(0)
        Packet.WriteInt(0) ' accid
        Packet.WriteLong(0)
        Packet.WriteByte(0)
        Packet.WriteMapleString(Username)
        Packet.WriteLong(3)
        Packet.WriteShort(0)
        Packet.WriteLong(0)
        Packet.WriteByte(1)
        Packet.WriteShort(6)
        Packet.WriteShort(0)
        Packet.WriteByte(1)
        Packet.WriteByte(2)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(1)
        Packet.WriteByte(4)
        Packet.WriteLong(DateTime.Now.ToFileTimeUtc())
        inSession.SendPacket(Packet.ToArray)
    End Sub

    Private Sub SendWorldSelect()
        Dim Packet As PacketWriter = New PacketWriter
        Packet.WriteShort(&H1C)
        Packet.WriteByte(1) ' ID
        Packet.WriteByte(0) 'IDK.

        inSession.SendPacket(Packet.ToArray)
    End Sub

    Private Sub ChannelSelect()
        Dim Packet As PacketWriter = New PacketWriter
        Packet.WriteShort(&H1B)
        Packet.WriteByte(2)
        Packet.WriteByte(0) ' world id
        Packet.WriteByte(0)
        Packet.WriteByte(0) ' Channel id
        Packet.WriteShort(0)
        Packet.WriteByte(8) 'IDK.

        inSession.SendPacket(Packet.ToArray)
    End Sub

    Public Sub SendWorld(pName As String)
        Dim Packet As PacketWriter = New PacketWriter
        Packet.WriteShort(&H9)
        Packet.WriteByte(0) ' ID
        Packet.WriteMapleString(pName)
        Packet.WriteByte(1) 'Flag.
        Packet.WriteMapleString("")
        Packet.WriteShort(100) ' rate modifier.
        Packet.WriteShort(100)
        Packet.WriteByte(0)
        Packet.WriteByte(1)
        Packet.WriteMapleString(pName & "-1")
        Packet.WriteInt(0)
        Packet.WriteByte(0)
        Packet.WriteShort(0)
        Packet.WriteShort(0)
        Packet.WriteInt(0)

        inSession.SendPacket(Packet.ToArray)

    End Sub

    Public Sub SendWorldEnd()
        Dim Packet As PacketWriter = New PacketWriter
        Packet.WriteShort(&H9)
        Packet.WriteByte(&HFF)
        Packet.WriteByte(0)

        inSession.SendPacket(Packet.ToArray)
    End Sub

    Public Sub Log(Message As String)
        MessageBox.Show(Message)
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

可能有很多原因。或者在.Net框架内引发和处理异常。所以忽略它,看看你的应用程序是否有效。它可能是防火墙。转过来确定。您确定要连接到正确的主机+端口吗?主机是否可在网络中ping通?你能用telnet打开端口吗?