用.net中的socket完成文件傳輸
發(fā)表時(shí)間:2024-06-18 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]socket是網(wǎng)絡(luò)套接字,使用它我們可以很方便的實(shí)現(xiàn)網(wǎng)絡(luò)的數(shù)據(jù)傳輸。在.net中提供了socket類(lèi)來(lái)幫助開(kāi)發(fā)者調(diào)用socket的使用,從而避免了調(diào)用socket的動(dòng)態(tài)庫(kù)來(lái)實(shí)現(xiàn)socket的麻煩。下面我們就來(lái)看一個(gè)簡(jiǎn)單的例子,從這個(gè)簡(jiǎn)單的例子來(lái)看看.net中的socket類(lèi)實(shí)現(xiàn)socket是多么的...
socket是網(wǎng)絡(luò)套接字,使用它我們可以很方便的實(shí)現(xiàn)網(wǎng)絡(luò)的數(shù)據(jù)傳輸。在.net中提供了socket類(lèi)來(lái)幫助開(kāi)
發(fā)者調(diào)用socket的使用,從而避免了調(diào)用socket的動(dòng)態(tài)庫(kù)來(lái)實(shí)現(xiàn)socket的麻煩。下面我們就來(lái)看一個(gè)簡(jiǎn)單的例子,從這個(gè)簡(jiǎn)單的例子來(lái)看看.net中的socket類(lèi)實(shí)現(xiàn)socket是多么的方便。
編程思路:在這個(gè)簡(jiǎn)單的例子中我們將通過(guò)socket來(lái)實(shí)現(xiàn)文件的網(wǎng)絡(luò)傳輸和接收功能。要實(shí)現(xiàn)這兩個(gè)功能我們需要通過(guò)vs.net建立兩個(gè)實(shí)例來(lái)完成。一個(gè)發(fā)送端,一個(gè)接收端。由于文中已經(jīng)給出了詳細(xì)的注釋這里就不羅嗦了。
發(fā)送端
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)
'實(shí)例化socket
Dim ipendpiont As New Net.IPEndPoint
(Net.IPAddress.Parse("192.168.0.1"), 8888)'建立終結(jié)點(diǎn)
'OpenFileDialog1.ShowDialog()
Dim fs As New IO.FileStream("c:\p.doc",
IO.FileMode.OpenOrCreate, IO.FileAccess.Read)'要傳輸?shù)奈募?br> Dim fssize(fs.Length - 1) As Byte
Dim strread As New IO.BinaryReader(fs)'流處理要傳輸?shù)奈募?br> 'fs.Read(fssize, 0, fssize.Length - 1)
strread.Read(fssize, 0, fssize.Length - 1)
sendsocket.Connect(ipendpiont)'連接遠(yuǎn)程計(jì)算機(jī)
sendsocket.Send(fssize)'發(fā)送文件
Label1.Text = fs.Length()
fs.Close()
sendsocket.Shutdown(Net.Sockets.SocketShutdown.Send)
'關(guān)閉發(fā)送連接
sendsocket.Close()'關(guān)閉本機(jī)socket
End Sub
End Class
接收端
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)
'建立遠(yuǎn)程計(jì)算機(jī)的的socket
receivesocket.Listen(2)'監(jiān)聽(tīng)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)
'接收數(shù)據(jù)并將其保存到一個(gè)新的文件中
Dim recbyte(229888) As Byte
Dim hostsocket As Net.Sockets.Socket =
receivesocket.Accept()
'同意和發(fā)送端計(jì)算機(jī)建立連接
Dim newfilestr As New IO.BinaryWriter(recfs)'流寫(xiě)
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
通過(guò)上面的簡(jiǎn)單語(yǔ)句我們就完成網(wǎng)絡(luò)文件的發(fā)送和接收,但是這里有一點(diǎn)要注意,此代碼只可以接受文本文件和圖片文件,對(duì)于其他文件的發(fā)送和接收將導(dǎo)致編碼錯(cuò)誤,如果你不相信你可以傳輸一個(gè)mp3文件,再在播放器中打開(kāi)接收到的mp3文件,你會(huì)聽(tīng)到只有開(kāi)頭不到2秒的音樂(lè)可以播放。如果想解決這個(gè)問(wèn)題你需要使用,net中的tcp/ip類(lèi)或udp類(lèi)來(lái)實(shí)現(xiàn),當(dāng)然調(diào)用底層的socket動(dòng)態(tài)庫(kù)也可以解決這