欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#使用Socket上传并保存图片的方法

程序员文章站 2023-08-22 21:57:45
本文实例讲述了c#使用socket上传并保存图片的方法。分享给大家供大家参考。具分析如下: 使用string filename = openfile.filename;即...

本文实例讲述了c#使用socket上传并保存图片的方法。分享给大家供大家参考。具分析如下:

使用string filename = openfile.filename;即返回带全路径的文件名 path.getfilenamewithoutextension(filename)即可获得不带路径、后缀名的文件名。 上传图片使用二进制 tcp协议上传的

客户端代码:

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)
  {
   //设定服务器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));
 //配置服务器ip与端口
   }
   catch
   {
    messagebox.show("连接服务器失败");
 
    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
   {
    //开始使用socket发送文件
    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;
   }
  }
 }
}

服务器端代码:

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);//从客户端接受信息 
   string name = encoding.ascii.getstring(recvbytes, 0, bytes);
   textbox1.text = name+".jpg";
    //设置接收数据缓冲区的大小 
    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);
    //关闭写文件流
    fs.close();
    //关闭接收数据的socket 
    newsocket.shutdown(system.net.sockets.socketshutdown.receive);
    newsocket.close();  
   }
   catch (exception se)
   {
    serversocket.close();
    messagebox.show("连接错误" + se.tostring());
    return;
   }
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。