Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the VB.net code below, please solve the issue. The program runs, but the problem is, when I want to send a string to another

Given the VB.net code below, please solve the issue. The program runs, but the problem is, when I want to send a string to another device, I have to close my program in order for the string to be sent (or received by the recipient). How can I fix this code so that I do not have to close my program to successfully send the string?

Here is the image of the code, with copy and pastable code below it. In the GUI I made, the user inputs the IP address, then clicks button to send a hard-coded string. My code is mostly based from the following link, but I did make changes like allowing the user to input the IP address, and hard-coding the string. http://www.dreamincode.net/forums/topic/21431-p2p-connections/

image text in transcribed

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 Me.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(TextBox1.Text, 5025) Dim Writer As New StreamWriter(Client.GetStream()) Writer.Write("*RST") 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 Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

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

File Edit View Project Build Debug Team Tools Test Analyze Window Help Full Screen Quick Launch (Ctrl+Q) Form1.vb" Form1.vb [Designl WindowsAppl f (Form1 Events) FormClosing EIrports System.Net.Sockets Irports System.Threading 3 Irports System.IO 1 reference Public Class Formi Dim Listener As New TcpListener (5825 Dim Client As New TcpClient Dim Message As String- Private Sub Forml Load (ByWal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ListThreed As New Thread(New ThreadStart(Addressof Listening)) ListThread.Start() 12 13 End Sub 1 reference Private Sub Listening) 16 Listener.Start) End Sub O references Private Sub Button1 Click(ByVal sender As System.Object, ByVal e As Systen.EventArgs) Handles Button1.Click 19 Client New TcpClient (TextBox1.Text, 5825) Dim Writer As New Streamuriter(Client.GetstreamO) Nriter.Write(" RST") riter.Flush() End Sub 0 referenes Private Sub Timer1 Tick(ByWal sender As Systen.Object, ByVal e As System.EventAres) Handles Tiner1.Tick 25 If Listener.Pending True Then Message-* Client Listener.AcceptTcpClient) Din Reader As New StreanReader (C1ient.Getstream()) While Reader.Peek 1 End While MsgBox (Message, MsgBoxStyle.Okonly) 31 E 32 Message Message Convert . ToChar(Reader. Read().ToString 35 36 End If End Sub 0 refarences Private Sub TextBox1_TextChanged(sender As Object, e As EventAres) Handles Textbox1.TextChanged 38 39 End Sub o references Private Sub Label1 Click(sender As Object, e As EventArgs) Handles Label1.Click 43 End Sub 0 references 46? Private Sub Forml-FormClosing(ByVal sender As Object, Byval e As System.Windows.Forms , FormClosingEventArgs) Handles Me. FormClosing Listener.Stop() End Sub End Class 51 90% Ready Ln 46 Col 1 Ch 1 Add to Source Control File Edit View Project Build Debug Team Tools Test Analyze Window Help Full Screen Quick Launch (Ctrl+Q) Form1.vb" Form1.vb [Designl WindowsAppl f (Form1 Events) FormClosing EIrports System.Net.Sockets Irports System.Threading 3 Irports System.IO 1 reference Public Class Formi Dim Listener As New TcpListener (5825 Dim Client As New TcpClient Dim Message As String- Private Sub Forml Load (ByWal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ListThreed As New Thread(New ThreadStart(Addressof Listening)) ListThread.Start() 12 13 End Sub 1 reference Private Sub Listening) 16 Listener.Start) End Sub O references Private Sub Button1 Click(ByVal sender As System.Object, ByVal e As Systen.EventArgs) Handles Button1.Click 19 Client New TcpClient (TextBox1.Text, 5825) Dim Writer As New Streamuriter(Client.GetstreamO) Nriter.Write(" RST") riter.Flush() End Sub 0 referenes Private Sub Timer1 Tick(ByWal sender As Systen.Object, ByVal e As System.EventAres) Handles Tiner1.Tick 25 If Listener.Pending True Then Message-* Client Listener.AcceptTcpClient) Din Reader As New StreanReader (C1ient.Getstream()) While Reader.Peek 1 End While MsgBox (Message, MsgBoxStyle.Okonly) 31 E 32 Message Message Convert . ToChar(Reader. Read().ToString 35 36 End If End Sub 0 refarences Private Sub TextBox1_TextChanged(sender As Object, e As EventAres) Handles Textbox1.TextChanged 38 39 End Sub o references Private Sub Label1 Click(sender As Object, e As EventArgs) Handles Label1.Click 43 End Sub 0 references 46? Private Sub Forml-FormClosing(ByVal sender As Object, Byval e As System.Windows.Forms , FormClosingEventArgs) Handles Me. FormClosing Listener.Stop() End Sub End Class 51 90% Ready Ln 46 Col 1 Ch 1 Add to Source Control

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What are the challenges associated with tunneling in urban areas?

Answered: 1 week ago

Question

What are the main differences between rigid and flexible pavements?

Answered: 1 week ago

Question

What is the purpose of a retaining wall, and how is it designed?

Answered: 1 week ago

Question

How do you determine the load-bearing capacity of a soil?

Answered: 1 week ago

Question

what is Edward Lemieux effect / Anomeric effect ?

Answered: 1 week ago

Question

4. Choose appropriate and powerful language

Answered: 1 week ago

Question

2. Choose an appropriate organizational pattern for your speech

Answered: 1 week ago