VB.net服务编程和使用TCP套接字(VB.net Service Programming and

2019-09-17 06:24发布

我有一个问题,我很好奇,如果有人可以帮助我解决这个问题。 我把客户端 - 服务器套接字编程VB.NET教程。 然后,我尝试使用服务,而不是一个程序来实现它。 我理解它是如何工作的程序,但是当我尝试将它移植到一个服务这是行不通的。 当我运行该服务在启动瞬间停止。 它从来没有打通。 不幸的是,我没有很大的VB.net程序员,但到目前为止,我喜欢它很多关于程序快速发展。

这项服务的想法是:

  1. 在计算机启动时运行
  2. 抢PC的名称
  3. 发送PC的名称服务器

    一种。 服务器然后采取的名称,并期待它在数据库中

    湾 返回客户端机器想备份的时间

  4. 然后,客户机做数学当前时间和时间它的假设来备份和把一切都放在毫秒。

  5. 那么该机在那个特定的时间通过运行DOS命令来启动该程序备份。

现在回答这个问题我已经在论坛上找到共同的问题。 我为什么不使用任务调度。 嗯,我没有使用任务调度和有服务器控制时间到机器的方式。 但是,某些计算机将进入休眠状态,我会说这休眠状态,影响了机器的20%。 没有这种休眠状态不是休眠模式,而不是冬眠。 这些电脑都在和他们的反应非常迅速地移动鼠标。 我创建了写入时间在C文件的服务:\,这一直工作。 所以,现在我已经决定要在客户机上的服务,并将它与服务器进行通信。

我收集了有关创建服务和网络套接字编程的信息非常少。 不幸的是,我还没有发现任何东西捆绑在一起的2。 我发现了一个vb.net客户端 - 服务器程序,我想要做什么,但我想它不作为服务的程序。 我发现创建一个使用PSEXEC从服务器上文件的临时解决方案,但这个过程就是这么嗯不成熟。

我做了一个最好的事情。我去一回顾了微软库插槽,并试图基于情理之中的事情,以建立自己的服务。 还是没有什么作品。 如果您知道的任何书籍,资源,有什么建议等任何帮助,您可以给我将不胜感激。 谢谢您的帮助。

下面你会发现我的代码。 在这一点上,我才不在乎这样做是使客户端和服务器之间的连接。 我可以回去搞清楚休息,并从那里tweek的代码。

麦克风

这里是服务器的代码,我一直在玩:

Imports System.Net.Sockets
Imports System.Net
Imports System.Text

Public Class BackupService

    Private Mythread As Threading.Thread
    Private clientThread As Threading.Thread
    Private listener As New TcpListener(IPAddress.Parse("#.#.#.252"), 8888)



    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.

        listener.Start()            'Listener for clients
        System.IO.File.WriteAllText("C:\test\listener.txt", My.Computer.Clock.LocalTime)
        Mythread = New Threading.Thread(AddressOf listenerLoop)
        Mythread.Start()

    End Sub

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

    Protected Sub listenerLoop()

        Dim client As TcpClient = listener.AcceptTcpClient()
        Dim networkStream As NetworkStream = client.GetStream
        Dim bytes(client.ReceiveBufferSize) As Byte
        Dim dataReceived As String


        While True
            networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))            'Receives data from client and stores it into bytes
            dataReceived = Encoding.ASCII.GetString(bytes)                          'Encodes the data to ASCII standard
            System.IO.File.AppendAllText("C:\test\listener.txt", dataReceived)      'Copies information to text file
            Threading.Thread.Sleep(1000)

        End While




        'Listening for incoming connections
        'While True
        '    If (listener.Pending = False) Then
        '        System.IO.File.AppendAllText("C:\test\listener.txt", "Sorry, no connection requests have arrived")
        '    Else
        '        'Finds Incoming message and creates a thread for the client-server to pass information'
        '        clientThread = New Threading.Thread(AddressOf clientConnection)
        '        clientThread.Start()

        '    End If
        '    Threading.Thread.Sleep(1000)    'Let loop/thread sleep for 1 second to allow other processing and waits for clients
        'End While
    End Sub

    'Protected Sub clientConnection()
    '    Dim client As TcpClient = listener.AcceptTcpClient()
    '    Dim networkStream As NetworkStream = client.GetStream
    '    Dim bytes(client.ReceiveBufferSize) As Byte
    '    Dim dataReceived As String
    '    Dim datasent As Boolean = False

    '    While datasent = False  'Continuously loops looking for sent data
    '        If (networkStream.CanRead = True) Then
    '            networkStream.Read(bytes, 0, CInt(client.ReceiveBufferSize))            'Receives data from client and stores it into bytes
    '            dataReceived = Encoding.ASCII.GetString(bytes)                          'Encodes the data to ASCII standard
    '            System.IO.File.AppendAllText("C:\test\listener.txt", dataReceived)      'Copies information to text file
    '            datasent = True
    '        End If
    '        Threading.Thread.Sleep(1000)
    '    End While

    '    networkStream.Close()       'Closes the network stream
    '    client.Close()              'Closes the client
    '    clientThread.Abort()        'Kills the the current thread

    'End Sub
