Discussione Tcp Listener In visual basic

Hummingbird07

Utente Iron
1 Novembre 2020
12
6
0
15
Ultima modifica da un moderatore:
Ciao ragazzi, mi da errore dicendo che il tcp listener è obsoleto

Codice:
Imports System.Net.Sockets
Imports System.Text.UTF8Encoding

Public Class Form1
    Dim Server As TcpListener
    Dim cliente As TcpClient
    Dim flusso As Network.Stream
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        servitore = New TcpListener(8888)
        servitore.Start()
        Timer2.Start()
        ListBox1.Items.Add("Server Avviato")
    End Sub

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        If servitore.Pending() Then
            Timer2.Stop()
            cliente = servitore.AcceptTcpClient()
            flusso = cliente.GetStream()
            Timer1.Start()
            ListBox1.Items.Add("Si è connesso un utente in data alle " & Now())
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If cliente.Available > 0 Then
            Dim temp(cliente.Available - 1) As Byte
            flusso.Read(temp, 0, temp.Length)
            Dim testo As String = UTF8.GetString(temp)
            ListBox1.Items.Add("Client: " & testo)
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Add("Server: " & TextBox1.Text)
        Dim temp() As Byte = UTF8.GetBytes(TextBox1.Text)
        flusso.Write(temp, 0, temp.Length)
        TextBox1.Text = ""
    End Sub
End Class
 
TcpListener non e' obsoleto, solo il costruttore che hai utilizzato lo e':
servitore = New TcpListener(8888)

Se devi metterti in ascolto solo per la macchina che stai usando usa localhost, altrimenti per tutti usa 0.0.0.0 (cioe' IPAddress.Any):
Codice:
servitore = New TcpListener(IPAddress.Any, 8888)