'xiansr.2014/2/7 vb.net2010 调试通过
创新互联公司是一家集网站建设,北湖企业网站建设,北湖品牌网站建设,网站定制,北湖网站建设报价,网络营销,网络优化,北湖网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim udpClient As New UdpClient(11000)
Try
udpClient.Connect("127.0.0.1", 11000)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(RichTextBox1.Text)
udpClient.Send(sendBytes, sendBytes.Length)
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
RichTextBox2.Text = "接收到: " + returnData.ToString()
'Debug.Print(("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()))
udpClient.Close()
Catch ex As Exception
stop
End Try
End Sub
End Class
'缓冲区长度
Const BufLen As Integer=800
'包发送间隔
Const Interval As Integer=62
'缓冲区
Dim buf As Byte()=Nothing
'远程(目的)网络端点
Dim remoteep As IPEndPoint=Nothing
'如果你已将s定义为类的成员变量(实例或共享),注释掉下面这句
Dim s As UdpClient=Nothing
Dim willread As Integer
Try
buf=New Byte(BufLen-1) {}
s=New UdpClient()
' TextBox1.Text包含远程(目的)主机的IP地址
' TextBox2.Text包含远程(目的)主机的端口号
remoteep=New IPEndPoint(IPAddress.Parse(Trim(TextBox1.Text)),CInt(TextBox2.Text))
' Label2.Text包含要发送的文件的路径
Using fs As New FileStream(Label2.Text,FileMode.Open,FileAccess.Read)
While fs.Positionfs.Length
willread=BufLen
If fs.Length-fs.PositionBufLen Then
willread=CInt(fs.Length-fs.Position)
End If
fs.Read(buf,0,willread)
s.Send(buf,willread,remoteep)
Thread.Sleep(Interval)
End While
End Using
Catch ex As Exception
MsgBox(ex.ToString())
Finally
If s IsNot Nothing Then
s.Close()
End If
End Try
DatagramSocket用于接收和发送UDP的Socket实例。该类有3个构造函数:DatagramSocket():通常用于客户端编程,它并没有特定监听的端口,仅仅使用一个临时的。程序会让操作系统分配一个可用的端口。DatagramSocket(int port):创建实例,并固定监听Port端口的报文。通常用于服务端。