I am trying to get some XML data from a third party app. To acquire this data, I can Putty, using Telnet, into the IP Address with a specific port. Once the connection is open, all I need to do is type in
<StageDisplayLogin>password</StageDisplayLogin>
Once that is submitted. All of my return XML data pops up in my Putty Window which I will end up parsing into variables very often. I tried replicating this with many different solutions in VB.net and I couldn't get anything to work. Here it what I was currently working on. I already tried a few TCP Client projects like the one listed here
Using streams through TcpClient and TcpServer
I would like to do this with my own app not using putty. This is my latest try using a POST method instead to see if that might work. I can enter everything but the program hangs from what I am guessing is a timeout. You wouldn't be able to simulate this since you don't have my software (ProPresenter) but is this the correct way to simulate a Putty Telnet session? The user will input their IP Address, their port and their password. This program will stay open as the third party program updates the session at any data changes.
<Window x:Name="vWindow" x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:vPresenter"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="#FF1E2328" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="137*"/>
<RowDefinition Height="20*"/>
</Grid.RowDefinitions>
<TextBox x:Name="vIPAddress" Margin="165,43,173,211.4" TextWrapping="Wrap" Text="IP Address" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<TextBox x:Name="vPort" Margin="165,91,173,163.4" TextWrapping="Wrap" Text="Port" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<TextBox x:Name="vPassword" Margin="165,137,173,117.4" TextWrapping="Wrap" Text="Password" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<TextBox x:Name="PPData" Margin="27,43,312,146.4" TextWrapping="Wrap" Text="Data" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
<TextBlock Margin="151,-6,152,251.4" TextWrapping="Wrap" Text="vMixPresenter" Foreground="White" FontSize="22" TextAlignment="Center"/>
<Button x:Name="ConntectButton" Content="Connect!" HorizontalAlignment="Left" Margin="165,179,0,0" VerticalAlignment="Top" Width="102" Background="DarkGreen" Foreground="White" BorderBrush="DarkGreen" Click="Button_Click"/>
<Button x:Name="SendButton" Content="Send!" HorizontalAlignment="Left" Margin="165,221,0,0" VerticalAlignment="Top" Width="102" Background="DarkGreen" Foreground="White" BorderBrush="DarkGreen" Click="Button2_Click"/>
<TextBlock x:Name="ServerText" HorizontalAlignment="Left" Margin="329,249,0,-16.6" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Foreground="White" Height="42" Grid.RowSpan="2" Width="100"/>
</Grid>
</Window>
And the VB File
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Class MainWindow
Private Server As TcpListener = Nothing
Private ServerThread As Thread = Nothing
Dim tcpclnt As TcpClient
Dim stream As NetworkStream
Dim clientstream As NetworkStream
Public Shared bytes(1024) As Byte
Public Shared data As String = Nothing
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Server = New TcpListener(IPAddress.Any, 45678)
ServerThread = New Thread(AddressOf ConnectionListener)
ServerThread.IsBackground = True
ServerThread.Start()
ServerText.Text = "Server started!"
End Sub
Public Sub ConnectionListener()
Try
Server.Start()
While True
Dim myClient As TcpClient = Server.AcceptTcpClient()
Dim T As New Thread(AddressOf SomeClientActions)
T.Start(myClient)
'Invoke(Sub() ServerText.Text = ServerText.Text + Environment.NewLine + "Client connected")
Console.WriteLine("1")
' HERE IM TRYING TO READ DATA FROM STREAM
stream = myClient.GetStream()
Dim i As Int32
i = stream.Read(bytes, 0, bytes.Length)
Console.WriteLine("1.5")
While True
Console.WriteLine("2")
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
'Invoke(Sub() TextBox3.Text = "Received: {0}" + data)
Console.WriteLine("received" & data)
' Process the data sent by the client.
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
'Invoke(Sub() TextBox3.Text = "Sent: {0}" + data)
Console.WriteLine("sent" & data)
i = stream.Read(bytes, 0, bytes.Length)
End While
myClient.Close()
End While
Catch ex As Exception
Console.WriteLine("Server Error")
End Try
Application.ExitThread()
End Sub
Private Sub SomeClientActions(ByVal client As Object)
' ... do something with "client" in here ...
' MAYBE SOME CODE HERE?..
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
tcpclnt = New TcpClient()
tcpclnt.Connect("192.168.1.101", 45678)
clientstream = tcpclnt.GetStream()
End Sub
Private Sub Button2_Click(sender As Object, e As RoutedEventArgs)
Dim msgbyte() As Byte = Encoding.UTF8.GetBytes(PPData.Text)
clientstream.Write(msgbyte, 0, msgbyte.Length)
End Sub
End Class