VB.Net Tutorial/Socket Network/Udp — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:55, 26 мая 2010
Содержание
Socket Udp connection
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Tester
Public Shared Sub Main
Dim client1 As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim server1 As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim MyIP As IPAddress
Dim str As String
Dim bytes1(200) As Byte
Dim bytes2(200) As Byte
Dim Port1 As Integer = 8080
Dim port2 As Integer = 3128
MyIP = IPAddress.Parse("168.169.1.81")
Dim server2 As New IPEndPoint(MyIP, Port1)
Dim client2 As New IPEndPoint(MyIP, port2)
client1.Bind(client2)
Try
str = "asdf"
bytes1 = Encoding.ASCII.GetBytes(str)
client1.SendTo(bytes1, server2)
client1.ReceiveFrom(bytes2, server2)
Catch ex As Exception
End Try
client1.Close()
End Sub
End Class
Udp Broadcast
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
UdpClient Multicast Receive
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 Multicast Send
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