为什么我无法从套接字缓冲区UWP中读取

时间:2017-01-23 22:48:20

标签: uwp stream-socket-client

我正在尝试编写一个DLL文件来处理我的UWP应用程序的套接字,就像我为winforms应用程序所做的那样。问题是我刚开始编写UWP应用程序。我的原始套接字控制DLL文件将启动一个接收线程,并且每100毫秒从套接字读取并引发一个事件。我尝试在我的UWP DLL中做同样的事情,但它似乎不适用于我从MSDN网站复制的代码。有人可以看一看,看看我做错了什么?我不会撒谎,我以前从未使用过任务(线程似乎更容易)。

PS,我已经确认连接和发送数据有效。

编辑:稍微更改了我的代码。但仍然行不通。有什么想法吗?

即使发送数据,似乎也不会越过下面的行。

Dim DataText As String = Reader.ReadToEnd

Imports Windows.Networking

Imports Windows.Networking.Sockets

Public Class ClientSocket

Private CSocket As New StreamSocket

Dim strHost As String = "192.168.0.11"

Dim RPort As String = "80"

Private killswitch As Boolean = False

Public Event SocketError(ByVal Operation As String, ByVal ErrorMessage As String)

Public Event DataReceived(ByVal DataString As String)

Public Async Sub Connect()

    Try

        Dim RHost As HostName = New HostName(strHost)

        Await CSocket.ConnectAsync(RHost, RPort)

        ReceiveTask.Start()

    Catch ex As Exception

        RaiseEvent SocketError("Connecting", ex.Message)

    End Try

End Sub

Public Sub Disconnect()

    killswitch = True

    CSocket.Dispose()

End Sub

Public Async Sub SendText(ByVal SendString As String)

    Try

        Dim OutStream As Stream = CSocket.OutputStream.AsStreamForWrite

        Dim Writer As StreamWriter = New StreamWriter(OutStream)

        Await Writer.WriteLineAsync(SendString)

        Await Writer.FlushAsync

    Catch ex As Exception

        RaiseEvent SocketError("Sending", ex.Message)

    End Try

End Sub

Private ReceiveTask As New Task(Sub()

                                    Dim InStream As Stream = CSocket.InputStream.AsStreamForRead

                                    Dim Reader As StreamReader = New StreamReader(InStream)

                                    While killswitch = False

                                        Try

                                            Dim DataText As String = Reader.ReadToEnd

                                            RaiseEvent DataReceived(DataText)

                                        Catch ex As Exception

                                            RaiseEvent SocketError("Receiving", ex.Message)

                                        End Try

                                        Task.Delay(TimeSpan.FromSeconds(1))

                                    End While

                                End Sub)

Public Property Host As String

    Get

        Host = strHost

    End Get

    Set(value As String)

        strHost = value

    End Set

End Property

Public Property Port As String

    Get

        Port = RPort

    End Get

    Set(value As String)

        RPort = value

    End Set

End Property

End Class

1 个答案:

答案 0 :(得分:0)

使用接收任务中的以下代码解决:

    Private ReceiveTask As New Task(Sub()

                                    Dim InStream As Stream = CSocket.InputStream.AsStreamForRead

                                    While killswitch = False

                                        Try

                                            Dim Reader As StreamReader = New StreamReader(InStream)

                                            Dim DataText As String = ""

                                            While Reader.Peek <> -1

                                                DataText &= Convert.ToChar(Reader.Read)

                                            End While



                                            RaiseEvent DataReceived(DataText)

                                        Catch ex As Exception

                                            RaiseEvent SocketError("Receiving", ex.Message)

                                        End Try

                                    End While

                                End Sub)