I use TCPClientand TCPListenerto send data between applications running on the same computer. I am targeting Framework 4.5, so applications run on Windows Vista SP2 and higher, if I'm not mistaken. I use very simple code for this:
Customer:
Try
Using c = New Net.Sockets.TcpClient()
c.Connect("localhost", 7643)
If c.Connected Then
Dim Specs As New List(Of Spectrum)
' Generating some data here to send
Using stream = c.GetStream
Dim xml As New Xml.Serialization.XmlSerializer(GetType(List(Of Spectrum)))
xml.Serialize(stream, specs)
End Using
End If
End Using
Catch sEx As Net.Sockets.SocketException
MessageBox.Show("Could not send the data." & vbCrLf & _
"This can have multiple reasons:" & vbCrLf & _
"(i) The receiver is not running" & vbCrLf & _
"(ii) A firewall is blocking the connection")
Catch ex As Exception
MessageBox.Show("An unexpected exception occured while sending the data: " & vbCrLf & _
ex.Message)
End Try
Server:
Private Async Sub DoListen()
Do
Dim client = Await datagrabber.AcceptTcpClientAsync
If client IsNot Nothing AndAlso client.Connected Then
Dim t = Task.Run(Sub() HandleClient(client, Date.Now))
End If
Loop
End Sub
Private Sub HandleClient(client As Net.Sockets.TcpClient)
Dim specs As New List(Of RelaxIS_Shared.CSSpectrum)
Using stream = client.GetStream
Dim ser As New System.Xml.Serialization.XmlSerializer(GetType(List(Of RelaxIS_Shared.CSSpectrum)))
specs = CType(ser.Deserialize(stream), List(Of RelaxIS_Shared.CSSpectrum))
client.Close()
End Using
' Do stuff with data
End Sub
This works well, but I started wondering if this is really a reasonable way to send data between applications. For example, does this always work on a modern Windows machine, or what if the computer does not have a network card? Are TCP connections virtualized to work on a local IP address, regardless of the actual hardware in the system?
I found a lot of information about several network cards, but said nothing about network cards.
Windows. , , / , TCPClient?
, , , , . , Windows Communication Framework , .
# VB.NET , .
. . , , , . , -.
: usr . , WCF. , . , " " . , - ... .