Ok, eine Windows Forms Version (schnell geschrieben und nicht besonders gut getestet). Diesmal zwei Projekte.
Client (TextBox - CommandInput, Button - SendButton, RichTextBox - LogData)
Imports System.IO
Imports System.IO.Pipes
Imports System.Threading
Public Class Form1
Private pipeStream As NamedPipeClientStream
Private sw As StreamWriter
Private sr As StreamReader
Public Sub SendData(data As String)
If pipeStream Is Nothing Then
pipeStream = New NamedPipeClientStream("mytestpipe")
pipeStream.Connect()
addLine("[Client] Pipe connection established")
sw = New StreamWriter(pipeStream)
sw.AutoFlush = True
End If
addLine("[Client] send data: " & data)
sw.WriteLine(data)
If data = "time" Then
If sr Is Nothing Then
sr = New StreamReader(pipeStream)
End If
Dim response As String = sr.ReadLine()
addLine("[Response] time=" & response)
End If
End Sub
Private Sub addLine(line As String)
LogData.AppendText(line & vbCrLf)
LogData.SelectionLength = LogData.TextLength
LogData.ScrollToCaret()
End Sub
Private Sub SendButton_Click(sender As System.Object, e As _
System.EventArgs) Handles SendButton.Click
If CommandInput.Text IsNot String.Empty Then
SendData(CommandInput.Text)
CommandInput.Clear()
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As _
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If pipeStream IsNot Nothing Then pipeStream.Close()
End Sub
End Class Server (RichTextBox - LogData)
Imports System.IO
Imports System.IO.Pipes
Imports System.Threading
Public Class Form1
Public Sub ThreadStartServer()
Using pipeStream As New NamedPipeServerStream("mytestpipe")
addLine("[Server] Pipe created")
pipeStream.WaitForConnection()
addLine("[Server] Pipe connection established")
Using sr As New StreamReader(pipeStream),
sw As New StreamWriter(pipeStream)
sw.AutoFlush = True
Dim temp As String = String.Empty
While (assign(temp, sr.ReadLine())) IsNot Nothing
Dim dt As DateTime = DateTime.Now
addLine(String.Format("[Server] {0}: {1}", dt, temp))
If temp = "time" Then
sw.WriteLine(dt.ToString())
End If
End While
End Using
End Using
addLine("[Server] Connection closed")
End Sub
Private Sub addLine(line As String)
If InvokeRequired Then
Invoke(New Action(Of String)(AddressOf addLine), New String() _
{line})
Return
End If
LogData.AppendText(line & vbCrLf)
LogData.SelectionLength = LogData.TextLength
LogData.ScrollToCaret()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
Handles MyBase.Load
Dim ServerThread As New Thread(AddressOf ThreadStartServer)
ServerThread.IsBackground = True
ServerThread.Start()
End Sub
Private Shared Function assign(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
End Class ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |