第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > C# TCP通信发送桌面图片到服务器

C# TCP通信发送桌面图片到服务器

时间:2020-05-19 22:41:16

相关推荐

C#  TCP通信发送桌面图片到服务器

第一次发文,也是啥也不会,初次接触C#,觉得网络通信挺好玩,就在网上找例子看,有很多例子和方法都可以实现传送图片,同步或异步,通过套接字或者简单的客户端等等都可以通过Networkstream流实现数据 文件 图片等的收发,所以小小尝试一下,感谢各位前辈的指导,也感谢无数无私奉献的前辈的例子

/download/qq_42459294/10971751#0-qzone-1-13814-d020d2d2a4e8d1a374a433f596ad1440

服务器代码

class Program{//全局TcpClientstatic TcpClient client;//文件流建立到磁盘上的读写流static FileStream fs = new FileStream("C:\\Users\\HGS\\Desktop\\2.jpg", FileMode.Create);//static MemoryStream fs = new MemoryStream();//bufferstatic byte[] buffer = new byte[1024*1024];//网络流static NetworkStream ns;static void Main(string[] args){Connectclient();}static void Connectclient(){//服务端IpTcpListener listener = new TcpListener(IPAddress.Parse("192.168.0.145"), 50800);//监听对象开始监听listener.Start();while (true){Console.WriteLine("wait connected");client = listener.AcceptTcpClient();Console.WriteLine("conected");//获取客户端网络流ns = client.GetStream();int readresult = -1;Thread.Sleep(1000);//如果网络流中有数据while (ns.DataAvailable){Thread.Sleep(10);//异步读取网络流中的byte信息ns.BeginRead(buffer, 0, buffer.Length, ReadAsyncCallBack, null);//readresult += ns.Read(buffer, 0, buffer.Length);}//if (readresult < 1)//{// client.Close();// ns.Dispose();// fs.Dispose();// return;//}//fs.Write(buffer, 0, readresult);}}/// <summary>/// 异步读取/// </summary>/// <param name="result"></param>static void ReadAsyncCallBack(IAsyncResult iar){int readCount;//获得每次异步读取数量readCount = client.GetStream().EndRead(iar);//如果全部读完退出,垃圾回收if (readCount < 1){client.Close();ns.Dispose();fs.Dispose();return;}//将网络流中的图片数据片段顺序写入本地fs.Write(buffer, 0, readCount);//Console.WriteLine(buffer);//Bitmap Img = new Bitmap(fs);//Img.Save(@"2.png", ImageFormat.Png);//再次异步读取//ns.BeginRead(buffer, 0, buffer.Length, ReadAsyncCallBack, null);}}

客户端代码:首先需要抓取桌面图片,并命名图片存储类型和名字

/// <summary>/// 截取桌面图片/// </summary>/// <returns></returns>public static Bitmap getScreen(){Bitmap bitimage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);//获得位图的长宽Graphics g = Graphics.FromImage(bitimage);// 创建GDI+绘图g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);//从屏幕到绘图画面截取图片g.Dispose();return bitimage;}/// <summary>/// 保存图片/// </summary>/// <param name="b"></param>public static void img2file(Bitmap b){confirmexis(filePath);//判断文件是否存在,不存在则创建string bpath=filenamepaths();//获取图片数量,按照数量命名b.Save(bpath, System.Drawing.Imaging.ImageFormat.Png);}/// <summary>/// 获取图片数量/// </summary>/// <returns></returns>public static string filenamepaths( ){ string[] filenamepath = Directory.GetFiles("C:\\Users\\abc\\Desktop\\imgae");string Numname = "C:\\Users\\HGS\\Desktop\\imgae\\image" + Convert.ToString(filenamepath.Length)+ ".png"; return Numname ;}

客户端发送数据:

if (File.Exists(imgURl)){ //创建一个文件流打开图片FileStream fs = File.Open(imgURl, FileMode.Open);//声明一个byte数组接受图片byte信息byte[] fileBytes = new byte[fs.Length];using (fs){//将图片byte信息读入byte数组中fs.Read(fileBytes, 0, fileBytes.Length);fs.Close();}try{IPAddress address = IPAddress.Parse("192.168.0.145");//创建TcpClient对象实现与服务器的连接TcpClient client = new TcpClient();//连接服务器client.Connect(address, 50800);using (client){//连接完服务器后便在客户端和服务端之间产生一个流的通道NetworkStream ns = client.GetStream();using (ns){//通过此通道将图片数据写入网络流,传向服务器端接收ns.Write(fileBytes, 0, fileBytes.Length);}}}catch { }//找到服务器的IP地址}

END

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。