Ciao, pensi che in questo modo possa funzionare?
codice:
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Net.Sockets.TcpClient
Imports System.Windows.Forms.Timer
Public Class frmPabx
Dim client As TcpClient 'Variabile client
Dim stream As NetworkStream 'Variabile stream di dati client-server
Private Sub frmPbx_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnConnetti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnetti.Click
Dim indirizzo As String = txtIP.Text 'Metto l'ip dentro ad Indirizzo IP
Dim porta As String = txtPorta.Text 'Metto la porta TCP a porta
client = New TcpClient 'cliente è un nuovo client
client.Connect(indirizzo, porta) 'Connetti cliente all'indirizzo ip attraverso la porta tcp
Dim stream As NetworkStream = client.GetStream() 'flusso è l’invio/ricezione(Stream)di dati con il server.
Dim Write As System.IO.StreamWriter
Write = IO.File.CreateText("C:\recorder.txt") 'Crea il file txt.
' Examples for CanRead, Read, and DataAvailable.
' Check to see if this NetworkStream is readable.
If stream.CanRead Then
Dim myReadBuffer(1024) As Byte
Dim myCompleteMessage As StringBuilder = New StringBuilder()
Dim numberOfBytesRead As Integer = 0
' Incoming message may be larger than the buffer size.
Do
numberOfBytesRead = stream.Read(myReadBuffer, 0, CInt(myReadBuffer.Length))
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead))
Loop While stream.DataAvailable
' Print out the received message to the console.
Console.WriteLine(("You received the following message : " + myCompleteMessage.ToString()))
Else
Console.WriteLine("Sorry. You cannot read from this NetworkStream.")
End If
Return
End Sub
Pensi che il file txt venga compilato correttamente?
Pensi che creare un timer per rimpetere ad intervalli il mio ciclo possa essere giusto?
Se si, come lo posso usare all'interno del mio evento click?
(ti ricordo che io devo solamente ricevere righe di testo da un server e riaprire e chiudere il file txt solo quando il server mi invia la/le righe.
Grazie tante.