为什么我无法从我的套接字缓冲区 UWP 读取
How come I can't read from my socket buffer UWP
我正在尝试编写一个 DLL 文件来处理我的 UWP 应用程序的套接字,就像我为我的 winforms 应用程序所做的那样。问题是我刚刚开始编写 UWP 应用程序。我原来的套接字控制 DLL 文件将启动一个接收线程,每 100 毫秒从套接字中读取一次并引发一个事件。我尝试在我的 UWP DLL 中做同样的事情,但它似乎不适用于我从 MSDN 网站复制的代码。有人可以看看我做错了什么吗?我不会撒谎,我以前从未使用过任务(线程似乎更容易)。
PS,我已确认连接和发送数据有效。
编辑:稍微更改了我的代码。但还是不行。大家有什么想法吗?
似乎没有超过下一行,即使向其发送数据也是如此。
将 DataText 变暗为字符串 = 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
在接收任务中使用以下代码解决:
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)
我正在尝试编写一个 DLL 文件来处理我的 UWP 应用程序的套接字,就像我为我的 winforms 应用程序所做的那样。问题是我刚刚开始编写 UWP 应用程序。我原来的套接字控制 DLL 文件将启动一个接收线程,每 100 毫秒从套接字中读取一次并引发一个事件。我尝试在我的 UWP DLL 中做同样的事情,但它似乎不适用于我从 MSDN 网站复制的代码。有人可以看看我做错了什么吗?我不会撒谎,我以前从未使用过任务(线程似乎更容易)。
PS,我已确认连接和发送数据有效。
编辑:稍微更改了我的代码。但还是不行。大家有什么想法吗?
似乎没有超过下一行,即使向其发送数据也是如此。
将 DataText 变暗为字符串 = 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
在接收任务中使用以下代码解决:
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)