最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
用socket实现简单的文件传输
时间:2022-07-02 12:08:07 编辑:袖梨 来源:一聚教程网
'发送端:
'By wgscd
Dim sendsocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
'实例化socket
Dim ipendpiont As New Net.IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 8888) '建立终结点
'OpenFileDialog1.ShowDialog()
Dim fs As New IO.FileStream("c:wgscd.dat", IO.FileMode.OpenOrCreate, IO.FileAccess.Read) '要传输的文件
Dim fssize(fs.Length - 1) As Byte
Dim strread As New IO.BinaryReader(fs) '流处理要传输的文件
'fs.Read(fssize, 0, fssize.Length - 1)
strread.Read(fssize, 0, fssize.Length - 1)
sendsocket.Connect(ipendpiont) '连接远程计算机
sendsocket.Send(fssize) '发送文件
Label1.Text = fs.Length()
fs.Close()
sendsocket.Shutdown(Net.Sockets.SocketShutdown.Send)
'关闭发送连接
sendsocket.Close() '关闭本机socket
=======================================
接收端:
Dim receivesocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp)
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
Dim hostipendpiont As New Net.IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 8888)
receivesocket.Bind(hostipendpiont)
'建立远程计算机的的socket
receivesocket.Listen(2) '监听socket
'By wgscd
Dim sendsocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
'实例化socket
Dim ipendpiont As New Net.IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 8888) '建立终结点
'OpenFileDialog1.ShowDialog()
Dim fs As New IO.FileStream("c:wgscd.dat", IO.FileMode.OpenOrCreate, IO.FileAccess.Read) '要传输的文件
Dim fssize(fs.Length - 1) As Byte
Dim strread As New IO.BinaryReader(fs) '流处理要传输的文件
'fs.Read(fssize, 0, fssize.Length - 1)
strread.Read(fssize, 0, fssize.Length - 1)
sendsocket.Connect(ipendpiont) '连接远程计算机
sendsocket.Send(fssize) '发送文件
Label1.Text = fs.Length()
fs.Close()
sendsocket.Shutdown(Net.Sockets.SocketShutdown.Send)
'关闭发送连接
sendsocket.Close() '关闭本机socket
=======================================
接收端:
Dim receivesocket As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp)
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
Dim hostipendpiont As New Net.IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 8888)
receivesocket.Bind(hostipendpiont)
'建立远程计算机的的socket
receivesocket.Listen(2) '监听socket
相关文章
- 《无限暖暖》天星之羽获得位置介绍 12-20
- 《流放之路2》重铸台解锁方法介绍 12-20
- 《无限暖暖》瞄准那个亮亮的成就怎么做 12-20
- 《无限暖暖》魔气怪终结者完成方法 12-20
- 《无限暖暖》曙光毛团获得位置介绍 12-20
- 《无限暖暖》日光果获得位置介绍 12-20