VB.NET控制台输出问题 [英] VB.NET console output issue

查看:105
本文介绍了VB.NET控制台输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个代码,它应滚动数据流数组,并在其中一个输入后立即输出流,但每次输入时它都会输出它应该输出但是后面有20个空行,请帮助,



-Jordan



module1.vb的内容:

 模块 Module1 
Dim 套接字作为 Socket_Control.Server_Socket
Dim inStream As Threading.Thread( AddressOf Streaming)
Dim 已连接正如 布尔 = False

Sub Main()
ms g( Max Client Slots:
Console.Write( >>
Dim max As String = Console.ReadLine()
msg( 端口:
Console.Write( >>
Dim port As String = Console.ReadLine()
Socket.MaxSlots = max
Socket.Port = port
Socket.Start()
Connected = True
msg( 现在监控端口:& Socket.Port)
inStream。开始()
True

结束 while
结束 Sub

私有 Sub Streaming()
True
Dim 计数 As 整数 = 0
Count = Socket.MaxSlots
Dim Stream 作为 字符串 = Socket.Stream(计数)
如果 < span class =code-keyword>不 Stream = %False% < span class =code-keyword>然后
msg( [客户&计数+ 1 & ]&流)
结束 如果
计数+ = 1
结束
结束
结束 Sub

私人 Sub msg( ByVal 消息作为 字符串
Console.WriteLine( >>& Message)
结束 Sub
结束 模块





Socket_Cont rol.dll Server_Socket类:

  Imports  System.Windows.Forms 
Imports System.Net.Sockets
Imports System.Text

公共 Server_Socket
私有 boolConnected 作为 布尔 = 错误
公共 事件 ClientDisconnect()

公共 属性端口作为 整数 = 55344
公共 属性 MaxSlots
公开 事件 ConnectionEstablished()

公共 < span class =code-keyword> ReadOnly Property 已连接
获取
返回 boolConnected
结束 获取
结束 财产

私有套接字作为 TcpListener
私有客户端( 1 作为 TcpClient
私有 serverStream 作为 NetworkStream
私有连接 As Threading.Thread( AddressOf AwaitConnection)

私有 sendData( 10000 作为 字节
私有 recieveData( 10000 作为 字节

私有 < span class =code-keyword> WithEvents
计时器作为 计时器

公共 Sub Start()
Socket = TcpListener(端口)
ReDim 客户端(MaxSlots - 1
Socket.Start()
Conne ction.Start()
' Timer.Interval = 100
Timer.Enabled = True
结束 Sub

私有 Sub AwaitConnection()
Dim 计数作为 整数 = 0
True
如果 Socket.Pending() AndAlso Count = MaxSlots 然后
客户端(Count)= Socket.AcceptTcpClient
计数+ = 1
结束 如果
结束
结束 Sub

公共 ReadOnly 属性流( ByVal ID As 字符串
获取
如果 客户端(ID) 没什么 < span class =code-keyword>然后
serverStream =客户端(ID).GetStream()
如果客户端(ID ).GetStream.DataAvailable 然后
serverStream.Read(recieveData, 0 10000
返回 Encoding.ASCII.GetString(recieveData)
否则
Array.Clear(recieveData, 0 10000
返回 %False%
结束 如果
其他
返回 %False%
< span class =code-keyword>结束 如果
结束 获取
结束 属性

公共 Sub DataSend( ByVal 数据)
尝试
sendData = Encoding.ASCII.GetBytes(data)
serverStream.Write (sendData, 0 ,sendData.Length)
Catch ex 作为异常
boolConnected = 错误
RaiseEvent ClientDisconnect ()
结束 尝试
结束 Sub
结束

解决方案

您没有捕获NetworkStread Read方法的结果。结果告诉您读取了多少字节。



在Socket_Control中dll:



 ... 
昏暗intLengthRead as Integer = serverStream.Read(receiveData,0,10000)
返回Encoding.ASCII.GetString(receiveData).Substring(0,intLengthRead)
...


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

解决方案

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)
...


这篇关于VB.NET控制台输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