Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use socket to transfer VB.NET files

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use socket to complete VB.NET file transfer", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "how to use socket to complete VB.NET file transfer"!

VB.NET file transfer code:

Sender PublicClassForm1 InheritsSystem.Windows.Forms.Form PrivateSubButton1_Click (ByValsenderAsSystem.Object, ByValeAsSystem.EventArgs) HandlesButton1.Click DimsendsocketAsNewNet.Sockets.Socket (Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream,Net.Sockets.ProtocolType.Tcp)''instantiate socket DimipendpiontAsNewNet.IPEndPoint (Net.IPAddress.Parse ("192.168.0.1"), 8888)' establish Endpoint''OpenFileDialog1.ShowDialog () DimfsAsNewIO.FileStream ("c:p.doc", IO.FileMode.OpenOrCreate IO.FileAccess.Read)''Dimfssize (fs.Length-1) AsByte DimstrreadAsNewIO.BinaryReader (fs)' 'stream processes files to be transferred' 'fs.Read (fssize,0,fssize.Length-1) strread.Read (fssize,0) Fssize.Length-1) sendsocket.Connect (ipendpiont)''connect to remote computer sendsocket.Send (fssize)' 'send file Label1.Text=fs.Length () fs.Close () sendsocket.Shutdown (Net.Sockets.SocketShutdown.Send)' 'close send connection sendsocket.Close ()' 'close native socket EndSub EndClass receiver PublicClassForm1 InheritsSystem.Windows.Forms.Form DimreceivesocketAsNewNet.Sockets.Socket (Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream) Net.Sockets.ProtocolType.Tcp) PrivateSubForm1_Load (ByValsenderAsSystem.Object, ByValeAsSystem.EventArgs) HandlesMyBase.Load DimhostipendpiontAsNewNet.IPEndPoint (Net.IPAddress.Parse ("192.168.0.1"), 8888) receivesocket.Bind (hostipendpiont)''build socket receivesocket.Listen (2)' of remote computer''listening on socket EndSub PrivateSubButton1_Click (ByValsenderAsObject, ByValeAsSystem.EventArgs) HandlesButton1.Click DimrecfsAsNewIO.FileStream ("p.doc") IO.FileMode.OpenOrCreate)''receive the data and save it to a new file Dimrecbyte (229888) AsByte DimhostsocketAsNet.Sockets.Socket= receivesocket.Accept ()' 'agree to establish a connection with the sending computer DimnewfilestrAsNewIO.BinaryWriter (recfs)' 'stream write hostsocket.Receive (recbyte)' 'recfs.Write (recbyte,0,recbyte.Length-1) newfilestr.Write (recbyte,0) Recbyte.Length-1) recfs.Close () hostsocket.Shutdown (Net.Sockets.SocketShutdown.Receive) hostsocket.Close () EndSub EndClass

Through the above simple statement we complete the VB.NET file transfer, but there is one thing to note here, this code can only accept text files and picture files, for other files sent and received will lead to coding errors, if you do not believe you can transfer a mp3 file, and then open the received mp3 file in the player, you will hear only the beginning of less than 2 seconds of music can be played. If you want to solve this problem, you need to use the tcp/ip class or udp class in net. Of course, calling the underlying socket dynamic library can also solve this problem.

At this point, I believe you have a deeper understanding of "how to use socket to complete VB.NET file transfer". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report