Use the socket in .NET to implement file transfer ICKNAY [original]

xiaoxiao2021-03-06  162

Socket is a network socket, using it we can easily implement network data transfer. Provide a socket class in .NET to help open

The sender calls the use of sockets to avoid calling the Socket's dynamic library to implement the trouble of Socket. Let's take a look at a simple example, from this simple example to see how convenient Socket in .NET. Programming: In this simple example we will implement file network transfer and reception functions through Socket. To implement these two features, we need to establish two instances through vs.net to complete. A sender, one receiving end. Due to the detailed comments in the text, it is not coming. Send 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 Socket ?????? Dim IpendPIONT AS NEW NET. IpendPoint (Net.ipaddress.Parse ("192.168.0.1"), 8888) 'Established endpoint ???????' OpenFileDialog1.showdialog () ??????? DIM FS AS New IO.FILESTREAM (" C: /P.doc ", IO.filemode.openorcreate, IO.FileAccess.read) 'To transfer files ??????? Dim fssize (fs.length - 1) as Byte ??????? DIM STREAD AS New I.BINARYREADER (FS) 'stream processing files to be transferred ???????' fs.read (fssize, 0, fssize.length - 1) ?????? Strread.read ( Fssize, 0, fssize.length - 1) ?????? sendsocket.connect (ipendpiont) "Connect the 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 this machine socket ??? End Subend 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 . Lendpoint (Net.Ipaddress.Parse ("192.168.0.1"), 8888) ??????? ReceiveSocket.bind (HostilendPiont) 'Establishing the Socket "of the remote computer ?????? ReceiveSocket.Listen (2 ) 'Monitor Socket ??? End Sub ??? private sub button1_click (Byval E AS Object, ??? byval e as system.eventargs) handles button1.click ??????? Dim Recfs as new omewo.filestream "p.doc", IO.FILEMODE.Openorcreate "receives the data and saves it to a new file ??????? Dim Recbyte (229888) AS BYTE ??????? Dim Hostsocket As Net .Sockets.socket = receiveSocket.accept () 'agreed to and send a computer to establish a connection ??????? Dim newFileStr As new ooarywriter (RECFS)' Flow ?????? 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 Subend Class We complete the send and receive of the network file through the simple statement above, but here should pay attention to this code can only accept text. Files and image files, sending and receiving other files will result in encoding errors, if you don't believe you can transfer a MP3 file, open the received MP3 file in the player, you will hear only 2 seconds Music can be played. If you want to solve this problem, you need to use the TCP / IP class or UDP class in NET to implement, of course, the Socket dynamic library that calls the underlying can also solve this problem.

转载请注明原文地址:https://www.9cbs.com/read-128364.html

New Post(0)