0

これは私のクラスです。私がやっていることは、2つの「セッション」で接続しています。1つはゲームクライアントに、もう1つはサーバーに接続し、クライアントからデータを受信するとサーバーに送信し、データを受信するとデータを受信しますサーバーから - クライアントに送信します。奇妙な理由で、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
4

1 に答える 1

0

いくつかの理由が考えられます。または、例外が発生し、.Net フレームワーク自体で処理されます。したがって、それを無視して、アプリケーションが機能するかどうかを確認してください。ファイアウォールである可能性があります。オフにして確認してください。正しいホスト + ポートに接続していますか? ホストはネットワークで ping 可能ですか? telnet でポートを開放できますか?

于 2013-07-29T07:56:56.557 に答える