File transfer with socket in. Net

Source: Internet
Author: User
Tags implement socket
Socket is a network socket, using it we can easily realize the network data transmission. Socket classes are provided in. NET to help open the

The sender calls the socket's use, thus avoids the call socket's dynamic library to realize the socket the trouble. Let's look at a simple example from this simple example to see how convenient it is for the socket class in. NET to implement the socket.

Programming Ideas: In this simple example, we will use the socket to achieve the file's network transmission and reception functions. To achieve these two functions, we need to build two instances through vs.net to complete. A sender, a receiving end. Because the text has given a detailed comment here is not wordy.


Sending end


Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub button1_click (ByVal sender as System.Object,
ByVal e as System.EventArgs) Handles Button1.Click
Dim Sendsocket as New Net.Sockets.Socket
(Net.Sockets.AddressFamily.InterNetwork,
Net.Sockets.SocketType.Stream, NET.SOCKETS.PROTOCOLTYPE.TCP)
' Instantiate the socket
Dim Ipendpiont as New net.ipendpoint
(Net.IPAddress.Parse ("192.168.0.1"), 8888) ' Establish an endpoint
' Openfiledialog1.showdialog ()
Dim FS as New IO. FileStream ("C:\p.doc",
Io. FileMode.OpenOrCreate, IO. FileAccess.Read) ' The file to be transferred
Dim fssize (fs. LENGTH-1) as Byte
Dim Strread as New IO. 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) ' Connecting to a remote computer
Sendsocket. Send (Fssize) ' sends a file
Label1.Text = fs. Length ()
Fs. Close ()
Sendsocket. Shutdown (Net.Sockets.SocketShutdown.Send)
' Turn off Send connection
Sendsocket. Close () ' closes the native socket
End Sub
End Class

Receiving end

Public Class Form1
Inherits System.Windows.Forms.Form
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 ("192.168.0.1"), 8888)
Receivesocket. Bind (Hostipendpiont)
' Set up the socket for the remote computer
Receivesocket. Listen (2) ' Listening socket
End Sub

Private Sub button1_click (ByVal sender as Object,
ByVal e as System.EventArgs) Handles Button1.Click
Dim Recfs as New IO. FileStream ("P.doc",
Io. FileMode.OpenOrCreate)
' Receive data and save it to a new file
Dim RecByte (229888) as Byte
Dim Hostsocket as Net.Sockets.Socket =
Receivesocket. Accept ()
' Agree to establish a connection with the sending-end computer
Dim newfilestr as New IO. 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 ()
End Sub
End Class

Through the simple statement above we will complete the network file sending and receiving, but here's one thing to note that this code can only accept text and picture files, sending and receiving other files will cause coding errors, if you don't believe you can transfer a MP3 file, and then open the received MP3 file in the player , you'll hear music that can be played only for less than 2 seconds. If you want to solve this problem you need to use, net TCP/IP class or UDP class to implement, of course, call the underlying socket dynamic library can also solve this problem.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.