VB.net服务编程和使用TCP套接字

我有一个问题,我很好奇,如果有人可以帮我解决它。 我为VB.NET编写了客户端 – 服务套接字编程教程。 然后我尝试使用服务而不是程序来实现它。 我了解它是如何作为一个程序工作,但是当我尝试将它移植到一个服务,它不起作用。 当我运行服务时,它立即启动并停止。 它从来没有build立连接。 不幸的是,我并不是那么棒的VB.net程序员,但到目前为止我对程序的快速开发非常喜欢。

这项服务的想法是:

  1. 电脑启动时运行
  2. 抓住电脑的名字
  3. 将PC的名称发送到服务器

    一个。 服务器然后取名称并在数据库中查找它

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

  4. 然后,客户端机器进行当前时间的math计算,以及假设备份并将所有内容都放入ms中的时间。

  5. 然后在指定的时间通过运行dos命令启动程序来备份机器。

现在回答我在论坛中发现的一个常见问题。 为什么我不使用任务调度程序。 那么我确实使用任务计划,并有服务器控制时间机器的方式。 但是,有些电脑会进入hibernate状态,我认为这个hibernate状态影响了20%的机器。 没有这个hibernate状态不是睡眠模式,也不是hibernate状态。 电脑开着,他们对鼠标移动的反应非常快。 我创build了一个将时间写入C:\上的文件的服务,并且一直有效。 所以现在我决定在客户端机器上有一个服务,并与服务器通信。

我收集了很less关于创build服务和networking套接字编程的信息。 不幸的是,我还没有发现任何将两者联系在一起的东西。 我发现一个vb.net客户端服务器程序,做我想要的,但我想它作为一个服务,而不是一个程序。 我发现了一个使用PSEXEC从服务器上创build文件的临时解决scheme,但这个过程简直就是这么简单。

我做了下一个最好的事情,我去了微软的套接字库,并试图build立自己的服务基于什么是有道理的。 还是没有用。 如果你知道任何书籍,资源,有任何意见,等任何帮助,你可以给我将不胜感激。 谢谢您的帮助。

下面你会发现我的代码。 在这一点上,我所关心的是在客户端和服务器之间build立连接。 我可以重新找出其余的东西,并从那里撬动代码。

麦克风

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

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 

Solutions Collecting From Web of "VB.net服务编程和使用TCP套接字"

要找出启动和停止的原因,可以尝试在启动服务后查看应用程序事件日志。 可能会在启动时(或刚刚启动后)导致服务停止时发生错误。

当我试图编写一个类似的服务时,遇到了这个问题 – 在我的情况下,我试图自动检测IP地址使用,事实证明,这是无意中选择我的IPv6回送地址,并无法绑定。 事件日志中的错误暗示了我的这一点。