询问-TcpListener(AsyncCallBack)问题 [英] Ask - TcpListener (AsyncCallBack) Problem

查看:89
本文介绍了询问-TcpListener(AsyncCallBack)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试创建一个简单的客户端服务器应用程序.我有AsyncCallBack的问题.这段代码不会产生错误,但是在发送消息时,它不会在txtMessage.Text(TextBox)中显示该消息.请帮助我如何以正确的方式编写AsyncCallBack.这是我的代码.

Hi, I am trying to create a simple Client Server application. I have problem with AsyncCallBack. This code generates no errors but when a message sent, it doesn''t show the message in txtMessage.Text (TextBox). Please help me how to write AsyncCallBack in the right way. Here is my code.

Dim NetStream As NetworkStream ''NetworkStream to send text in stream
Dim AsynCallback As AsyncCallback
Dim Client As TcpClient = New TcpClient() ''TcpClient to connect to the server
Dim ByteStream() As Byte ''Data in bytes

Private Sub Login()
    Try
        If IsNothing(Client) Then
            Client = New TcpClient()
        End If

        With Client
            ''.NoDelay = True
            .Connect("server", 5482)

        End With

        If Client.Connected = True Then
            Dim mData As String = "This is Client"

            Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(mData)
            NetStream = Client.GetStream()
            NetStream.Write(data, 0, data.Length)

            If Client.Connected = True Then
                Dim nAsynCallback As AsyncCallback = New AsyncCallback(AddressOf DataReceived)
            End If

        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Private Sub DataReceived(ByVal AsyncResults As IAsyncResult)
    Try
        ReDim ByteStream(Client.ReceiveBufferSize) ''Data in bytes
        If NetStream.CanRead Then
            NetStream.BeginRead(ByteStream, 0, Convert.ToInt16(NetStream.Length), New AsyncCallback(AddressOf CallBack), NetStream)
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub

Private Sub CallBack(ByVal AsyncResults As IAsyncResult)
    ''NetStream = CType(AsyncResults.AsyncState, NetworkStream)
    ''Dim i As Integer = NetStream.EndRead(AsyncResults)
    NetStream.EndRead(AsyncResults)
    Me.txtMessages.Text = System.Text.Encoding.ASCII.GetString(ByteStream)
    AsynCallback = New AsyncCallback(AddressOf DataReceived)
End Sub

推荐答案

您需要处理Callback中的读取结果并将其从那里发送到UI.
当前设置的方式看起来像是同步读取,只是不能异步工作.

还有更多说明:

You need to handle the result of the read in the Callback and send it to the UI from there.
The way it is currently setup looks like a synchronous read which just doest''t work async.

And with a bit more explanation:

Dim NetStream As NetworkStream 'NetworkStream to send text in stream
Dim AsynCallback As AsyncCallback
Dim Client As TcpClient = New TcpClient() 'TcpClient to connect to the server
Dim ByteStream(1024) As Byte
Private Sub Login()
	Try
		If IsNothing(Client) Then
			Client = New TcpClient()
		End If
		With Client
			'.NoDelay = True
			.Connect("127.0.0.1", 5482)
		End With
		If Client.Connected = True Then
			Dim mData As String = "This is Client"
			Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(mData)
			NetStream = Client.GetStream()
			NetStream.Write(data, 0, data.Length)
			If Client.Connected = True Then
		NetStream.BeginRead(ByteStream, 0, ByteStream.Length, New AsyncCallback(AddressOf CallBack), NetStream)
			End If
		End If
	Catch ex As Exception
		MessageBox.Show(ex.Message)
	End Try
End Sub
Private Sub CallBack(ByVal AsyncResults As IAsyncResult)
	NetStream.EndRead(AsyncResults)
	Console.WriteLine(System.Text.Encoding.ASCII.GetString(ByteStream))
	'Me.txtMessages.Text = System.Text.Encoding.ASCII.GetString(ByteStream) 'this needs to be invoked :) 
	NetStream.BeginRead(ByteStream, 0, ByteStream.Length, New AsyncCallback(AddressOf CallBack), NetStream)
End Sub



现在,我知道我会做些小事情,以确保该缓冲区不会被另一个线程等在外部访问.您还可以仔细看看调用Me.txtMessages.Text使其在UI线程上.并不是说这个agian取决于您如何启动异步调用.

无论如何,最好还是



Now I know that I kindly step over little things as makeing sure that the buffer is not accessed outside by another thread etc. You might also take a good look at Invoking the Me.txtMessages.Text to get it on the UI thread. not that this agian depends on how you start the async call.

in any case it''s always better to do the

if(InvokeRequired)
  Invoke(...



我很确定这就是您要重新开始的全部.

干杯,AT



I''m pretty sure this is all you need to get going again.

Cheers, AT


这篇关于询问-TcpListener(AsyncCallBack)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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