vb.net UdpClient怎样及时的接收数据
麻烦给一个代码,因为我刚刚学.net嘛
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Public Class Form1
Dim udp As New UdpClient(11000)
Dim ReceiveThread As New Thread(AddressOf listen, 0)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ReceiveThread.Start()
End Sub
Sub listen()
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
While True
Dim receiveBytes As [Byte]() = udp.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
BeginInvoke(New EventHandler(AddressOf showMsg), returnData)
End While
End Sub
待续
End Class
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim bytes(1024) As Byte
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim LocalEndpoint As New IPEndPoint(IPAddress.Parse("10.92.20.57"), 1024)
s.Connect(LocalEndpoint)
s.Send(Encoding.Unicode.GetBytes(TextBox1.Text))
s.Close()
Catch ex As Exception
End Try
End Sub
End Class