Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [VB.NET] Piccolo aiuto con una variabile Byte ...

    Ho tradotto un codice da C# a VB.NET

    ho trovato un solo errore, non so come tradurre il rigo:

    come si vede nel commento, in C# funziona cosi: '( in C# ) ---> Byte[] RecvBytes = new Byte[2256];


    in VB.NET cosi non da errore ma non mi funziona
    Dim RecvBytes(2256) As Byte


    se faccio cosi mi dice che Byte non ha costruttori....
    Dim RecvBytes As Byte New Byte(2256)


    Quale e' il metodo giusto ???



    PS. di seguito al codice VB.NET ho riportato l'originale commentato in C#



    codice:
    Imports System
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    
    Module Module1
    
        Sub Main()
    
            'scelgo l'host 
            Dim IPHost As IPHostEntry = Dns.Resolve("localhost") 'IPHostEntry(IPHost = Dns.Resolve("localhost"))
            Console.WriteLine(IPHost.HostName)
    
            Dim aliases() As String = IPHost.Aliases 'string []aliases = IPHost.Aliases
    
            Dim addr As IPAddress() = IPHost.AddressList 'IPAddress[] addr = IPHost.AddressList
            Console.WriteLine(addr(0))
    
            Dim ep As EndPoint = New IPEndPoint(addr(0), 80) 'EndPoint ep = new IPEndPoint(addr[0],80)
    
            Dim sock As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 'Socket(sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
    
            sock.Connect(ep)
            If sock.Connected Then Console.WriteLine("OK")
    
            Dim ASCII As Encoding = Encoding.ASCII 'Encoding = ASCII = Encoding.ASCII
    
    
            Dim protocol As String = "HTTP/1.0" 'string protocol="HTTP/1.0" ; 
            Dim br As String = "\r\n" 'string br="\r\n" ; 
            Dim target As String = "/phpinfo.php" 'string target="/phpinfo.php" ; 
    
    
            Dim req_body As String = "" 'string req_body="" ;
            req_body += "&to=5" 'req_body+="&to=5"; 
    
    
            Dim headers As String = "POST " + target + " " + protocol + br 'string headers="POST "+target+" "+protocol+br ; 
            headers += "Content-Type: application/x-www-form-urlencoded" + br 'headers+="Content-Type: application/x-www-form-urlencoded"+br ; 
            headers += "Content-Length: " + req_body.Length.ToString + br + br 'headers+="Content-Length: "+req_body.Length+br+br ;
    
    
            Dim ByteGet As Byte() = ASCII.GetBytes(headers + req_body) 'Byte[] ByteGet = ASCII.GetBytes(headers+req_body); 
    
            '( in C# ) ---> Byte[] RecvBytes = new Byte[2256]; 
            Dim RecvBytes(2256) As Byte '= New Byte(2256) '### se faccio cosi mi dice che Byte non ha costruttori.... ###
    
            sock.Send(ByteGet, ByteGet.Length, 0) 'sock.Send(ByteGet, ByteGet.Length, 0); 
            Dim bytes As Int32 = sock.Receive(RecvBytes, 0, RecvBytes.Length, 0) 'Int32 bytes = sock.Receive(RecvBytes,0, RecvBytes.Length, 0); 
            Dim strRetPage As String = "" 'string strRetPage=""; 
            strRetPage = ASCII.GetString(RecvBytes, 0, RecvBytes.Length) 'strRetPage = ASCII.GetString(RecvBytes, 0,RecvBytes.Length); 
            Console.WriteLine(strRetPage)
    
    
            strRetPage = Nothing 'strRetPage = null 
            strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes) 'strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes); 
            While bytes > 0
                bytes = sock.Receive(RecvBytes, 0, RecvBytes.Length, SocketFlags.Peek)
                strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes)
                Console.WriteLine(strRetPage)
            End While
            sock.Shutdown(SocketShutdown.Both)
            sock.Close()
    
    
            'STOP codice
            Console.WriteLine()
    
        End Sub
    
    End Module

  2. #2
    Utente di HTML.it L'avatar di cassano
    Registrato dal
    Aug 2004
    Messaggi
    3,002
    se è una matrice cosi

    Dim RecvBytes(2256) as byte

  3. #3
    OK, Funziona, molte grazie, mi hai fatto un gran piacere.

    Emm!!!
    pero ho un altro problema ora...............


    1) Invio la Stringa per la Query a PHP, e fin qui' tutto OK!
    2) Mi ritorna uno Stream di dati e cioe' un elenco di nomi di "Agenti" (vedi codice...)
    3) Dovrei potere esplodere questa stringa di ritorno in tante sottostringhe contenenti un nome ciascuna, per poi popolarci la mia ListView


    se fossi in PHP "esploderei la stringa dato un carattere da decidere", in un Array di dati, per poi fare un ciclo "foreach" Popolando la "ListView".


    ma in VB.NET non so come si fa per esplodere una stringa .........



    codice:
            Dim lipa As IPHostEntry = Dns.Resolve("localhost")
            Dim addr As IPAddress() = lipa.AddressList
            Dim lep As New IPEndPoint(lipa.AddressList(0), 80)
            Dim s As New Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            Try
                s.Connect(lep)
            Catch e As Exception
                MsgBox(("Exception Thrown: " + e.ToString()))
            End Try
    
            Dim br As String = vbCrLf
            Dim protocol As String = "HTTP/1.0"
            Dim target As String = "/phpinfo.php"
    
            Dim body As String = "sql=SELECT Nome_Agente FROM Agenti"
    
            Dim headers As String = "POST " & target & " " & protocol & br
            headers += "Content-Type: application/x-www-form-urlencoded" & br
            headers += "Content-Length: " & body.Length.ToString & br & br
    
            Dim msg As Byte() = Encoding.ASCII.GetBytes(headers & body)
            Dim i As Integer = s.Send(msg, 0, msg.Length, SocketFlags.None)
    
    
            Dim RecvBytes(1024) As Byte
            Dim strRetPage As String = Nothing
            Dim ASCII As Encoding = Encoding.ASCII
            Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0)
    
    
            'OUTPUT
            MsgBox(Encoding.ASCII.GetString(RecvBytes))
            s.Shutdown(SocketShutdown.Both)
            s.Close()

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.