'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