C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法
更新時(shí)間:2015年04月02日 12:15:54 作者:令狐不聰
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法,涉及C#操作socket發(fā)送與接收文件的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了C#使用Socket實(shí)現(xiàn)發(fā)送和接收?qǐng)D片的方法。分享給大家供大家參考。具體如下:
using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; using System.IO; namespace ConsoleApplication1 { Class Program { static void Main (String[] args) { // 1. to create a socket Socket sListen = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 2. Fill IP IPAddress IP = IPAddress.Parse ("127.0.0.1"); IPEndPoint IPE = new IPEndPoint (IP, 4321); // 3. binding sListen.Bind (IPE); // 4. Monitor Console.WriteLine ("Service is listening ..."); sListen.Listen (2); // 5. loop to accept client connection requests while (true) { Socket clientSocket; try { clientSocket = sListen.Accept (); } catch { throw; } // send data to the client //clientSocket.Send (Encoding.Unicode.GetBytes ("I am a server, you there?? !!!!")); // send the file byte[] buffer = ReadImageFile ("1.jpg"); clientSocket.Send (buffer, buffer.Length, SocketFlags.None); Console.WriteLine ("Send success!"); } } private static byte[] ReadImageFile (String img) { FileInfo fileinfo = new FileInfo (img); byte[] buf = new byte[fileInfo.Length]; FileStream fs = new FileStream (img, FileMode.Open, FileAccess.Read); fs.Read (buf, 0, buf.Length); fs.Close (); //fileInfo.Delete (); GC.ReRegisterForFinalize (fileinfo); GC.ReRegisterForFinalize (fs); return buf; } } }
客戶端接收和保存圖片的代碼如下:
using System; using System.Collections.Generic; using System.Text; using System.Net.Sockets; using System.Net; using System.IO; namespace ConsoleApplication2 { Class Program { static void Main (String[] args) { // 1. to create a socket Socket S = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 2. fill in the remote IP IPAddress IP = IPAddress.Parse ("127.0.0.1"); IPEndPoint IPE = new IPEndPoint (IP, 4321); Console.WriteLine ("started connection service ...."); // 3. connect to the server s.Connect (IPE); // 4. receive data byte[] buffer = new byte[1000000]; s.Receive (buffer, buffer.Length, SocketFlags.None); //var Msg = Encoding.Unicode.GetString (buffer); //Console.WriteLine ("received message: (0)", msg); Console.WriteLine ("Receive success"); FileStream fs = File.Create ("1.jpg"); fs.Write (buffer, 0, buffer.Length); fs.Close (); Console.ReadKey (); } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- C#使用Socket發(fā)送和接收TCP數(shù)據(jù)實(shí)例
- c#(Socket)異步套接字代碼示例
- C#實(shí)現(xiàn)的Socket服務(wù)器端、客戶端代碼分享
- C#中Socket通信用法實(shí)例詳解
- c#(Socket)同步套接字代碼示例
- C#開(kāi)發(fā)之Socket網(wǎng)絡(luò)編程TCP/IP層次模型、端口及報(bào)文等探討
- C#中異步Socket通信編程代碼實(shí)例
- C#實(shí)現(xiàn)Socket通信的解決方法
- C# Socket網(wǎng)絡(luò)編程實(shí)例
- C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用
相關(guān)文章
Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07C#使用Socket實(shí)現(xiàn)本地多人聊天室
這篇文章主要為大家詳細(xì)介紹了C#使用Socket實(shí)現(xiàn)本地多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤序列號(hào)
這篇文章主要介紹了C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤序列號(hào)的方法及代碼分享,需要的朋友可以參考下2015-03-03