Click here to Skip to main content
13,050,896 members (61,910 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I created a code that should scroll through an array of data streams and output the stream as soon as one of them gets input, but everytime it gets input it outputs like it should but with a following 20 blank lines, please help,


Contents of module1.vb:
Module Module1
    Dim Socket As New Socket_Control.Server_Socket
    Dim inStream As New Threading.Thread(AddressOf Streaming)
    Dim Connected As Boolean = False
    Sub Main()
        msg("Max Client Slots:")
        Console.Write(" >> ")
        Dim max As String = Console.ReadLine()
        msg("Port: ")
        Console.Write(" >> ")
        Dim port As String = Console.ReadLine()
        Socket.MaxSlots = max
        Socket.Port = port
        Connected = True
        msg("Now Monitoring Port: " & Socket.Port)
        While True
        End While
    End Sub
    Private Sub Streaming()
        While True
            Dim Count As Integer = 0
            While Not Count = Socket.MaxSlots
                Dim Stream As String = Socket.Stream(Count)
                If Not Stream = "%False%" Then
                    msg("[CLIENT " & Count + 1 & "] " & Stream)
                End If
                Count += 1
            End While
        End While
    End Sub
    Private Sub msg(ByVal Message As String)
        Console.WriteLine(" >> " & Message)
    End Sub
End Module

Socket_Control.dll Server_Socket Class:
Imports System.Windows.Forms
Imports System.Net.Sockets
Imports System.Text
Public Class Server_Socket
    Private boolConnected As Boolean = False
    Public Event ClientDisconnect()
    Public Property Port As Integer = 55344
    Public Property MaxSlots
    Public Event ConnectionEstablished()
    Public ReadOnly Property Connected
            Return boolConnected
        End Get
    End Property
    Private Socket As TcpListener
    Private Client(1) As TcpClient
    Private serverStream As NetworkStream
    Private Connection As New Threading.Thread(AddressOf AwaitConnection)
    Private sendData(10000) As Byte
    Private recieveData(10000) As Byte
    Private WithEvents Timer As New Timer
    Public Sub Start()
        Socket = New TcpListener(Port)
        ReDim Client(MaxSlots - 1)
        'Timer.Interval = 100
        'Timer.Enabled = True
    End Sub
    Private Sub AwaitConnection()
        Dim Count As Integer = 0
        While True
            If Socket.Pending() AndAlso Not Count = MaxSlots Then
                Client(Count) = Socket.AcceptTcpClient
                Count += 1
            End If
        End While
    End Sub
    Public ReadOnly Property Stream(ByVal ID As String)
            If Not Client(ID) Is Nothing Then
                serverStream = Client(ID).GetStream()
                If Client(ID).GetStream.DataAvailable Then
                    serverStream.Read(recieveData, 0, 10000)
                    Return Encoding.ASCII.GetString(recieveData)
                    Array.Clear(recieveData, 0, 10000)
                    Return "%False%"
                End If
                Return "%False%"
            End If
        End Get
    End Property
    Public Sub DataSend(ByVal data)
            sendData = Encoding.ASCII.GetBytes(data)
            serverStream.Write(sendData, 0, sendData.Length)
        Catch ex As Exception
            boolConnected = False
            RaiseEvent ClientDisconnect()
        End Try
    End Sub
End Class
Posted 29-Mar-13 23:53pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are not capturing the result of the NetworkStread Read method. The result tells you how many bytes were read.

In Socket_Control dll:

   Dim intLengthRead as Integer = serverStream.Read(receiveData, 0, 10000)
   Return Encoding.ASCII.GetString(receiveData).Substring(0,intLengthRead)
Sicppy 30-Mar-13 6:46am
Nice try but no, that line of code worked in the previous version where it wasnt an array of clients, I'm am fairly certain it has to do with the while loop
Sicppy 30-Mar-13 6:51am
Actually that worked, sorry to jump to conclusions, thanks mate!
Sicppy 30-Mar-13 6:52am
oh, and the - 1 was unnecessary

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100