私は問題を抱えており、誰かがそれを解決するのを手伝ってくれるかどうか興味がありました. VB.NET のクライアント サーバー ソケット プログラミングのチュートリアルを受講しました。次に、プログラムではなくサービスを使用して実装しようとしました。プログラムとしての動作は理解していますが、サービスに移植しようとすると動作しません。サービスを実行すると、すぐに開始および停止します。決してつながりません。残念ながら、私は VB.net プログラマーがそれほど得意ではありませんが、これまでのところ、プログラムの迅速な開発には大いに気に入っています。
このサービスのアイデアは次のとおりです。
- コンピュータの起動時に実行
- PCの名前をつかむ
PCの名前をサーバーに送信する
a. サーバーは名前を取得し、データベースで検索します
b. クライアント マシンがバックアップする予定の時刻を返します
次に、クライアント マシンは、現在の時刻と、すべてをミリ秒単位でバックアップして配置する予定の時刻を計算します。
- 次に、dos コマンドを実行してプログラムを起動することにより、指定された時刻にマシンがバックアップされます。
ここで、フォーラムでよくある質問に答えます。タスクスケジューラを使用しないのはなぜですか。さて、私はタスクスケジュールを使用し、サーバーがそのようにマシンへの時間を制御するようにしました。ただし、一部のコンピューターは休止状態になります。この休止状態はマシンの 20% に影響すると言えます。いいえ、この休眠状態はスリープ モードでも休止状態でもありません。コンピュータの電源が入っていて、マウスの動きに非常に素早く反応します。C:\ のファイルに時刻を書き込むサービスを作成しましたが、これは常に機能しています。そこで、クライアント マシンにサービスを配置し、サーバーと通信させることにしました。
サービスおよびネットワーク ソケット プログラミングの作成に関する情報はほとんど収集していません。残念ながら、この 2 つを結び付けるものは見つかりませんでした。私が望むことを行う vb.net クライアント サーバー プログラムを見つけましたが、それをプログラムではなくサービスとして使用したいと考えています。サーバーからPSEXECを使用してファイルを作成する一時的な解決策を見つけましたが、このプロセスは非常に単純です。
私は次善の策を講じ、ソケット用の Microsoft ライブラリにアクセスして確認し、理にかなっていることに基づいて独自のサービスを構築しようとしました。それでも何も機能しません。書籍、リソース、アドバイスなどをご存じでしたら、お力添えをいただければ幸いです。ご協力いただきありがとうございます。
以下に私のコードがあります。この時点で私が気にしているのは、クライアントとサーバー間の接続を確立することだけです。残りの部分を見つけて、そこからコードを微調整できます。
マイク
私が遊んでいるサーバーコードは次のとおりです。
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