ADEZOYSA1
asked on
Visual Studio 2012 TCP Connections
Hello,
I was a traditional VB6 code writer and have been transitioning to Visual Studio 2012 Visual Basic. For the most part I have gotten myself “up to speed” on most things. But there are still some differences between the two versions that I need assistance with.
In my current issue, I am trying to establish a TCP connection to another host computer. In VB6 I would use a .connect to attach to the host server and then use the DataArrival method to capture the incoming packets as they happened. I am unsure how to continuously “read” data using Visual Studio 2012 Visual Basic.
I have used the below code as my example. Where there are two buttons labeled BUTTON1 and BUTTON2. I use BUTTON1 to connect to the server and then BUTTON2 would retrieve the data that is streaming from the host server and display it in the textbox labeled TEXTBOX1.
My question is, how do I capture the data continuously without having to initiating BUTTON2? Do I have to setup a separate thread to do this process or is there a VB6 similar DataArrival function?
Thanks
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class Form1
Dim tcpClient As New System.Net.Sockets.TcpClie nt()
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim networkStream As NetworkStream = TcpClient.GetStream()
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("c omputer1")
networkStream.Write(sendBy tes, 0, sendBytes.Length)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tcpClient.Connect("xxx.xxx .xxx.xxx", 39500)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBuf ferSize) As Byte
NetworkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBuff erSize))
Dim returndata As String = Encoding.ASCII.GetString(b ytes)
TextBox1.Text = returndata
End Sub
End Class
I was a traditional VB6 code writer and have been transitioning to Visual Studio 2012 Visual Basic. For the most part I have gotten myself “up to speed” on most things. But there are still some differences between the two versions that I need assistance with.
In my current issue, I am trying to establish a TCP connection to another host computer. In VB6 I would use a .connect to attach to the host server and then use the DataArrival method to capture the incoming packets as they happened. I am unsure how to continuously “read” data using Visual Studio 2012 Visual Basic.
I have used the below code as my example. Where there are two buttons labeled BUTTON1 and BUTTON2. I use BUTTON1 to connect to the server and then BUTTON2 would retrieve the data that is streaming from the host server and display it in the textbox labeled TEXTBOX1.
My question is, how do I capture the data continuously without having to initiating BUTTON2? Do I have to setup a separate thread to do this process or is there a VB6 similar DataArrival function?
Thanks
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class Form1
Dim tcpClient As New System.Net.Sockets.TcpClie
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim networkStream As NetworkStream = TcpClient.GetStream()
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("c
networkStream.Write(sendBy
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tcpClient.Connect("xxx.xxx
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBuf
NetworkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBuff
Dim returndata As String = Encoding.ASCII.GetString(b
TextBox1.Text = returndata
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
excellent article.
http://www.codeproject.com/Articles/38914/A-TCP-IP-Chat-Program
-saige-