Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Sockets
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,
 
-Jordan
 
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
        Socket.Start()
        Connected = True
        msg("Now Monitoring Port: " & Socket.Port)
        inStream.Start()
        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
        Get
            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)
        Socket.Start()
        Connection.Start()
        '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)
        Get
            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)
                Else
                    Array.Clear(recieveData, 0, 10000)
                    Return "%False%"
                End If
            Else
                Return "%False%"
            End If
        End Get
    End Property
 
    Public Sub DataSend(ByVal data)
        Try
            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 30-Mar-13 0:53am
Sicppy1.5K

1 solution

Rate this: bad
good
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)
...
  Permalink  
v2
Comments
Sicppy at 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 at 30-Mar-13 6:51am
   
Actually that worked, sorry to jump to conclusions, thanks mate!
Sicppy at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2014
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