Results 1 to 6 of 6

Thread: TCP Listen to Stream and Print to textbox but nothing incomming

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    TCP Listen to Stream and Print to textbox but nothing incomming

    Hi All,

    i need to Listen on a TCP Port and the Result should be written to a textbox.

    The Client opens a Connection to my Port, sends his data (asci chars , max 1024 bytes) and afterwards drops connection. Ive been trying with the following code but that way it doesnt work. I never used tcp stuff before and absolutely have no idea whats going wrong :-)

    So generally i need to listen to a port (1581 in that case) and print it to a textbox or store it in a variable. I know that the data incimming starts with "ami" and ends with "eof"


    Anybody who might help me out please ?

    Code:
    Imports System.Net.Sockets
    Imports System.IO
    Imports System.Net
    Imports System.Text
    Public Class Form1
        Dim stream As NetworkStream
        Dim streamw As StreamWriter
        Dim server As TcpListener
        Dim client As New TcpClient
        Dim ipendpoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 1581)
        Dim t As New Threading.Thread(AddressOf Main)
        Dim datastring As String = Nothing
        Sub Main()
            Try
                server = New TcpListener(ipendpoint)
                server.Start()
                client = server.AcceptTcpClient
                client.ReceiveBufferSize = 1024
                Dim ns As NetworkStream = client.GetStream
                Dim data(client.ReceiveBufferSize) As Byte
                '---read incoming stream; Read() is a blocking call---
                Dim numBytesRead As Integer = ns.Read(data, 0, CInt(client.ReceiveBufferSize))
    
                While (numBytesRead <> 0)
                    datastring = Encoding.ASCII.GetString(data, 0, numBytesRead)
                    Console.Write(Encoding.ASCII.GetString(data, 0, numBytesRead))
                    REM MsgBox(Encoding.ASCII.GetString(data, 0, numBytesRead))
    
                End While
            Catch
                MsgBox("error")
            End Try
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Button1.Text = "Start" Then
                t.Start()
                Button1.Text = "Stop"
            Else
                client.Close()
                streamw.Close()
                stream.Close()
    
                Button1.Text = "Start"
            End If
        End Sub
    End Class
    Last edited by lastyle; Oct 14th, 2021 at 04:19 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: TCP Listen to Stream and Print to textbox but nothing incomming

    Quote Originally Posted by lastyle View Post
    it doesnt work
    This is never an acceptable description of a problem. You need to have debugged your code before posting here and you need to explain to us exactly what does happen and where and how that differs from your expectations. We may be able to find the issue by trawling through the code but we shouldn't have to work something out that you should already know.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    Re: TCP Listen to Stream and Print to textbox but nothing incomming

    I am sorry for that.

    the correct explantation is

    The Textbox text doesnt change at all, it stays empty and
    the messagebox only shows 16 chars, but there should be atleast 5xx up to 1000 , depending on amount of chars have been sent from the client.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    Re: TCP Listen to Stream and Print to textbox but nothing incomming

    That's still not a proper explanation. That's looking at it from a user's perspective. You're not a user. You're a developer, so use your development tools. If you don't know how to debug code then stop what you're doing and learn now, because it's an essential skill. Set a breakpoint at the top of your code and step through line by line, examining the course of execution and the state at each step. When either or both do not match your expectation, you have found an issue. That is what I mean by I say "where and how". You need to be able to tell us what the specific line of code was, what your expectation was and what the actual state/behaviour was.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    Re: TCP Listen to Stream and Print to textbox but nothing incomming

    Quote Originally Posted by jmcilhinney View Post
    That's still not a proper explanation. That's looking at it from a user's perspective. You're not a user. You're a developer, so use your development tools. If you don't know how to debug code then stop what you're doing and learn now, because it's an essential skill. Set a breakpoint at the top of your code and step through line by line, examining the course of execution and the state at each step. When either or both do not match your expectation, you have found an issue. That is what I mean by I say "where and how". You need to be able to tell us what the specific line of code was, what your expectation was and what the actual state/behaviour was.
    I know what you mean and already did so.

    I know that the incomming data is bigger than 16 chars,but only the first 16 chars (bytes) have been read.

    Why isnt this a proper description whan i say i am reading in a loop but still dont recieve more data ?

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    Re: TCP Listen to Stream and Print to textbox but nothing incomming

    Quote Originally Posted by lastyle View Post
    I know what you mean and already did so.

    I know that the incomming data is bigger than 16 chars,but only the first 16 chars (bytes) have been read.

    Why isnt this a proper description whan i say i am reading in a loop but still dont recieve more data ?
    Meanwhile i noticed that the incomming Stream isnt updated.

    The client connects sends Data and then drops connection.The tcplistener doesnt accept the new connection and read new data from it. What is the best way to close and restart the TCPlistener ?

    monitoring in a second thread which will abort thread one and restart it ?

    my current code looks like this.

    Code:
    Imports System.Net.Sockets
    Imports System.IO
    Imports System.Net
    Imports System.Text
    Public Class Form1
        Dim stream As NetworkStream
        Dim streamw As StreamWriter
        Dim server As TcpListener
        Dim client As New TcpClient
        Dim ipendpoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 1581)
        Dim t As New Threading.Thread(AddressOf Main)
        Dim datastring As String = Nothing
        Public bbsdata As String = Nothing
        Public bbsdataprevious As String = Nothing
    
        Sub Main()
            Try
                server = New TcpListener(ipendpoint)
                server.Start()
                client = server.AcceptTcpClient
                client.ReceiveBufferSize = 1024
                Dim ns As NetworkStream = client.GetStream
                Dim data(client.ReceiveBufferSize) As Byte
                datastring = ""
                '---read incoming stream; Read() is a blocking call---
                Dim numBytesRead As Integer = ns.Read(data, 0, CInt(client.ReceiveBufferSize))
                Dim z As Integer = 0
                While (numBytesRead <> 0)
                    datastring = Encoding.ASCII.GetString(data, 0, numBytesRead)
                    System.Diagnostics.Debug.WriteLine(Encoding.ASCII.GetString(data, 0, numBytesRead))
                    SetTextBoxInfo(datastring)
                End While
            Catch
            End Try
        End Sub
        Public Sub SetTextBoxInfo(stringValue As String)
            If stringValue <> bbsdataprevious Then
                bbsdataprevious = stringValue
                Me.BeginInvoke(Sub() Me.TextBox2.Text = stringValue)
                datastring = ""
                client.Close()
                stream.Close()
            Else
            End If
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Button1.Text = "Start" Then
                t.Start()
                Button1.Text = "Stop"
            Else
                client.Close()
                stream.Close()
    
                Button1.Text = "Start"
            End If
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width