Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Net套接字调用_Vb.net_Sockets_Invoke - Fatal编程技术网

Vb.net Net套接字调用

Vb.net Net套接字调用,vb.net,sockets,invoke,Vb.net,Sockets,Invoke,我正在使用vb.net 2010,我创建了一个程序,该程序使用套接字在windows服务器和unix服务器之间传输数据。这段代码最初来自微软的一个示例项目,因此我对它的理解很少 一切都很好,直到我有了把程序变成服务的想法。调用命令不能从服务访问。我想我明白为什么,但更重要的是,我如何绕过它或修复它 ' need to call Invoke before can update UI elements Dim args As Object() = {command, data} I

我正在使用vb.net 2010,我创建了一个程序,该程序使用套接字在windows服务器和unix服务器之间传输数据。这段代码最初来自微软的一个示例项目,因此我对它的理解很少

一切都很好,直到我有了把程序变成服务的想法。调用命令不能从服务访问。我想我明白为什么,但更重要的是,我如何绕过它或修复它

' need to call Invoke before can update UI elements
    Dim args As Object() = {command, data}
    Invoke(_processInStream, args)
有人请帮助我,我非常想完成这个项目,这样我可以继续:)

下面是这个类的其余部分,也有一个服务器套接字类,但我不想让事情复杂化

Public Class srvMain

' start the InStream code to receive data control.Invoke callback, used to process the socket notification event on the GUI's thread
Delegate Sub ProcessSocketCommandHandler(ByVal command As NotifyCommandIn, ByVal data As Object)
Dim _processInStream As ProcessSocketCommandHandler

' network communication
Dim WithEvents _serverPRC As New ServerSocket
Dim _encryptDataIn() As Byte
Dim myConn As SqlConnection
Dim _strsql As String = String.Empty

Protected Overrides Sub OnStart(ByVal args() As String)
    ' watch for filesystem changes in 'FTP Files' folder
    Watch()

    ' hookup Invoke callback
    _processInStream = New ProcessSocketCommandHandler(AddressOf ProcessSocketCommandIn)
    ' listen for Ultimate sending signatures
    _serverPRC.Start(My.Settings.listen_port_prc)
    myConn = New SqlConnection(My.Settings.Mill_SQL_Connect)
End Sub

Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
End Sub

' this is where we will break the data down into arrays
Private Sub processDataIn(ByVal data As Object)
    Try
        If data Is Nothing Then
            Throw New Exception("Stream empty!")
        End If

        Dim encdata As String

        ' decode to string and perform split(multi chars not supported)
        encdata = Encoding.Default.GetString(data)

        _strsql = encdata
        myConn.Open()
        Dim commPrice As New SqlCommand(_strsql, myConn)
        Dim resPrice As SqlDataReader = commPrice.ExecuteReader

        '********************************THIS MUST BE DYNAMIC FOR MORE THAN ONE NATIONAL
        If resPrice.Read = True And resPrice("ats" & "_price") IsNot DBNull.Value Then
            'If resPrice("ats" & "_price") Is DBNull.Value Then
            ' cannot find price so error
            'natPrice = ""
            'natAllow = 2
            'End If
            natPrice = resPrice("ats" & "_price")
            natAllow = resPrice("ats" & "_allow")
        Else
            ' cannot find price so error
            natPrice = ""
            natAllow = 2
        End If


        myConn.Close()
        ' substring not found therefore must be a pricing query
        'MsgBox("string: " & encdata.ToString)
        'natPrice = "9.99"

    Catch ex As Exception
        ErrHandle("4", "Process Error: " + ex.Message + ex.Data.ToString)
    Finally
        myConn.Close() ' dont forget to close!
    End Try
End Sub

'========================
'= ServerSocket methods =
'========================
' received a socket notification for receiving from Ultimate
Private Sub ProcessSocketCommandIn(ByVal command As NotifyCommandIn, ByVal data As Object)
    ' holds the status message for the command
    Dim status As String = ""
    Select Case command
        Case NotifyCommandIn.Listen
            'status = String.Format("Listening for server on {0} ...", CStr(data))
            status = "Waiting..."
        Case NotifyCommandIn.Connected
            'status = "Connected to Ultimate" ' + CStr(data)
            status = "Receiving..."
        Case NotifyCommandIn.Disconnected
            status = "Waiting..." ' disconnected from Ultimate now ready...
        Case NotifyCommandIn.ReceivedData
            ' store the encrypted data then process
            processDataIn(data)
    End Select
End Sub

' called from socket object when a network event occurs.
Private Sub NotifyCallbackIn(ByVal command As NotifyCommandIn, ByVal data As Object) Handles _serverPRC.Notify
    ' need to call Invoke before can update UI elements
    Dim args As Object() = {command, data}
    Invoke(_processInStream, args)
End Sub
末级

谢谢你的帮助
非常感谢

Invoke
System.Windows.Forms.Form
的成员,用于确保在UI线程上调用某个方法。如果所讨论的方法涉及UI控件,这是必需的

在这种情况下,看起来您可以直接调用该方法,即

而不是

Dim args As Object() = {command, data}
Invoke(_processInStream, args)
你可以简单地写

ProcessSocketCommandIn(command, data)

此外,在这种情况下,您可以去掉
\u processInStream
委托实例。

为什么需要将其作为服务运行?您是否想过使用任务调度器重复启动应用程序?我在您提供的类中没有看到提到的代码?我不明白它的用途是什么?嗨,我想要这项服务,因为我想让它在没有任何用户登录的情况下运行,我想让它从windows开始。“提到的”代码位于子notifycallbackin下的代码部分的底部。非常感谢您提供了如此快速和详细的回复-看看它,它应该有明显的效果,但这不是我探索过的VB领域。你的回答很有效,你让我的星期五过得很愉快!再次非常感谢