我的文字未显示 [英] My Text not shown

查看:69
本文介绍了我的文字未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有用于从端口发送和接收消息的代码,
它可以正确发送和接收消息,但不会在网页中显示消息.
请更正我的代码
thx

Hi,
I have code for sending and receiving message from port,
It send and receive message correctly but it not show message in web page.
please correct my code
thx

Imports System
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Partial Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim Client As New TcpClient
    Dim BufferSize(Client.ReceiveBufferSize - 1) As Byte
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Client.Connect("127.0.0.1", 80)
            SendMessage("aaaaa")
            Client.GetStream().BeginRead(BufferSize, 0, BufferSize.Length, AddressOf ReceiveMessage, Nothing)
        Catch ex As Exception
            Console.WriteLine("error")
        End Try
    End Sub
    Public Sub SendMessage(ByVal message As String)
        Try
            Dim netStr As NetworkStream = Client.GetStream()
            Dim Data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
            netStr.Write(Data, 0, Data.Length)
            netStr.Flush()
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try
    End Sub
    Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
        Dim bufferLength As Integer
        Dim message As String
        Try
            SyncLock Client.GetStream
                bufferLength = Client.GetStream().EndRead(ar)
            End SyncLock
            If bufferLength < 1 Then
                Client.Close()
            End If
            message = System.Text.Encoding.ASCII.GetString(BufferSize, 0, bufferLength).ToString
            Label1.Text = message
            Console.WriteLine(Client.Client.RemoteEndPoint.ToString() + "-> " + message)
            Client.GetStream().BeginRead(BufferSize, 0, BufferSize.Length, AddressOf ReceiveMessage, Nothing)
        Catch ex As Exception
            Console.WriteLine(ex)
            Client.Close()
        End Try
    End Sub
End Class



[edit]整理显示,更改标记以包括VB [/edit]



[edit]Tidied up display, changed tags to include VB[/edit]

推荐答案

首先,我要确保下面的行实际上是在将信息放入消息变量

First thing i would is make sure that the line below is actually putting information into the message variable

message = system.Text.Encoding.ASCII.GetString(BufferSize, 0, bufferLength).ToString
Label1.Text = message



在该行之前放置一个断点并逐步执行代码,看看会发生什么情况



put a break point before that line and step through the code and see what happens


这篇关于我的文字未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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