Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP
Polling a server for a value using TCP Polling a server for a value using TCP
Polling a server for a value using TCP
Go Back  Xtreme Visual Basic Talk > > > Polling a server for a value using TCP


Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2008, 04:41 PM
TexasAggie TexasAggie is offline
Junior Contributor
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 286
Default Polling a server for a value using TCP


We have a server in the building that we can poll for barometric pressure using TCP. Another developer uses LabVIEW to do this, so it may be a little more straightforward for him. He can't help me in VB.

Here are the instructions:

*******************************
TCP Request to IP xxx.xxx.xxx.xxx
Port xxxx
4Bytes
*******************************

How can I set up a "TCP Request?" Is this all of the information that I need?
Reply With Quote
  #2  
Old 06-03-2008, 05:02 PM
TexasAggie TexasAggie is offline
Junior Contributor
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 286
Default

I tried this:

Code:
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Const portNum As Integer = xxxx Const hostName As String = "xxx.xxx.xxx.xxx" ' Entry point that delegates to C-style main Private Function. Try Dim client As New TcpClient(hostName, portNum) Dim ns As NetworkStream = client.GetStream() Dim bytes(3) As Byte Dim bytesRead As Integer = ns.Read(bytes, 0, bytes.Length) txtOutput.AppendText(Encoding.ASCII.GetString(bytes, 0, bytesRead)) client.Close() Catch ex As Exception Console.WriteLine(e.ToString()) End Try End Sub

and it returned 4 bytes as expected, but the first three are always zero and the last is always 6.

Last edited by TexasAggie; 06-03-2008 at 05:03 PM. Reason: oops
Reply With Quote
  #3  
Old 06-04-2008, 09:33 AM
PrOpHeT's Avatar
PrOpHeTPolling a server for a value using TCP PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

Just out of curiosity, have you tried connecting to the server/port with something else like netcat. telnet, or rawporttcp?

I am just curious what the actual return is through one of them, perhaps there is some formatting involved and the first 4 bytes are not the four you need?
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #4  
Old 06-04-2008, 10:24 AM
TexasAggie TexasAggie is offline
Junior Contributor
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 286
Default

The other developer said it was a "human readable string," so I guess it would come back as "29.2" or something. I can see the data while tracing through the program, but it appears to be zeros to me.
Reply With Quote
  #5  
Old 06-04-2008, 11:21 AM
PrOpHeT's Avatar
PrOpHeTPolling a server for a value using TCP PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

well, some things are more human readable than others depending on the format. for instance try talking to a telnet server in raw packets, you get a lot of gibberish because of the terminal formatting.

Maybe try something like this, which should show what it returns (first line at least)

Code:
 Private Sub GetData()
        Dim Connection As TcpClient
        Dim NWStream As NetworkStream
        Dim Reader As StreamReader
        Dim Response As String

        Connection = New TcpClient("192.168.1.X", 9999)
        NWStream = Connection.GetStream()
        Reader = New StreamReader(NWStream)
        Response = Reader.ReadLine

        Debug.WriteLine(Response)
    End Sub
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #6  
Old 06-04-2008, 11:49 AM
AtmaWeapon's Avatar
AtmaWeaponPolling a server for a value using TCP AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I agree with PrOpHeT; "human readable string" doesn't give you much information. LabVIEW probably expects the data in some particular format and handles decoding behind-the-scenes. .NET needs specifics. You need to know endian-ness and encoding at the minimum; it could be UTF-8, ASCII, etc.

PrOpHet's code is good, but if you don't get a usable string out of it, give this a whirl:
Code:
Imports System.Net
Imports System.Net.Sockets

Module Module1

    Sub Main()
        Dim ip As String = "127.0.0.1"
        Dim portNumber As Integer = 4444
        Dim client As New TcpClient()

        client.Connect(ip, portNumber)

        Dim buffer(3) As Byte
        Dim bytesRead As Integer = 0

        Using stream As NetworkStream = client.GetStream()
            While bytesRead <= 3
                bytesRead += stream.Read(buffer, 0, 4)
            End While
        End Using
        client.Close()

        Console.WriteLine("Here's what I got:")
        For i As Integer = 0 To buffer.Length - 1
            Console.WriteLine("{0}: {1:X2}", i, buffer(i))
        Next
    End Sub

