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

    [VB.NET] Niente WinSock, c'è System.NET ma . . .

    Ciao a tutti!

    Ho installato il Visual Basic 2005 Express (prima programmavo in Viual Basic 6) ed ho cercato il controllo WinSock da inserire nel programma, purtroppo ho scoperto che tale elemento non è presente in questa nuova versione del VB!

    Navigando sulla rete mi è parso di capire che è perà presente la una funzionalità, System.NET in grado di permettermi di fare ciò che facevo con WinSock, potete confermarmi ciò?

    Ho cercato nella guida in linea informazioni su System.NET ma non ci ho capito molto, conoscete una buona guida in italiano (possibilmente) che spieghi il funzionamento e insegni almeno come fare le azioni fondamentali, come il traffico di dati tra una applicazione client e una server?

    Grazie.

  2. #2
    Prova a dare un'occhiata su msdn

    msdn


    e poi a questo esempio:

    Imports System
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Imports System.Web.UI.WebControls
    Namespace MyApps

    'Classe per la connessione via socket
    Public Class clSocket
    Private s As Socket

    Private Fdns As String
    Public ReadOnly Property dns() As String
    Get
    Return Fdns
    End Get
    End Property

    Private Fport As Integer
    Public ReadOnly Property port() As Integer
    Get
    Return Fport
    End Get
    End Property

    Private Fuser As String
    Public ReadOnly Property user() As String
    Get
    Return Fuser
    End Get
    End Property

    Private Fpassword As String
    Public ReadOnly Property password() As String
    Get
    Return Fpassword
    End Get
    End Property

    Public Sub clSocket()
    End Sub

    'Costruttore
    ' <param name="dns">Il server a cui collegarsi</param>
    ' <param name="port">La porta</param>
    ' <param name="user">Nome utente</param>
    ' <param name="password">Password</param>
    Public Sub clSocket(ByVal dns As String, ByVal port As Integer, ByVal user As String, ByVal password As String)
    Fdns = dns
    Fport = port
    Fuser = user
    Fpassword = password
    Connect()
    End Sub

    ' Crea la connessione al socket
    ' <returns>True se va a buon fine</returns>
    Public Function Connect() As Boolean
    s = Nothing
    Dim hostEndPoint As IPEndPoint
    Dim hostAddress As IPAddress = Nothing
    Dim hostInfo As IPHostEntry = System.Net.Dns.Resolve(Me.dns)
    Dim IPaddresses() As IPAddress = hostInfo.AddressList
    hostAddress = IPaddresses(0)
    hostEndPoint = New IPEndPoint(hostAddress, Me.port)
    Try
    s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    s.Connect(hostEndPoint)
    Return s.Connected
    Catch err As Exception
    s.Shutdown(SocketShutdown.Both)
    s.Close()
    Throw err
    End Try
    End Function

    ' Esegue il login
    ' <returns>True se va a buon fine</returns>
    Public Function login() As Boolean
    SendCommand("USER " + Me.user)
    SendCommand("PASS " + Me.password)
    If (ReceiveCommand("login").IndexOf("200 login OK, proceed\r\n\0") > -1) Then
    clearBuffer()
    Return True
    Else
    Return False
    End If
    End Function

    ' Esegue la disconnessione
    Public Sub Disconnect()
    s.Shutdown(SocketShutdown.Both)
    s.Close()
    s = Nothing
    End Sub

    ' Invia un comando via socket
    ' <param name="command">Il nome del comando inviato</param>
    Public Sub SendCommand(ByVal command As String)
    Dim msg As Byte()
    msg = Encoding.ASCII.GetBytes(command + "\r\n")
    s.Send(msg, 0, msg.Length, SocketFlags.None)
    End Sub

    ' Legge la risposta (dopo l'invio di un comando
    ' <param name="sTerminate">Il carattere di fine che cia aspettiamo</param>
    ' <returns>La risposta</returns>
    Public Function ReceiveCommand(ByVal sTerminate As String) As String
    Dim str As String = ""
    Dim iRx As Integer = 0
    Dim buffer(1024) As Byte
    While (True)
    If (s.Available > 0) Then
    iRx = s.Receive(buffer)
    str += Encoding.ASCII.GetString(buffer)
    If str.IndexOf(sTerminate) > -1 Or str.IndexOf("512") > -1 Then
    Return str
    End If
    End If
    End While
    End Function

    ' Svuota il buffer di ricazione
    Public Sub clearBuffer()
    Dim iRx As Integer
    Dim buffer(1024) As Byte
    While (s.Poll(100000, SelectMode.SelectRead))
    iRx = s.Receive(buffer)
    End While
    End Sub
    End Class
    End Namespace

    Un esempio di utilizzo

    'istanzio la classe
    Dim cs As clSocket = New clSocket("web.myDomain.com", port, User, password)
    Try
    'eseguo il login
    cs.login()
    'invio un comando
    cs.SendCommand("NOMECOMANDO")
    'leggo la risposta
    cs.ReceiveCommand("ENDCHAR")
    Catch
    'gestione eccezioni
    Finally
    cs.Disconnect()
    End Try

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.