VB6 had a winsock control that handled everything for you. In .Net you have to do everything yourself.
If you are using UDP it is fairly simple. If you are using TCP it is a little more complicated.
In both cases, for 2-way communication you need to create a Listener thread. Its code that runs on its own thread and simply listens for new data. Sending data is simple. See below for UDP code. Sorry about the formattiing. Local port is the port that recieves data. Remote port is the port you send data to. If two applications on the same machine, local and remote ports should be different, eg, 4000 and 4001. When connecting to a remote pc, they can use the same port number.
Option Explicit On
Option Strict On
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text.Encoding
Public Class UDP
' ----------------------------------------------------------------------------------
' Module Name: clsUDP
' Comments:
' Author: Chris Van Duin - BluescopeSteel Lab Services.
' Created: August 2004.
' Revisions: 13th January, 2005 - Chris Van Duin
' - Converted to
VB.NET 2005
' 04/01/2007 - C. Van Duin
' - Added LocalPort assignment to constructor.
' Local variables used:
' m_Client: System.Net.Sockets.UDPClient
' m_LocalPort: Local winsocket UDP is bound to. Default value is 4000.
' m_RemotePort: Remote winsocket UDP is bound to. Default value is 4000.
' m_RemoteHost: Remote Host computer to connect to.
' m_Thread: Thread.
' m_State: SocketStates
' ----------------------------------------------------------------------------------
#Region "Local variables"
Private m_Client As UdpClient
Private m_LocalPort As Integer = 4000
Private m_RemotePort As Integer = 4000
Private m_RemoteHost As String
Private m_Thread As Thread = New Thread(AddressOf Listen)
Private m_State As SocketStates
Public Enum SocketStates As Integer
CLOSED = 0
OPENED = 1
End Enum
Public Event DataArrival(ByVal strData As String)
#End Region
#Region "Constructors/Destructors"
' ----------------------------------------------------------------------------------
' Name: New
' Comments: Creates a new instance of this class.
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: 04/01/2007 - C. Van Duin
' - Added LocalPort assignment to constructor.
' Parameters:
' RemoteHost: Remote Host computer to connect to.
' RemotePort: Remote winsocket UDP is bound to.
' LocalPort: Local winsocket UDP is bound to.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Sub New(ByVal RemoteHost As String, _
ByVal RemotePort As Integer, _
ByVal LocalPort As Integer)
Me.m_RemoteHost = RemoteHost
Me.m_RemotePort = RemotePort
Me.m_LocalPort = LocalPort
End Sub
' ------------------------------------------------------------------------------------
' Name: Dispose
' Comments:
' Written By: Chris Van Duin - 11th May, 2007.
' Modifications: None.
' Parameters: None.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used: None.
' ------------------------------------------------------------------------------------
Public Sub Dispose()
Call CloseSocket()
End Sub
#End Region
#Region "Properties"
' ----------------------------------------------------------------------------------
' Name: LocalPort
' Comments: Syntax: Debug.Write X.LocalPort
' Syntax: X.LocalPort = 4000
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: None.
' Parameters: Value.
' Returns: LocalPort
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Property LocalPort() As Integer
Get
Return m_LocalPort
End Get
Set(ByVal Value As Integer)
m_LocalPort = Value
End Set
End Property
' ----------------------------------------------------------------------------------
' Name: RemotePort
' Comments: Syntax: Debug.Write X.RemotePort
' Syntax: X.RemotePort = 4000
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: None.
' Parameters: Value.
' Returns: RemotePort
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Property RemotePort() As Integer
Get
Return m_RemotePort
End Get
Set(ByVal Value As Integer)
m_RemotePort = Value
End Set
End Property
' ----------------------------------------------------------------------------------
' Name: RemoteHostName
' Comments: Syntax: Debug.Write X.RemoteHostName
' Syntax: X.RemoteHostName = "Local Host"
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: None.
' Parameters: Value.
' Returns: RemoteHostName
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Property RemoteHostName() As String
Get
Return m_RemoteHost
End Get
Set(ByVal Value As String)
m_RemoteHost = Value
End Set
End Property
' ----------------------------------------------------------------------------------
' Name: State
' Comments: Syntax: Debug.Write X.State
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: None.
' Parameters: None.
' Returns: SocketStates
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public ReadOnly Property State() As SocketStates
Get
Return m_State
End Get
End Property
#End Region
#Region "Methods"
' ----------------------------------------------------------------------------------
' Name: CloseSocket
' Comments:
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: 04/01/2007 - C. Van Duin
' - Renamed from StopSocket.
' Parameters: None.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Sub CloseSocket()
If m_State = SocketStates.OPENED Then
m_State = SocketStates.CLOSED
' Send an empty message to the listener thread so that it can
' check the SocketState and act accordingly.
Send("", "LocalHost", m_LocalPort)
End If
End Sub
' ----------------------------------------------------------------------------------
' Name: Listen
' Comments:
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: 04/01/2007 - C. Van Duin
' - Renamed from GetData.
' Parameters: None.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used:
' GetSocket: System.Net.Sockets.UDPClient
' RemoteComputer: System.Net.IPEndPoint
' myByte():
' msg: Message string being recieved
' ----------------------------------------------------------------------------------
Private Sub Listen()
Dim GetSocket As UdpClient
Dim RemoteComputer As System.Net.IPEndPoint
Dim myByte() As Byte
Dim msg As String
Try
GetSocket = New UdpClient(m_LocalPort)
RemoteComputer = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Do While m_State = SocketStates.OPENED
myByte = GetSocket.Receive(RemoteComputer)
msg = ASCII.GetString(myByte)
RaiseEvent DataArrival(msg)
Loop
Catch
End Try
End Sub
' ----------------------------------------------------------------------------------
' Name: OpenSocket
' Comments: Open socket for listening. Create Listener.
' Written By: Chris Van Duin - 4th January, 2007.
' Modifications: None.
' Parameters: None.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Sub OpenSocket()
If m_State = SocketStates.CLOSED Then
m_State = SocketStates.OPENED
m_Thread.Start()
End If
End Sub
' ----------------------------------------------------------------------------------
' Name: OpenSocket
' Comments:
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: 04/01/2007 - C. Van Duin
' - Renamed from Bind.
' Parameters:
' LocalPort: Local winsocket UDP is bound to.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Sub OpenSocket(ByVal LocalPort As Integer)
Me.LocalPort = LocalPort
Call OpenSocket()
End Sub
' ----------------------------------------------------------------------------------
' Name: Send
' Comments:
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: None.
' Parameters:
' Msg:
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used: None.
' ----------------------------------------------------------------------------------
Public Sub Send(ByVal Msg As String)
Call Send(Msg, RemoteHostName, RemotePort)
End Sub
' ----------------------------------------------------------------------------------
' Name: Send
' Comments: Send UDP message. Create a client if one does not already
' exist.
' Written By: Chris Van Duin - 21st August, 2004.
' Modifications: None.
' Parameters:
' Msg:
' RemoteHost: Remote Host computer to connect to.
' RemotePort: Remote winsocket UDP is bound to.
' Returns: Nothing.
' Affected Parameters: None.
' Local variables used:
' myByte(): Byte array.
' ----------------------------------------------------------------------------------
Public Sub Send(ByVal Msg As String, ByVal RemoteHost As String, _
ByVal RemotePort As Integer)
Dim myByte() As Byte
Try
Msg = Dns.GetHostName & ":" & Msg
myByte = ASCII.GetBytes(Msg)
If m_Client Is Nothing Then
m_Client = New UdpClient
End If
m_Client.Send(myByte, myByte.Length, RemoteHost, RemotePort)
Catch ex As System.Net.Sockets.SocketException
Throw New Exception("RemoteHost(" & RemoteHost & ") either not available " & _
"or RemotePort(" & RemotePort & ") specification invalid")
End Try
End Sub
#End Region
End Class