小屋創作

日誌2019-12-30 17:38

C# Socket Send File

作者:貓貓風 ฅ●ω●ฅ

.




















此程式為典型的檔案傳輸

由於沒有特別對檔案傳輸與接收端做特別處理

因此只能做容量小的檔案交換

但對一般應用已經很足夠

如果需要大檔案傳輸

可以參考此篇 -> C# Socket大檔案傳輸


以下程式分為兩部分 : 傳輸端 與 接收端

接收端程式需要按 Recieve開始監聽

傳輸端只需Sned就可以將檔案傳輸至接收端


傳輸端


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Net;  
  10. using System.Net.Sockets;  
  11. using System.IO;  
  12.   
  13. namespace file_send_Client  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void btn_send_file_Click(object sender, EventArgs e)  
  23.         {  
  24.             try   
  25.             {   
  26.             Console.WriteLine("That program can transfer small file.File limit 850kb ");   
  27.             IPAddress[] ipAddress = Dns.GetHostAddresses("127.0.0.1");   
  28.             IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], 5656);   
  29.             Socket clientSock = new Socket(AddressFamily.InterNetwork,
  30.             SocketType.Stream, ProtocolType.IP);  
  31.   
  32.             String file_path = System.Windows.Forms.Application.StartupPath +
  33.             "\\storage\\";  
  34.             string file_Name = "test.txt";   
  35.             byte[] fileNameByte = Encoding.ASCII.GetBytes(file_Name);  
  36.   
  37.             byte[] fileData = File.ReadAllBytes(file_path + file_Name);   
  38.             byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];   
  39.             byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);   
  40.        
  41.             fileNameLen.CopyTo(clientData, 0);   
  42.             fileNameByte.CopyTo(clientData, 4);   
  43.             fileData.CopyTo(clientData, 4 + fileNameByte.Length);   
  44.        
  45.             clientSock.Connect(ipEnd);   
  46.             clientSock.Send(clientData);  
  47.             clientSock.Close();   
  48.             Console.ReadLine();   
  49.             }   
  50.             catch (Exception ex)   
  51.             {  
  52.                 MessageBox.Show("File Sending fail." + ex.Message);  
  53.             }   
  54.         }  
  55.     }  
  56. }  


接收端


  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Net;  
  10. using System.Net.Sockets;  
  11. using System.IO;  
  12. using System.Threading;  
  13.   
  14. namespace file_recieve_server  
  15. {  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         public Form1()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.   
  23.         Thread _waiting_file;  
  24.   
  25.         private void btn_recieve_Click(object sender, EventArgs e)  
  26.         {  
  27.             _waiting_file = new Thread(wait_file);  
  28.             _waiting_file.Start();  
  29.         }  
  30.   
  31.         private void wait_file()  
  32.         {  
  33.             try  
  34.             {  
  35.                 IPEndPoint ipEnd = new
  36.                 IPEndPoint(IPAddress.Parse("127.0.0.1"), 5656);  
  37.                 Socket sock = new Socket(AddressFamily.InterNetwork,
  38.                 SocketType.Stream, ProtocolType.IP);  
  39.                 sock.Bind(ipEnd);  
  40.                 sock.Listen(100);  
  41.                 Socket clientSock = sock.Accept();  
  42.   
  43.                 byte[] clientData = new byte[1024 * 5000];  
  44.                 String receivedPath = System.Windows.Forms.Application.StartupPath +
  45.                 "\\storage\\";  
  46.   
  47.                 int receivedBytesLen = clientSock.Receive(clientData);  
  48.                 int fileNameLen = BitConverter.ToInt32(clientData, 0);  
  49.                 string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);  
  50.   
  51.                 BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName,
  52.                 FileMode.Append));  
  53.                 bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 -
  54.                 fileNameLen);  
  55.   
  56.                 bWrite.Close();  
  57.                 clientSock.Close();  
  58.                 sock.Close();  

  59.             }  
  60.             catch (Exception ex)  
  61.             {  
  62.                 MessageBox.Show("File Receiving fail." + ex.Message);  
  63.             }  
  64.   
  65.             _waiting_file.Abort();  
  66.         }  
  67.     }  
  68. }  


測試結果

接收端按下 Recieve,開始監聽連線,等待接收檔案


傳輸端按下Send 開始傳輸檔案


接收端偵測到連線,開始接收檔案


接收檔案完成



傳輸端 原始檔案位置 與資料內容



接收端 接收檔案位置 與 資料內容


可以看出與傳輸的原始資料一致


如需優化可以加上傳輸進度

傳輸端預先將檔案大小傳給接收端

接收端再依照檔案大小顯示傳輸進度條

12

6

LINE 分享

相關創作

碧藍航線 自動戰鬥腳本 v2.2.2 奏響鳶尾之歌

碧藍航線 自動戰鬥腳本 v2.2.1 破曉冰華

碧藍航線 自動戰鬥腳本 v2.2.6杰諾瓦的焰火

留言

開啟 APP

face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】