Torna al Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim server As TcpListener = Nothing
Try
' Setto la Porta i ascolto
Dim port As Int32 = 9999
' Setto l'IP del Server
Dim IpLocal As String = "127.0.0.1"
Dim localAddr As IPAddress = IPAddress.Parse(IpLocal)
' TcpListener server = new TcpListener(port);
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes As Byte() = Nothing
Dim data As String = Nothing
Dim ClientList As List(Of TcpClient) = Nothing
' Enter the listening loop.
TextBox1.AppendText("Waiting for a connection... ")
Do
If server.Pending Then
Dim client As TcpClient = server.AcceptTcpClient()
MsgBox("ciao")
ClientList.Add(client)
TextBox1.AppendText("Connected!")
data = Nothing
Dim stream As NetworkStream = client.GetStream()
Dim i As Integer
While Not ((i = stream.Read(bytes, 0, 256)) = 0)
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
TextBox1.AppendText("Sent: " & data)
data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
stream.Write(msg, 0, msg.Length)
TextBox1.AppendText("Sent: " & data)
End While
client.Close()
End If
Loop
Catch ex As Exception
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try
End Sub