Salve dovrei fare un'applicazione in visual basic che si connette al mio server di asterisk ...
ora io non ho mai fatto una classa per connessioni via reti .... e non ho mai usato i socket di windows ... perciò vi volevo chiedere qualche consiglio ...
ora posto la mia classe ... il problema sta nel fatto che il socket me lo crea ... ma poi appena rimando una richiesta non va + ...
help
Imports System.Net.Sockets
Imports System.Text
Public Class clSocket
Dim tcpClient As New System.Net.Sockets.TcpClient()
Dim networkStream As NetworkStream
Private CRLF As String = ControlChars.Cr + ControlChars.Lf
Public HOST As String = "10.4.0.1"
Public PORT As String = "5038"
Public USER As String = "user"
Public PASS As String = " "
Public Sub Connetti()
tcpClient = New System.Net.Sockets.TcpClient()
tcpClient.Connect(HOST, PORT)
networkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
MsgBox("Socket creato !!", MsgBoxStyle.Information, "inf")
MandaTXTRiceviTXT(" ")
Else
If Not networkStream.CanRead Then
MsgBox("cannot not write data to this stream", MsgBoxStyle.Critical, "Errore")
Chiudi()
Else
If Not networkStream.CanWrite Then
MsgBox("cannot read data from this stream", MsgBoxStyle.Critical, "Errore")
Chiudi()
End If
End If
End If
End Sub
Public Sub Chiudi()
tcpClient.Close()
End Sub
Public Sub MandaTXT(ByVal testo As String)
Try
networkStream = tcpClient.GetStream()
' Do a simple write.
Console.WriteLine(testo)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(testo)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Errore")
End Try
End Sub
Public Function MandaTXTRiceviTXT(ByVal testo As String) As String
Try
networkStream = tcpClient.GetStream()
' Do a simple write.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(testo)
networkStream.Write(sendBytes, 0, sendBytes.Length)
' Read the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
If bytes.Length <> 0 Then
'networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
'Return Encoding.ASCII.GetString(bytes)
End If
Return "----" & CRLF
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Errore")
Return ""
End Try
End Function
End Class
Nel dubbio GASSE !!!