VB.Net Tutorial/Socket Network/UdpClient — различия между версиями

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

Текущая версия на 12:55, 26 мая 2010

Create Udp Connection using UdpClient

Imports System.Net
Imports System.Net.Sockets
Public Class Tester
    Public Shared Sub Main
    
        Dim myUdpClient As UdpClient
        Try
            Dim myEndPoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345)
            myUdpClient = New UdpClient()
            myUdpClient.Connect(myEndPoint)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class

Use UdpClient to receive a file

Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Imports System.Net
Public Class Tester
    Public Shared Sub Main
        Try
            Dim bteDataAccept() As Byte
            Dim strdataAccept As String = ""
            Dim myEndPoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345)
            Dim myByte(36000) As Byte
            Dim myUdpClient As UdpClient = New UdpClient(12345)
            myByte = myUdpClient.Receive(Nothing)
            Dim myFileStream As FileStream = New FileStream("test.txt", FileMode.Create, FileAccess
.Write)
            myFileStream.Write(myByte, 0, myByte.Length)
            myFileStream.Close()
            myUdpClient.Close()
        Catch e As Exception
            Console.WriteLine(e.ToString )
        End Try
    End Sub
End Class

Use UdpClient to receive data

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Tester
    Public Shared Sub Main
        Try
            Dim bteDataAccept() As Byte
            Dim strdataAccept As String = ""
            Dim myEndPoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345)
            Dim myByte(36000) As Byte
            Dim myUdpClient As UdpClient = New UdpClient(12345)
            myByte = myUdpClient.Receive(myEndPoint)
        
            ""  strdataAccept = Encoding.ASCII.GetString(myByte)
            strdataAccept = Encoding.Unicode.GetString(myByte)
            Console.WriteLine(strdataAccept)
            myUdpClient.Close()
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class

Use UdpClient to send a file

Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Tester
    Public Shared Sub Main
        Try
            Dim dataSend() As Byte
            Dim myUdpClient As UdpClient = New UdpClient("127.0.0.1", 12345)
            Dim myFilestream As FileStream
            myFilestream = New FileStream("test.txt", FileMode.Open, FileAccess.Read)
            ReDim dataSend(myFilestream.Length)
            myFilestream.Read(dataSend, 0, myFilestream.Length)
            myUdpClient.Send(dataSend, dataSend.Length)
            myFilestream.Close()
            myUdpClient.Close()
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

    End Sub
End Class

Use UdpClient to send message

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Tester
    Public Shared Sub Main
        Try
            Dim dataSend() As Byte
            Dim myUdpClient As UdpClient
            myUdpClient = New UdpClient("127.0.0.1", 12345)
            ""dataSend = Encoding.ASCII.GetBytes(SendMessage)
            dataSend = Encoding.Unicode.GetBytes("SendMessage")
            myUdpClient.Send(dataSend, dataSend.Length)
            myUdpClient.Close()
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class