socket通訊檔案傳輸

2021-06-10 05:14:48 字數 1614 閱讀 4850

主機傳送方

fileinfo ezonefile = new fileinfo("檔案路徑");

//開啟檔案流  

filestream ezonestream = ezonefile.openread();

//包的大小  

int packetsize = 1024;

//包的數量  

int packetcount = (int)(ezonestream.length / ((long)packetsize));

//最後乙個包的大小  

int lastdatapacket = (int)(ezonestream.length - ((long)(packetsize * packetcount)));

//傳送[檔名]到客戶端  

transferfiles.sendvardata(csocket, system.text.encoding.unicode.getbytes(ezonefile.name));

//傳送[包的大小]到客戶端  

transferfiles.sendvardata(csocket, system.text.encoding.unicode.getbytes(packetsize.tostring()));

//傳送[包的總數量]到客戶端  

transferfiles.sendvardata(csocket, system.text.encoding.unicode.getbytes(packetcount.tostring()));

//傳送[最後乙個包的大小]到客戶端  

transferfiles.sendvardata(csocket, system.text.encoding.unicode.getbytes(lastdatapacket.tostring()));

//資料報  

byte data = new byte[packetsize];

//開始迴圈傳送資料報  

for (int i = 0; i < packetcount; i++)

//如果還有多餘的資料報,則應該傳送完畢!  

if (lastdatapacket != 0)

備機接收方

byte filename = transferfiles.receivevardata(s);

string sendfilename = system.text.encoding.unicode.getstring(filename);

"檔名" + sendfilename);   

//獲得[包的大小]   

string bagsize = system.text.encoding.unicode.getstring(transferfiles.receivevardata(s));

"包大小" + bagsize);   

//獲得[包的總數量]   

int bagcount = int.parse(system.text.encoding.unicode.getstring(transferfiles.receivevardata(s)));

"包的總數量" + bagcount);   

時間倉促,寫的有點急,以後在修改請多多見諒

QT模組學習 TCP通訊(檔案傳輸)

檔案傳輸其實和前邊的差不多,都是伺服器監聽客戶端的連線請求,客戶端連線後向其傳送檔案,為了防止檔案傳輸有問題,先傳送檔名稱和檔案大小,隨後傳送檔案內容,因此客戶端接受檔名稱,檔案大小,隨後接收檔案內容。ui 介面 檔案資訊 qmessagebox information this 檔案傳輸情況 檔案...

socket檔案傳輸

伺服器 本檔案是伺服器的 include for sockaddr in include for socket include for socket include for printf include for exit include for bzero include for time t an...

socket檔案傳輸

最近入職培訓中需要寫乙個linux下的c s網路檔案傳輸工具,在實現的過程中,遇到了一些坑,在這裡 做個總結 由於udp伺服器不需要accept 因此也沒有link id 連線的客戶端id 在伺服器檔案接收或下發檔案時需要知道當下與之通訊的客戶端的識別符號,由於udp是無連線的,並且sockfd s...