发送端代码
程序代码: | [ 复制代码到剪贴板 ] |
Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Public Class Form1
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim UDP_Client As New UdpClient
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If TxtIP.Text = "" Or txtPort.Text = "" Then Exit Sub
If txtFile.Text = "" Then Exit Sub
Dim socketSent As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
Dim xIpEndPoint As New IPEndPoint(Net.IPAddress.Parse(Trim(TxtIP.Text)), CLng(txtPort.Text))
Sleep(1000)
System.Windows.Forms.Application.DoEvents()
Me.ListBox1.Items.Add("连接成功")
Dim Fs As New FileStream(txtFile.Text, FileMode.OpenOrCreate, FileAccess.Read)
Me.ListBox1.Items.Add("文件总字节:" & Fs.Length)
If Fs.Length < 65507 Then 'UDP发送的最大数据包65507
Dim File(Fs.Length - 1) As Byte
Dim StrRead As New BinaryReader(Fs)
StrRead.Read(File, 0, File.Length)
UDP_Client.Send(File, File.Length, xIpEndPoint)
Me.ListBox1.Items.Add("已发送字节数:" & Fs.Length)
Else
Me.ListBox1.Items.Add("文件较大,需分包发送。")
Dim Packet() As Byte
Dim N As Integer = Fs.Length \ 65500
Me.ListBox1.Items.Add("分包:" & N + 1 & " 份,每个包大小:65500 字节")
Dim Y As Integer = Fs.Length Mod 65500
Me.ListBox1.Items.Add("最后一个包大小:" & Y & " 字节")
For i As Integer = 0 To N
If i = N Then
ReDim Packet(Y - 1)
Fs.Read(Packet, 0, Y)
Sleep(1000)
Else
ReDim Packet(65499)
Fs.Read(Packet, 0, 65500)
Sleep(1000)
End If
UDP_Client.Send(Packet, Packet.Length, xIpEndPoint)
Next
End If
Sleep(1000)
'发送文件传输完成标志:
Dim B As Byte() = System.Text.Encoding.Unicode.GetBytes("TcFileComplete")
UDP_Client.Send(B, B.Length, xIpEndPoint)
Fs.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
Dim OFD As New OpenFileDialog
Try
OFD.ShowDialog()
Me.txtFile.Text = OFD.FileName
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Public Class Form1
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim UDP_Client As New UdpClient
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If TxtIP.Text = "" Or txtPort.Text = "" Then Exit Sub
If txtFile.Text = "" Then Exit Sub
Dim socketSent As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
Dim xIpEndPoint As New IPEndPoint(Net.IPAddress.Parse(Trim(TxtIP.Text)), CLng(txtPort.Text))
Sleep(1000)
System.Windows.Forms.Application.DoEvents()
Me.ListBox1.Items.Add("连接成功")
Dim Fs As New FileStream(txtFile.Text, FileMode.OpenOrCreate, FileAccess.Read)
Me.ListBox1.Items.Add("文件总字节:" & Fs.Length)
If Fs.Length < 65507 Then 'UDP发送的最大数据包65507
Dim File(Fs.Length - 1) As Byte
Dim StrRead As New BinaryReader(Fs)
StrRead.Read(File, 0, File.Length)
UDP_Client.Send(File, File.Length, xIpEndPoint)
Me.ListBox1.Items.Add("已发送字节数:" & Fs.Length)
Else
Me.ListBox1.Items.Add("文件较大,需分包发送。")
Dim Packet() As Byte
Dim N As Integer = Fs.Length \ 65500
Me.ListBox1.Items.Add("分包:" & N + 1 & " 份,每个包大小:65500 字节")
Dim Y As Integer = Fs.Length Mod 65500
Me.ListBox1.Items.Add("最后一个包大小:" & Y & " 字节")
For i As Integer = 0 To N
If i = N Then
ReDim Packet(Y - 1)
Fs.Read(Packet, 0, Y)
Sleep(1000)
Else
ReDim Packet(65499)
Fs.Read(Packet, 0, 65500)
Sleep(1000)
End If
UDP_Client.Send(Packet, Packet.Length, xIpEndPoint)
Next
End If
Sleep(1000)
'发送文件传输完成标志:
Dim B As Byte() = System.Text.Encoding.Unicode.GetBytes("TcFileComplete")
UDP_Client.Send(B, B.Length, xIpEndPoint)
Fs.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
Dim OFD As New OpenFileDialog
Try
OFD.ShowDialog()
Me.txtFile.Text = OFD.FileName
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
End Class
接收端代码
程序代码: | [ 复制代码到剪贴板 ] |
Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Public Class Form1
Public UDP_Listen As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Public T As Thread
Public IpEndPoint As IPEndPoint
Dim Fs As FileStream
Public Function MB_to_Bytes(ByVal MB As Integer) As Integer
Dim IntBytes As Integer
IntBytes = MB * 1024 * 1024
Return IntBytes
End Function
Private Sub Listen()
Try
Dim Bytes(65536) As Byte
Dim NumGet As Integer
While True
NumGet = UDP_Listen.Receive(Bytes)
Debug.Print(NumGet)
'文件传输完成标志:"TcFileComplete"
If NumGet = 28 Then
Dim NewBytes(27) As Byte
Array.Copy(Bytes, 0, NewBytes, 0, 28)
If System.Text.Encoding.Unicode.GetString(NewBytes) = "TcFileComplete" Then
Debug.Print("TcFileComplete")
Fs.Close()
MsgBox("文件传输完成!")
End If
Else
Fs.Write(Bytes, 0, NumGet)
End If
End While
Catch ex As Exception
End Try
End Sub
Private Sub btnListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListen.Click
If TxtIP.Text = "" Or txtPort.Text = "" Then Exit Sub
Dim socketSent As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Fs = New FileStream(txtFile.Text, FileMode.Create)
Try
IpEndPoint = New IPEndPoint(Net.IPAddress.Parse(Trim(TxtIP.Text)), CLng(txtPort.Text))
UDP_Listen.Bind(IpEndPoint)
T = New Thread(AddressOf Listen)
T.Start()
MessageBox.Show("已经成功启动监听。")
Me.btnListen.Enabled = False
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Public Class Form1
Public UDP_Listen As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Public T As Thread
Public IpEndPoint As IPEndPoint
Dim Fs As FileStream
Public Function MB_to_Bytes(ByVal MB As Integer) As Integer
Dim IntBytes As Integer
IntBytes = MB * 1024 * 1024
Return IntBytes
End Function
Private Sub Listen()
Try
Dim Bytes(65536) As Byte
Dim NumGet As Integer
While True
NumGet = UDP_Listen.Receive(Bytes)
Debug.Print(NumGet)
'文件传输完成标志:"TcFileComplete"
If NumGet = 28 Then
Dim NewBytes(27) As Byte
Array.Copy(Bytes, 0, NewBytes, 0, 28)
If System.Text.Encoding.Unicode.GetString(NewBytes) = "TcFileComplete" Then
Debug.Print("TcFileComplete")
Fs.Close()
MsgBox("文件传输完成!")
End If
Else
Fs.Write(Bytes, 0, NumGet)
End If
End While
Catch ex As Exception
End Try
End Sub
Private Sub btnListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListen.Click
If TxtIP.Text = "" Or txtPort.Text = "" Then Exit Sub
Dim socketSent As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Fs = New FileStream(txtFile.Text, FileMode.Create)
Try
IpEndPoint = New IPEndPoint(Net.IPAddress.Parse(Trim(TxtIP.Text)), CLng(txtPort.Text))
UDP_Listen.Bind(IpEndPoint)
T = New Thread(AddressOf Listen)
T.Start()
MessageBox.Show("已经成功启动监听。")
Me.btnListen.Enabled = False
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
【VB.Net 局域网传输文件.rar】点击下载此文件
[本日志由 tiancao1001 于 2015-06-17 08:57 AM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |