GremlinSA
January 22nd, 2008, 02:44 PM
Q: How do I retrieve a list of Local IP Address?
A:
Imports System.Net
Public Function GetLIPAdresses() As ArrayList<O:p
Dim NetInterfaces() As NetworkInformation.NetworkInterface = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces<O:p
Dim IPList As New ArrayList<O:p
Try<O:p
For Each NetInterface As NetworkInformation.NetworkInterface In NetInterfaces<O:p
Dim infosDns As NetworkInformation.UnicastIPAddressInformationCollection = NetInterface.GetIPProperties.UnicastAddresses<O:p
For Each address As NetworkInformation.UnicastIPAddressInformation In infosDns<O:p
IPList.Add(address.Address.ToString)<O:p
Next<O:p
Next<O:p
Catch ex As Exception<O:p
MessageBox.Show("Error: " & ex.Message)<O:p
End Try<O:p
Return IPList<O:p
End Function
'Code is thanks to aniskhan.
Q: How do I init a TCP port for linking (Listen or Client)
A:
Imports System.Net<O:p
Imports System.Text<O:p
Public Enum TCP_State<O:p
Listen<O:p
Conect<O:p
Client<O:p
End Enum<O:p
Private TCP_Listen As Sockets.TcpListener<O:p
Private TCP_Client As Sockets.TcpClient<O:p
Private Con_Method As TCP_State<O:p
Public Event Socket_Error(ByVal Err As Exception)<O:p
Public Function Init(ByVal Method As TCP_State, ByVal Port As Integer) As Boolean<O:p
Try<O:p
Con_Method = Method<O:p
Select Case Con_Method<O:p
Case TCP_State.Client<O:p
TCP_Client = New Sockets.TcpClient<O:p
Case TCP_State.Listen<O:p
TCP_Listen = New Sockets.TcpListener(L_IP, Port)<O:p
TCP_Listen.Start()<O:p
Case Else<O:p
Exit Function<O:p
End Select<O:p
C_Timer.Enabled = True<O:p
Init = True<O:p
Catch ex As Exception<O:p
Init = False<O:p
RaiseEvent Socket_Error(ex)<O:p
End Try<O:p
End Function
Q: How do I connect to a listening TCP port (Client)
A:
Public Function Connect(ByVal HostAddress As IPAddress, ByVal Port As Integer) As Boolean
Select Case Con_Method<O:p
Case TCP_State.Client<O:p
Try<O:p
TCP_Client.Connect(HostAddress, Port)<O:p
Return True<O:p
Catch ex As Exception<O:p
RaiseEvent Socket_Error(ex)<O:p
Return False<O:p
End Try<O:p
Case Else<O:p
Return False<O:p
End Select<O:p
End Function
Q: How do I accept a connection request to a listening TCP port
A: Because the Net class does not raise events, we need to use a timer to check if a connection request is pending, there after we can raise our own event.
Private WithEvents C_Timer As System.Windows.Forms.Timer
<O:p Public Event Connection_Request(ByVal Remote_IP As EndPoint)<O:p
Private Con_Proc As Boolean<O:p
Private Sub C_Timer_Elapsed(ByVal sender As Object, ByVal e As System.EventArgs) Handles C_Timer.Tick<O:p
Select Case Con_Method<O:p
Case TCP_State.Listen<O:p
If TCP_Listen.Pending Then<O:p
TCP_Client = TCP_Listen.AcceptTcpClient<O:p
RaiseEvent Connection_Request(TCP_Client.Client.RemoteEndPoint)<O:p
Con_Method = TCP_State.Conect<O:p
Con_Proc = True<O:p
' Close listening port... We've got our conection<O:p
TCP_Listen.Stop()<O:p
TCP_Listen = Nothing<O:p
End If<O:p
End Select<O:p
End Sub
Q: How do I check the connection status during a TCP connection
A: The following code snip runs in the same timer event as above. Public Event Connection_Complete(ByVal Remote_IP As EndPoint)
Public Event Connection_Error()
Private Sub C_Timer_Elapsed(ByVal sender As Object, ByVal e As System.EventArgs) Handles C_Timer.Tick
Select Case Con_Method
Case TCP_State.Client, TCP_State.Conect
If Con_Proc Then
If TCP_Client.Available > 0 Then
Try
ReDim RecievedBytes(TCP_Client.Available - 1)
TCP_Client.GetStream.Read(RecievedBytes, 0, TCP_Client.Available)
Tot_Array = UBound(B_Data) + 1
ReDim Preserve B_Data(Tot_Array)
ReDim Preserve B_Sender(Tot_Array)
ReDim Preserve Raw_Data(Tot_Array)
B_Data(Tot_Array) = Encoding.ASCII.GetString(RecievedBytes)
B_Sender(Tot_Array) = RemoteIpEndPoint.Address
Raw_Data(Tot_Array).Data = RecievedBytes
I_Timer.Enabled = True
Catch ex As Exception
RaiseEvent Socket_Error(ex)
End Try
End If
If Not TCP_Client.Connected Then
RaiseEvent Connection_Error()
End If
Else
If TCP_Client.Connected
RaiseEvent Connection_Complete(TCP_Client.Client.RemoteEndPoint)<O:p
Con_Proc = True<O:p
End If<O:p
End If<O:p
End Select<O:p
End Sub<O:p
<O:p
A:
Imports System.Net
Public Function GetLIPAdresses() As ArrayList<O:p
Dim NetInterfaces() As NetworkInformation.NetworkInterface = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces<O:p
Dim IPList As New ArrayList<O:p
Try<O:p
For Each NetInterface As NetworkInformation.NetworkInterface In NetInterfaces<O:p
Dim infosDns As NetworkInformation.UnicastIPAddressInformationCollection = NetInterface.GetIPProperties.UnicastAddresses<O:p
For Each address As NetworkInformation.UnicastIPAddressInformation In infosDns<O:p
IPList.Add(address.Address.ToString)<O:p
Next<O:p
Next<O:p
Catch ex As Exception<O:p
MessageBox.Show("Error: " & ex.Message)<O:p
End Try<O:p
Return IPList<O:p
End Function
'Code is thanks to aniskhan.
Q: How do I init a TCP port for linking (Listen or Client)
A:
Imports System.Net<O:p
Imports System.Text<O:p
Public Enum TCP_State<O:p
Listen<O:p
Conect<O:p
Client<O:p
End Enum<O:p
Private TCP_Listen As Sockets.TcpListener<O:p
Private TCP_Client As Sockets.TcpClient<O:p
Private Con_Method As TCP_State<O:p
Public Event Socket_Error(ByVal Err As Exception)<O:p
Public Function Init(ByVal Method As TCP_State, ByVal Port As Integer) As Boolean<O:p
Try<O:p
Con_Method = Method<O:p
Select Case Con_Method<O:p
Case TCP_State.Client<O:p
TCP_Client = New Sockets.TcpClient<O:p
Case TCP_State.Listen<O:p
TCP_Listen = New Sockets.TcpListener(L_IP, Port)<O:p
TCP_Listen.Start()<O:p
Case Else<O:p
Exit Function<O:p
End Select<O:p
C_Timer.Enabled = True<O:p
Init = True<O:p
Catch ex As Exception<O:p
Init = False<O:p
RaiseEvent Socket_Error(ex)<O:p
End Try<O:p
End Function
Q: How do I connect to a listening TCP port (Client)
A:
Public Function Connect(ByVal HostAddress As IPAddress, ByVal Port As Integer) As Boolean
Select Case Con_Method<O:p
Case TCP_State.Client<O:p
Try<O:p
TCP_Client.Connect(HostAddress, Port)<O:p
Return True<O:p
Catch ex As Exception<O:p
RaiseEvent Socket_Error(ex)<O:p
Return False<O:p
End Try<O:p
Case Else<O:p
Return False<O:p
End Select<O:p
End Function
Q: How do I accept a connection request to a listening TCP port
A: Because the Net class does not raise events, we need to use a timer to check if a connection request is pending, there after we can raise our own event.
Private WithEvents C_Timer As System.Windows.Forms.Timer
<O:p Public Event Connection_Request(ByVal Remote_IP As EndPoint)<O:p
Private Con_Proc As Boolean<O:p
Private Sub C_Timer_Elapsed(ByVal sender As Object, ByVal e As System.EventArgs) Handles C_Timer.Tick<O:p
Select Case Con_Method<O:p
Case TCP_State.Listen<O:p
If TCP_Listen.Pending Then<O:p
TCP_Client = TCP_Listen.AcceptTcpClient<O:p
RaiseEvent Connection_Request(TCP_Client.Client.RemoteEndPoint)<O:p
Con_Method = TCP_State.Conect<O:p
Con_Proc = True<O:p
' Close listening port... We've got our conection<O:p
TCP_Listen.Stop()<O:p
TCP_Listen = Nothing<O:p
End If<O:p
End Select<O:p
End Sub
Q: How do I check the connection status during a TCP connection
A: The following code snip runs in the same timer event as above. Public Event Connection_Complete(ByVal Remote_IP As EndPoint)
Public Event Connection_Error()
Private Sub C_Timer_Elapsed(ByVal sender As Object, ByVal e As System.EventArgs) Handles C_Timer.Tick
Select Case Con_Method
Case TCP_State.Client, TCP_State.Conect
If Con_Proc Then
If TCP_Client.Available > 0 Then
Try
ReDim RecievedBytes(TCP_Client.Available - 1)
TCP_Client.GetStream.Read(RecievedBytes, 0, TCP_Client.Available)
Tot_Array = UBound(B_Data) + 1
ReDim Preserve B_Data(Tot_Array)
ReDim Preserve B_Sender(Tot_Array)
ReDim Preserve Raw_Data(Tot_Array)
B_Data(Tot_Array) = Encoding.ASCII.GetString(RecievedBytes)
B_Sender(Tot_Array) = RemoteIpEndPoint.Address
Raw_Data(Tot_Array).Data = RecievedBytes
I_Timer.Enabled = True
Catch ex As Exception
RaiseEvent Socket_Error(ex)
End Try
End If
If Not TCP_Client.Connected Then
RaiseEvent Connection_Error()
End If
Else
If TCP_Client.Connected
RaiseEvent Connection_Complete(TCP_Client.Client.RemoteEndPoint)<O:p
Con_Proc = True<O:p
End If<O:p
End If<O:p
End Select<O:p
End Sub<O:p
<O:p