|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
[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 |
|
Senior Member
Iscritto dal: Apr 2004
Città: Lariano (RM)
Messaggi: 1372
|
Non conosco il C, comunque...
In VB sia 6.0 che .NET le variabili Byte accettano valori da 0 a 255. Scrivendo Dim RecvBytes(2256) As Byte stai creando un buffer dati da 2256 bytes. Se non ho capito male questa variabile RecvBytes è utilizzata per "parcheggiarci" i dati in arrivo dal socket; uno sguardo veloce alla MSDN Library mi dice: ... Dim bytes(1024) As Byte s.Receive(bytes, 0, s.Available, SocketFlags.None) ... Dove s è la tua sock e bytes è la tua RecvBytes. Prova un pò...
__________________
Careful With That Axe |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
Veroooo!!!
grazie mille, vado a provare, ciao! |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2000
Messaggi: 1209
|
FUNZIONAAAA!!!
Ho pure portato il progetto in "Visuale" perche devo potere "popolare" una "ListView" con il risultato. Un altro problemino: 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 ......... 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()
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 08:56.



