End Module
That will show you the bytes that are sent to you; from there you can try to figure out how those bytes are encoded to a string.

Since you mentioned LabVIEW, I have to see if there's a chance for a shameless plug: How's the data output from the server? If it's a LabVIEW VI, there's easier ways to perform LabVIEW/.NET data exchange that, coincidentally, use a feature of the product I work on and I'm going to pitch using Shared Variables to you If it's just some random other app, my stuff won't be helpful to you and you'll be spared the sales pitch.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 06-04-2008, 11:52 AM
PrOpHeT's Avatar
PrOpHeTPolling a server for a value using TCP PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

Yeah, I was shooting for raw data just to see what it needed to be formated as perhaps.

But thanks for going that extra mile (sometimes I think you just like to type )
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #8  
Old 06-04-2008, 12:42 PM
AtmaWeapon's Avatar
AtmaWeaponPolling a server for a value using TCP AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by PrOpHeT View Post
Yeah, I was shooting for raw data just to see what it needed to be formated as perhaps.

But thanks for going that extra mile (sometimes I think you just like to type )
It's more like there's a few topics, such as asynchronous programming or text encodings, that fascinate me so much I cannot resist responding to threads about them. I think it's because I found them difficult to start with, and I feel I should help others understand them, but in the end this is why I tend to jump in to some threads with huge explanations -- they tend to be things I love to explain.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #9  
Old 06-04-2008, 12:55 PM
PrOpHeT's Avatar
PrOpHeTPolling a server for a value using TCP PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

We all have our things (mine are generally more focused on making windows do what it so earnestly does not want to), and personally my keister has been saved more than once by one of those long explanations so I am certainly not complaining.

Keep up the good work soldier *salute*
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #10  
Old 06-04-2008, 03:26 PM
TexasAggie TexasAggie is offline
Junior Contributor
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 286
Default

All I get is 0 0 0 6, or 00 00 00 06 which is the same thing I noticed in my original code. I will have to get with the other guy. I'll let you guys know what I find.
Reply With Quote
  #11  
Old 06-04-2008, 07:31 PM
PrOpHeT's Avatar
PrOpHeTPolling a server for a value using TCP PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

That basically translates to "NULL NULL NULL ACK"

and to me looks like perhaps a request for some sort of control conversation in order to get at your data.
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #12  
Old 06-05-2008, 08:43 AM
TexasAggie TexasAggie is offline
Junior Contributor
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 286
Default

He was wrong on the size. I was very confused as to how it could return a number like 28.323 using four bytes. It is really 6 bytes and it returns ascii values.

Now the question is... If I have an array of ascii values, what is the best way to get the value as a single?

I could use a loop to append the values into a string and then convert the string to a single, but I'm sure there is a better way.

here is my ghetto method:

Code:
Dim value As Single Dim valString As String = "" For i As Integer = 0 To buffer.Length - 1 valString &= Chr(buffer(i)) Next MsgBox(valString) MsgBox(Val(valString))
Reply With Quote
  #13  
Old 06-05-2008, 08:59 AM
PrOpHeT's Avatar
PrOpHeTPolling a server for a value using TCP PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

CSng(Text.Encoding.ASCII.GetString(YourArrayOfBytes))

P.S. where the ASCII is you have other encodings if you find out there are issues with just ascii
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #14  
Old 06-05-2008, 09:50 AM
TexasAggie TexasAggie is offline
Junior Contributor
 
Join Date: Feb 2004
Location: San Antonio, TX
Posts: 286
Default

Thanks guys!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP Polling a server for a value using TCP
Polling a server for a value using TCP
Polling a server for a value using TCP
 
Polling a server for a value using TCP
Polling a server for a value using TCP
 
-->