Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Class Form1
Private client As TcpClient
Public STR As StreamReader
Public STW As StreamWriter
Public receive As String
Public receiveAsync As String
Public texttosend As String
Public Sub New()
' Chiamata richiesta dalla finestra di progettazione.
InitializeComponent()
' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
Dim localIP As IPAddress() = Dns.GetHostAddresses(Dns.GetHostName)
For Each address As IPAddress In localIP
If address.AddressFamily = AddressFamily.InterNetwork Then
ServerIPTextBox.Text = address.ToString()
End If
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RadioButton1.Checked = True
End Sub
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
If ServerIPTextBox.Text = "" Or ServerPort.Text = "" Then
MsgBox("Attenzione! Inserire indirizzo IP o la porta !")
Else
Dim listener As New TcpListener(IPAddress.Any, Integer.Parse(ServerPort.Text))
listener.Start()
client = listener.AcceptTcpClient()
STR = New StreamReader(client.GetStream())
STW = New StreamWriter(client.GetStream())
STW.AutoFlush = True
BackgroundWorker1.RunWorkerAsync()
BackgroundWorker2.WorkerSupportsCancellation = True
End If
End Sub
Private Sub ConnectButton_Click(sender As Object, e As EventArgs) Handles ConnectButton.Click
client = New TcpClient
Dim IPEnd As New IPEndPoint(IPAddress.Parse(ClientIP.Text), Integer.Parse(ClientPort.Text))
Try
client.Connect(IPEnd)
If (client.Connected) Then
ChatScreenTextBox.AppendText(" Connesso al server " + Environment.NewLine)
STR = New StreamReader(client.GetStream())
STW = New StreamWriter(client.GetStream())
STW.AutoFlush = True
BackgroundWorker1.RunWorkerAsync()
BackgroundWorker2.WorkerSupportsCancellation = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub
Private Async Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While (client.Connected)
Try
receive = STR.ReadLine
receiveAsync = Await STR.ReadLineAsync
Me.ChatScreenTextBox.Invoke(New MethodInvoker(Function()
ChatScreenTextBox.AppendText("YOU: " + receive + Environment.NewLine)
End Function))
receive = ""
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End While
End Sub
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
If (client.Connected) Then
STW.WriteLine(texttosend)
'STW.WriteLineAsync(texttosend)
Me.ChatScreenTextBox.Invoke(New MethodInvoker(Function()
ChatScreenTextBox.AppendText("ME: " + texttosend + Environment.NewLine)
End Function))
Else
MessageBox.Show("Sending Failed")
End If
BackgroundWorker2.CancelAsync()
End Sub
Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles SendButton.Click
If (MessageTextBox.Text <> "") Then
texttosend = MessageTextBox.Text
BackgroundWorker2.RunWorkerAsync()
End If
MessageTextBox.Text = ""
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
GroupBox1.Enabled = True
GroupBox2.Enabled = False
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
GroupBox1.Enabled = False
GroupBox2.Enabled = True
End Sub
End Class