C#使用Socket上傳并保存圖片的方法
本文實(shí)例講述了C#使用Socket上傳并保存圖片的方法。分享給大家供大家參考。具分析如下:
使用string filename = openFile.FileName;即返回帶全路徑的文件名 Path.GetFileNameWithoutExtension(filename)即可獲得不帶路徑、后綴名的文件名。 上傳圖片使用二進(jìn)制 tcp協(xié)議上傳的
客戶(hù)端代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; namespace socketClient { public partial class Form1 : Form { Socket clientSocket; private static byte[] result = new byte[1024]; public Form1() { InitializeComponent(); } private void buttonBegin_Click(object sender, EventArgs e) { //設(shè)定服務(wù)器IP地址 IPAddress ip = IPAddress.Parse("127.0.0.1"); clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { clientSocket.Connect(new IPEndPoint(ip, 8000)); //配置服務(wù)器IP與端口 } catch { MessageBox.Show("連接服務(wù)器失敗"); return; } } private void buttonClose_Click(object sender, EventArgs e) { this.Close(); } private void buttonSelect_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "圖像文件(*.bmp;*.gif;*.jpg;*.jpeg;*.png)|*.bmp;*.gif;*.jpg;*.jpeg;*.png"; openFile.Multiselect = false; if (openFile.ShowDialog() == DialogResult.OK) { textBox2.Text =openFile.FileName; } //string filename = openFile.FileName; //即返回帶全路徑的文件名 //Path.GetFileNameWithoutExtension(filename) //即可獲得不帶路徑、后綴名的文件名。 byte[] msg = Encoding.Default.GetBytes(Path.GetFileNameWithoutExtension(openFile.FileName)); clientSocket.Send(msg); try { //開(kāi)始使用socket發(fā)送文件 FileStream fs = new FileStream(openFile.FileName, FileMode.OpenOrCreate, FileAccess.Read); byte[] fssize = new byte[fs.Length]; BinaryReader strread = new BinaryReader(fs); strread.Read(fssize, 0, fssize.Length - 1); clientSocket.Send(fssize); fs.Close(); clientSocket.Shutdown(System.Net.Sockets.SocketShutdown.Send); clientSocket.Close(); } catch (Exception ex) { string s = ex.ToString(); return; } } } }
服務(wù)器端代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; using System.IO; using System.Drawing.Imaging; namespace socketServer { public partial class Form1 : Form { private static byte[] result = new byte[1024]; Socket serverSocket; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { IPAddress ip = IPAddress.Parse("127.0.0.1"); IPEndPoint iep = new IPEndPoint(ip, 8000); serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(iep); serverSocket.Listen(10); byte[] recvBytes = new byte[1024]; int bytes = newSocket.Receive(recvBytes, recvBytes.Length, SocketFlags.None);//從客戶(hù)端接受信息 string name = Encoding.ASCII.GetString(recvBytes, 0, bytes); textBox1.Text = name+".jpg"; //設(shè)置接收數(shù)據(jù)緩沖區(qū)的大小 byte[] b = new byte[1024 * 4]; MemoryStream fs = new MemoryStream(); int got = 0; int datalength = 0; while (true) { got = newSocket.Receive(b); fs.Write(b, 0, got); if (got > 0) datalength = datalength + got; else break; } Bitmap Img = new Bitmap(fs); pictureBox1.Image = Img; string filename = name + ".jpg"; Img.Save(@"D:images"+filename, ImageFormat.Jpeg); //關(guān)閉寫(xiě)文件流 fs.Close(); //關(guān)閉接收數(shù)據(jù)的Socket newSocket.Shutdown(System.Net.Sockets.SocketShutdown.Receive); newSocket.Close(); } catch (Exception se) { serverSocket.Close(); MessageBox.Show("連接錯(cuò)誤" + se.ToString()); return; } } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#獲取客戶(hù)端相關(guān)信息實(shí)例總結(jié)
這篇文章主要介紹了C#獲取客戶(hù)端相關(guān)信息的方法,以實(shí)例形式總結(jié)了C#獲取客戶(hù)端IP地址、網(wǎng)絡(luò)連接、硬件信息等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09C# Pointer指針應(yīng)用實(shí)例簡(jiǎn)述
這篇文章主要介紹了C# Pointer指針應(yīng)用,對(duì)初學(xué)者很有借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-08-08C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06c#基礎(chǔ)之?dāng)?shù)組與接口使用示例(遍歷數(shù)組 二維數(shù)組)
本文主要介紹了c#基礎(chǔ)知識(shí)中的數(shù)組與接口使用方法,結(jié)合示例,大家一看就明白2014-01-01C#實(shí)現(xiàn)強(qiáng)制關(guān)閉當(dāng)前程序進(jìn)程
這篇文章主要介紹了C#實(shí)現(xiàn)強(qiáng)制關(guān)閉當(dāng)前程序進(jìn)程,本文直接給出實(shí)現(xiàn)代碼,可以實(shí)現(xiàn)完全Kill掉不留痕跡,需要的朋友可以參考下2015-06-06c#不使用windows api函數(shù)打開(kāi)我的電腦和獲取電腦驅(qū)動(dòng)器信息
這篇文章主要介紹了c#不使用windows api函數(shù)打開(kāi)我的電腦和電腦驅(qū)動(dòng)器信息的方法,大家參考使用2013-12-12幾分鐘搞懂c#之FileStream對(duì)象讀寫(xiě)大文件(推薦)
這篇文章主要介紹了c#之FileStream對(duì)象讀寫(xiě)大文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C#中LINQ?to?DataSet操作及DataTable與LINQ相互轉(zhuǎn)換
這篇文章介紹了C#中LINQ?to?DataSet操作及DataTable與LINQ相互轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05