VB.NET Socket5代理客户端链接 LHSocket5 Server源码

前端之家收集整理的这篇文章主要介绍了VB.NET Socket5代理客户端链接 LHSocket5 Server源码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


使用

        ' 下面以来接代理访问土豆网为例可直接使用IP地址或域名方式进行代理
        Dim VSocket As Net.Sockets.Socket = New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp)

        'Dim Result As Integer = SetConnection(VSocket,"192.168.1.6",60000,"U602414","P257638",1,"123.126.98.146",80)
        Dim Result As Integer = SetConnection(VSocket,2,"www.tudou.com",80)
        If Result <> 0 Then
            TextBox1.AppendText("LhS5服务器连接信息设置失败! 请查看错误代码的对应信息! 错误代码:" + Result.ToString)
            ' -1 本地连接到代理服务器或发送数据到代理服务器失败 请检查本机连接是否正常
            ' 0=成功
            ' 1=身份失败
            ' 2=服务器连接用户要代理的目标地址失败
            ' 3=服务器解析用户发送的域名地址失败
            ' 255=失败
            Return
        End If


        '发送头部
        Dim Content As Text.StringBuilder = New System.Text.StringBuilder
        Content.Append("GET /  HTTP/1.0" + vbCr + vbLf)
        Content.Append("User-Agent: Mozilla/5.0 Chrome/48.0.2564.109 Safari/537.36" + vbCr + vbLf)
        Content.Append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" + vbCr + vbLf)
        Content.Append("Host: www.tudou.com" + vbCr + vbLf + vbCr + vbLf)
        VSocket.Send(System.Text.Encoding.Default.GetBytes(Content.ToString))


        System.Threading.Thread.Sleep(1000)


        ' 返回访问网址的信息  如果读取内容不全 、可使用返回的 Content-Length中的数量进行读取
        Dim Vbyte(20000) As Byte
        Dim VIndex As Integer = VSocket.Receive(Vbyte)
        TextBox1.AppendText(System.Text.Encoding.UTF8.GetString(Vbyte) + vbCrLf)


        VSocket.Close()


操作方法

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Client"></param>
    ''' <param name="LhS5Ip">LH代理服务器程序下开通的用户账号的IP地址</param>
    ''' <param name="LhS5Port">LH代理服务器程序下开通的用户账号的端口</param>
    ''' <param name="LhS5UserName">LH代理服务器程序下开通的用户账号的账号</param>
    ''' <param name="LhS5UserPassword">LH代理服务器程序下开通的用户账号的密码</param>
    ''' <param name="ConnType">设置要代理的访问的目标类型 1=IP4  2=域名  此访问类型是告诉lh服务器要代理连接到的目标地址</param>
    ''' <param name="TragetAddress">根据上方输入内容  IP4地址 或  域名</param>
    ''' <param name="TragetProt">上方配套的连接地址对应的端口</param>
    ''' <returns></returns>
    Private Function SetConnection(ByRef Client As Socket,ByVal LhS5Ip As String,ByVal LhS5Port As UShort,ByVal LhS5UserName As String,ByVal LhS5UserPassword As String,ByVal ConnType As Integer,ByVal TragetAddress As String,ByVal TragetProt As UShort) As Integer


        ' 蓝恒网络Socket5客户端连接演示
        ' Http://www.lanheng.net
        ' 下面代码开发者可根据自行情况摘取整合进自己的程序内进行相关修改 也可以直接使用


        Dim Result As Integer = 0

        Try
            Client.Connect(Net.IPAddress.Parse(LhS5Ip),LhS5Port)
        Catch ex As Exception
            Console.WriteLine("错误信息:" + ex.Message)
            Return -1
        End Try

        Dim Vindex As Integer = 0
        Dim Size As Integer = 6 + System.Text.Encoding.ASCII.GetBytes(LhS5UserName + LhS5UserPassword).Count
        If ConnType = 2 Then
            Size += 1 + System.Text.Encoding.ASCII.GetBytes(TragetAddress).Count
        Else
            Size += 4
        End If

        Dim Vbyte(Size - 1) As Byte
        Vbyte(0) = 85
        Vbyte(1) = System.Text.Encoding.ASCII.GetBytes(LhS5UserName).Count
        Vindex = 2
        For Each i As Byte In System.Text.Encoding.ASCII.GetBytes(LhS5UserName)
            Vbyte(Vindex) = i
            Vindex += 1
        Next

        Vbyte(Vindex) = System.Text.Encoding.ASCII.GetBytes(LhS5UserPassword).Count
        Vindex += 1
        For Each i As Byte In System.Text.Encoding.ASCII.GetBytes(LhS5UserPassword)
            Vbyte(Vindex) = i
            Vindex += 1
        Next

        Select Case ConnType
            Case 1
                Vbyte(Vindex) = 1
                Vindex += 1
                For Each i As Byte In Net.IPAddress.Parse(TragetAddress).GetAddressBytes
                    Vbyte(Vindex) = i
                    Vindex += 1
                Next
            Case 2
                Vbyte(Vindex) = 2
                Vindex += 1
                Vbyte(Vindex) = System.Text.Encoding.ASCII.GetBytes(TragetAddress).Count
                Vindex += 1
                For Each i As Byte In System.Text.Encoding.ASCII.GetBytes(TragetAddress)
                    Vbyte(Vindex) = i
                    Vindex += 1
                Next
        End Select


        For Each i As Byte In BitConverter.GetBytes(BitConverter.ToUInt16(BitConverter.GetBytes(Net.IPAddress.HostToNetworkOrder(TragetProt)),2))
            Vbyte(Vindex) = i
            Vindex += 1
        Next
        Client.Send(Vbyte)

        ReDim Vbyte(1)
        Vindex = Client.Receive(Vbyte)
        If Vindex < 2 Then
            Console.WriteLine("获取服务器返回数据失败")
            Client.Close()
            Return -1
        Else
            Return Vbyte(1)
        End If

    End Function


注意:必须是链接到 蓝恒Socket5 Server 搭建的服务器才可以

官方帮助信息:http://www.lanheng.net/ProductContent/39/85

猜你在找的VB相关文章