Solved

Visual Studio 2012 TCP Connections

Posted on 2014-11-05
3
416 Views
Last Modified: 2014-11-08
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.TcpClient()
     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("computer1")

         networkStream.Write(sendBytes, 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.ReceiveBufferSize) As Byte
         NetworkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
         Dim returndata As String = Encoding.ASCII.GetString(bytes)

         TextBox1.Text = returndata
     End Sub
 End Class
0
Comment
Question by:ADEZOYSA1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40425454
You are correct, in .NET there are, literally, many different solutions to this problem.  At it's most basic are the System.Net.Sockets.Tcp* classes.  Creating a project using this can be accomplished quite easily.  Here is a great example to use:

http://www.codeproject.com/Articles/38914/A-TCP-IP-Chat-Program

-saige-
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 40427461
0
 

Author Closing Comment

by:ADEZOYSA1
ID: 40430836
excellent article.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question