使用套接字的Tcp握手

时间:2014-03-12 18:32:02

标签: vb.net

我试图与实时的msging protcol建立TCP连接,但这似乎对我没用。

当我查看通讯时,c0匹配s0c1匹配s2S1C2不匹配。 编辑:

Public server As String = "209.212.144.108"
Public port As Integer = 1936
Public socket As Socket
Public input As NetworkStream
Public output As BufferedStream

Sub Main()
    Dim t As New Task(Sub() Connect(server, port))
    t.Start()
    Console.ReadKey()
End Sub

Public Sub Connect(ByVal server As String, ByVal port As Integer)
    socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    Dim dns As DnsEndPoint = New DnsEndPoint(server, port)
    socket.SendTimeout = 100000
    socket.ReceiveTimeout = 100000
    socket.NoDelay = True
    socket.Connect(dns)
    input = New NetworkStream(socket)
    output = New BufferedStream(input)
    DoHandshake()
End Sub

Private Sub DoHandshake()
    Dim bWriter As BinaryWriter = New BinaryWriter(output)
    Dim C0 As Byte = &H3
    bWriter.Write(C0)

    ' C1

    Dim randC1 As Byte() = New Byte(1527) {}
    rand.NextBytes(randC1)
    bWriter.Write(0)
    bWriter.Write(0)
    bWriter.Write(randC1, 0, randC1.GetLength(0))
    Debug.WriteLine("Length: {0}", randC1.GetLength(0))
    bWriter.Flush()

    ' S0
    Dim S0 As Byte = CByte(input.ReadByte())
    If S0 <> &H3 Then
        Throw New Exception("Server returned incorrect version in handshake: " & S0)
    End If

    ' S1
    Dim S1 As Byte() = New Byte(1535) {}
    input.Read(S1, 0, 1536)

    ' S2
    Dim S2 As Byte() = New Byte(0 To 1535) {}
    input.Read(S2, 0, 1536)
    ' C2

    bWriter.Write(S1, 0, 0)
    bWriter.Write(S1, 0, 0)
    bWriter.Write(S1, 0, 1536)
    bWriter.Flush()
End Sub

0 个答案:

没有答案