'xiansr.2014/2/7 vb.net2010 調(diào)試通過
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,北湖企業(yè)網(wǎng)站建設,北湖品牌網(wǎng)站建設,網(wǎng)站定制,北湖網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,北湖網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
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
'緩沖區(qū)長度
Const BufLen As Integer=800
'包發(fā)送間隔
Const Interval As Integer=62
'緩沖區(qū)
Dim buf As Byte()=Nothing
'遠程(目的)網(wǎng)絡端點
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包含要發(fā)送的文件的路徑
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用于接收和發(fā)送UDP的Socket實例。該類有3個構造函數(shù):DatagramSocket():通常用于客戶端編程,它并沒有特定監(jiān)聽的端口,僅僅使用一個臨時的。程序會讓操作系統(tǒng)分配一個可用的端口。DatagramSocket(int port):創(chuàng)建實例,并固定監(jiān)聽Port端口的報文。通常用于服務端。
名稱欄目:vb.net中udp net和vb
當前網(wǎng)址:http://jinyejixie.com/article30/hehdpo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、軟件開發(fā)、用戶體驗、建站公司、營銷型網(wǎng)站建設、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)