vb.net udpclient 发送大文件循环问题 50
vb.netudpclient发送大文件循环问题麻烦大神帮我在while里做一个发送数据流的代码,分包发送每包800字节,62毫秒发送一包,一直到发光,谢谢啦,或者给一个...
vb.net udpclient 发送大文件循环问题麻烦大神帮我在while 里做一个发送数据流的代码,分包发送每包800字节,62毫秒发送一包,一直到发光,谢谢啦,或者给一个vb.net的udpclient分包发送的mod。
展开
1个回答
展开全部
'缓冲区长度
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.Position<>fs.Length
willread=BufLen
If fs.Length-fs.Position<BufLen 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
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.Position<>fs.Length
willread=BufLen
If fs.Length-fs.Position<BufLen 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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询