End Class

客户代码(服务):

Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class TestWindowsService

    Dim Mythread As Threading.Thread

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.


        'clientCommunication()


        Mythread = New Threading.Thread(AddressOf KeepCounting)
        Mythread.Start()
    End Sub

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

        Mythread.Abort()
    End Sub

    'Protected Sub KeepCounting()
    '    Dim wait As Integer = 0
    '    Dim hour As Integer = 0
    '    Dim min As Integer = 0

    '    System.IO.File.WriteAllText("C:\test\StartTime.txt", "Start Time: " & My.Computer.Clock.LocalTime)


    '    Do While True

    '        hour = My.Computer.Clock.LocalTime.Hour

    '        If (hour = 1) Then
    '            min = (My.Computer.Clock.LocalTime.Minute * 60) + 60000
    '            Threading.Thread.Sleep(min)         'Sleeps for the number of minutes till 2am
    '            file.FileTime()
    '        Else
    '            Threading.Thread.Sleep(3600000)     'Sleeps for 1 hour
    '            System.IO.File.WriteAllText("C:\test\hourCheck\ThreadTime.txt", "Time: " & My.Computer.Clock.LocalTime)

    '        End If

    '    Loop

    'End Sub

    Protected Sub KeepCounting()
        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect(IPAddress.Parse("#.#.#.11"), 8000)
        Dim networkStream As NetworkStream = tcpClient.GetStream()

        If networkStream.CanWrite And networkStream.CanRead Then

            ' Do a simple write.
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
            networkStream.Write(sendBytes, 0, sendBytes.Length)

            ' Read the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Host returned: " + returndata))


        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    tcpClient.Close()
                End If
            End If
        End If
        ' pause so user can view the console output
        Console.ReadLine()


    End Sub

End Class

客户机代码(扩展模块)

Imports System.Net.Sockets
Imports System.Net
Imports System.Text

Module Client_TCP_Communication

    Public Sub clientCommunication()
        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect("127.0.0.1", 8000)
        Dim networkStream As NetworkStream = tcpClient.GetStream()

        If networkStream.CanWrite And networkStream.CanRead Then

            ' Do a simple write.
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
            networkStream.Write(sendBytes, 0, sendBytes.Length)

            ' Read the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Host returned: " + returndata))


        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    tcpClient.Close()
                End If
            End If
        End If
        ' pause so user can view the console output
        Console.ReadLine()





        'Dim clientSocket As New System.Net.Sockets.TcpClient()
        'Dim serverStream As NetworkStream

        'While True
        '    serverStream = clientSocket.GetStream()
        '    Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes("Message from client$")
        '    Dim inStream(1024) As Byte
        '    Dim returnData As String

        '    System.IO.File.WriteAllText("C:\test\client\ClientStarted.txt", "Time: " & My.Computer.Clock.LocalTime)
        '    clientSocket.Connect(IPAddress.Parse("#.#.#.11"), 8999)
        '    System.IO.File.WriteAllText("C:\test\client\ClientConnected.txt", "Time: " & My.Computer.Clock.LocalTime)

        '    serverStream.Write(outStream, 0, outStream.Length)
        '    serverStream.Flush()

        '    serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
        '    returnData = System.Text.Encoding.ASCII.GetString(inStream)
        '    System.IO.File.WriteAllText("C:\test\client\returnData.txt", "Time: " & returnData)

        'End While

    End Sub

End Module

Answer 1:

要找出为什么它的开始,然后停止,你可以尝试尝试启动服务后,看在应用程序事件日志。 可以运行到上,导致该服务停止(或只是之后)启动时产生错误。

我跑进试图写一个类似的服务时的问题 - 在我的情况,我试图自动检测要使用的IP地址,它原来是无意中选择我的IPv6环回地址,未能绑定。 在事件日志中的错误,在此暗示我。



文章来源: VB.net Service Programming and using TCP Sockets