当前位置:首页 » 文件管理 » s上传文件

s上传文件

发布时间: 2024-09-05 04:58:27

⑴ c# c/s结构Socket上传文件的代码

  1. 发送端(client)

  2. private void button2_Click(object sender, EventArgs e)
    {

    this.button2.Enabled = false;
    Thread TempThread = new Thread(new ThreadStart(this.StartSend));
    TempThread.Start();

    }

    private void StartSend()
    {
    //FileInfo EzoneFile = new FileInfo(this.textBox1.Text);

    string path = @"E:old F directoryTangWeikangge ew1.jpg";

    FileInfo EzoneFile = new FileInfo(path);

    FileStream EzoneStream = EzoneFile.OpenRead();

    int PacketSize = 100000;

    int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));

    // this.textBox8.Text = PacketCount.ToString();

    // this.progressBar1.Maximum = PacketCount;

    int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));

    // this.textBox9.Text = LastDataPacket.ToString();
    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("163.180.117.229"), 7000);

    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    client.Connect(ipep);

    // TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(EzoneFile.Name));

    // TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketSize.ToString()));

    // TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(PacketCount.ToString()));

    // TransferFiles.SendVarData(client, System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));

    byte[] data = new byte[PacketSize];

    for(int i=0; i<PacketCount; i++)
    {
    EzoneStream.Read(data, 0, data.Length);

    TransferFiles.SendVarData(client, data);

    // this.textBox10.Text = ((int)(i + 1)).ToString();

    // this.progressBar1.PerformStep();
    }

    if(LastDataPacket != 0)
    {
    data = new byte[LastDataPacket];

    EzoneStream.Read(data, 0, data.Length);

    TransferFiles.SendVarData(client,data);

    // this.progressBar1.Value = this.progressBar1.Maximum;
    }
    client.Close();

    EzoneStream.Close();

    this.button2.Enabled = true;
    }

  3. 接收端(server)

  4. private void button2_Click(object sender, EventArgs e)
    {
    //int i = 0;
    //FileStream recfs = new FileStream("E:\kangge.jpg", FileMode.OpenOrCreate);
    //Byte[] recbyte = new Byte[2000000];
    //Socket hostsocket = receive.Accept();
    //BinaryWriter newfilestr = new BinaryWriter(recfs);
    //hostsocket.Receive(recbyte, recbyte.Length, SocketFlags.None);
    //for (i = 0; i < recbyte.Length; i++)
    //{
    // newfilestr.Write(recbyte, i, 1);
    //}
    //recfs.Close();

    //hostsocket.Shutdown(SocketShutdown.Receive);
    //hostsocket.Close();

    this.button2.Enabled = false;
    Thread TempThread = new Thread(new ThreadStart(this.StartReceive));
    TempThread.Start();

    }
    private void StartReceive()
    {
    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("163.180.117.229"), 7000);

    Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    server.Bind(ipep);

    server.Listen(10);

    Socket client = server.Accept();

    // IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;

    // string SendFileName = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

    // string BagSize = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));

    // int bagCount = int.Parse(System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client)));

    // string bagLast = System.Text.Encoding.Unicode.GetString(TransferFiles.ReceiveVarData(client));
    int file_name = 1;

    string fileaddr = "E:\old F directory\TangWei\Copy of kangge\" + file_name.ToString() + ".jpg";

    FileStream MyFileStream = new FileStream(fileaddr, FileMode.Create, FileAccess.Write);

    // int SendedCount = 0;

    while(true)
    {
    byte[] data = TransferFiles.ReceiveVarData(client);
    if(data.Length == 0)
    {
    break;
    }
    else
    {
    // SendedCount++;
    MyFileStream.Write(data, 0, data.Length);
    }
    }

    MyFileStream.Close();

    client.Close();

    this.button2.Enabled = true;
    }

  5. 公共类。 TransferFiles

  6. class TransferFiles
    {

    public TransferFiles()
    {

    }

    public static int SendVarData(Socket s, byte[] data) // return integer indicate how many data sent.
    {
    int total = 0;
    int size = data.Length;
    int dataleft = size;
    int sent;
    byte[] datasize = new byte[4];
    datasize = BitConverter.GetBytes(size);
    sent = s.Send(datasize);//send the size of data array.

    while (total < size)
    {
    sent = s.Send(data, total, dataleft, SocketFlags.None);
    total += sent;
    dataleft -= sent;
    }

    return total;
    }

    public static byte[] ReceiveVarData(Socket s) // return array that store the received data.
    {
    int total = 0;
    int recv;
    byte[] datasize = new byte[4];
    recv = s.Receive(datasize, 0, 4, SocketFlags.None);//receive the size of data array for initialize a array.
    int size = BitConverter.ToInt32(datasize, 0);
    int dataleft = size;
    byte[] data = new byte[size];

    while (total < size)
    {
    recv = s.Receive(data, total, dataleft, SocketFlags.None);
    if (recv == 0)
    {
    data = null;
    break;
    }
    total += recv;
    dataleft -= recv;
    }

    return data;

    }
    }

热点内容
如何取消子账号密码 发布:2025-01-13 09:22:41 浏览:346
抖音搜索有缓存 发布:2025-01-13 09:17:28 浏览:589
c语言字符数组连接 发布:2025-01-13 08:55:11 浏览:900
国二c语言编程题目 发布:2025-01-13 08:45:41 浏览:285
ipad软件如何加密 发布:2025-01-13 08:28:59 浏览:278
android的文件操作 发布:2025-01-13 08:24:48 浏览:173
电脑上服务器答题赚钱 发布:2025-01-13 08:24:05 浏览:430
有哪些学习编程的网站 发布:2025-01-13 08:23:17 浏览:529
ymodem源码 发布:2025-01-13 08:20:57 浏览:882
安卓桢布局怎么做 发布:2025-01-13 08:20:55 浏览:968