Question: How can I send a string of characters in Visual Basic by pressing a button? I want the string of characters to be hard-coded. I
How can I send a string of characters in Visual Basic by pressing a button? I want the string of characters to be hard-coded. I have the peer-to-peer connection already set up, but I currently have it so that I have to insert the string in a text box, then send it. I don't want that. I want the string hard-coded so that when I press the button, I send the preset string.
Another issue is, my string doesn't get sent unless I press the stop button in my Visual Studio IDE. How could I resolve this issue in the code?
Here is my current code:
Imports System.Net.Sockets Imports System.Threading Imports System.IO Public Class Form1
Dim Listener As New TcpListener(5025) Dim Client As New TcpClient Dim Message As String = "" Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim ListThread As New Thread(New ThreadStart(AddressOf Listening)) ListThread.Start() End Sub
Private Sub Listening() Listener.Start() End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Client = New TcpClient("10.10.13.165", 5025)
Dim Writer As New StreamWriter(Client.GetStream()) Writer.Write(TextBox1.Text) Writer.Flush()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick If Listener.Pending = True Then Message = "" Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream()) While Reader.Peek > -1 Message = Message + Convert.ToChar(Reader.Read()).ToString End While
MsgBox(Message, MsgBoxStyle.OkOnly) End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Listener.Stop() End Sub
End Class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
