VB.Net by API/System.Net.Sockets/UdpClient

Материал из VB Эксперт
Перейти к: навигация, поиск

UdpClient.DropMulticastGroup

  
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Threading
Public Class Tester
    Dim Shared GroupIP As IPAddress
    Dim Shared GroupEP As IPEndPoint
    Dim Shared ListenUdp As UdpClient
    Public Shared Sub Main
        Dim ListenThread As Thread
        ListenThread = New Thread(AddressOf BeginListen)
        ListenThread.Start()
"        ListenUdp.DropMulticastGroup(GroupIP)
"        ListenUdp.Close()
"        ListenThread.Abort()
    End Sub
    Private Shared Sub BeginListen()
        Dim bteReceiveData() As Byte
        Dim strReceiveData As String
        GroupIP = IPAddress.Parse("127.0.0.1")
        GroupEP = New IPEndPoint(GroupIP, 12345)
        ListenUdp = New UdpClient(12345)
        ListenUdp.JoinMulticastGroup(GroupIP)
        Do
            bteReceiveData = ListenUdp.Receive(GroupEP)
            strReceiveData = Encoding.Unicode.GetString(bteReceiveData)
            Console.WriteLine(strReceiveData)
        Loop
    End Sub
End Class


UdpClient.JoinMulticastGroup

  
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Net
Imports System.Threading
Public Class Tester
    Public Shared Sub Main
        Dim GroupEP As IPEndPoint
        Dim SendUdp As New UdpClient()
        Dim GroupIP As IPAddress
        GroupIP = IPAddress.Parse("127.0.0.1")
        GroupEP = New IPEndPoint(GroupIP, 12345)
        SendUdp.JoinMulticastGroup(GroupIP, 12)
        Dim bteSendDate() As Byte
        Try
            bteSendDate = Encoding.Unicode.GetBytes("message")
            SendUdp.Send(bteSendDate, bteSendDate.Length, GroupEP)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Class


UdpClient.Receive

  

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Tester
    Public Shared Sub Main
    
        Dim MyIP As IPAddress
        Dim bytes(200) As Byte
        Dim Port1 As Integer = 8080
        Dim port2 As Integer = 3128
        MyIP = IPAddress.Parse("166.111.175.81")
        Dim myLocal As New IPEndPoint(MyIP, Port1)
        Dim myRemote As New IPEndPoint(MyIP, port2)
        Dim receive1 As New UdpClient(myRemote)
        Dim send1 As New UdpClient(myLocal)
        Try
            bytes = System.Text.Encoding.ASCII.GetBytes("AAA")
            send1.Send(bytes, bytes.Length, myRemote)
            receive1.Receive(myLocal)
        Catch ex As Exception
            Console.WriteLine("Error")
        End Try
        send1.Close()
        receive1.Close()
    End Sub
End Class