用socket實現簡單的檔案傳輸

2021-04-13 07:09:44 字數 2190 閱讀 3046

'傳送端:

'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

end sub

private sub button1_click(byval sender as object,byval e as system.eventargs) handles button1.click

dim recbyte(229888990) as byte

dim hostsocket as net.sockets.socket = receivesocket.accept()

'同意和傳送端計算機建立連線

hostsocket.receive(recbyte)

if hostsocket.receive(recbyte) > 0.1 then

msgbox("yyyyy" & hostsocket.receive(recbyte))

end if

dim recfs as new io.filestream("c:wgscd2.dat", io.filemode.openorcreate)

'接收資料並將其儲存到乙個新的檔案中

dim newfilestr as new io.binarywriter(recfs) '流寫

'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

用socket實現簡單的檔案傳輸

傳送端 by wgscd dim sendsocket as new net.sockets.socket net.sockets.addressfamily.internetwork,net.sockets.sockettype.stream,net.sockets.protocoltype.tc...

用go實現簡單的socket通訊

服務端 package main import fmt net func process conn net.conn 3.顯示客戶端傳送的內容到伺服器終端 fmt.println string buf n func main defer listen.close 延時關閉 forelse 這裡準備乙...

利用socket傳檔案

1 顯示檔案進度 2 可以隨時終止傳輸過程 傳送資料執行緒 uint senddatathread lpvoid lpparam 接收資料執行緒 uint receivedatathread lpvoid lpparam 傳送資料按鈕訊息響應函式 void ctzg004dlg onbuttonse...