最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# TcpClient網(wǎng)絡編程傳輸文件的示例

 更新時間:2021年04月19日 09:00:08   作者:空白凌亂感  
這篇文章主要介紹了C# TcpClient網(wǎng)絡編程傳輸文件的示例,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下

一、簡述

  利用C# TcpClient在局域網(wǎng)內(nèi)傳輸文件,可是文件發(fā)送到對面的時候卻要重新命名文件的。那可不可以連著文件名與文件一起發(fā)過去呢?

二、內(nèi)容

  如上圖,把文件名字符流的長度的值的字符流(這個文件名字符流長度的值固定是11位的字符串,不足11位前面補0)與文件名的字符流合為一個byte數(shù)組然后與文件發(fā)送到對面。對面接收后解析出文件名字符流長度的值后,再根據(jù)長度解析出文件名,接下來再獲取文件流。

  服務端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TCPSendFile
{
    public partial class Form1 : Form
    {
        public delegate void TxtReceiveAddContentEventHandler(string txtValue);
        
        public Form1()
        {
            InitializeComponent();
        }
       
        public void TxtAddContent(string txtValue)
        {
            if (textBox1.InvokeRequired)
            {
                TxtReceiveAddContentEventHandler addContent = TxtAddContent;
                textBox1.Invoke(addContent, new object[] { txtValue });
            }
            else
            {
                textBox1.Text = txtValue + "\r\n" + textBox1.Text;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            TcpListener tcpListener = new TcpListener(IPAddress.Any, 18001);
            tcpListener.Start();
            textBox1.Text = "開始偵聽...";
            Thread thread = new Thread(SendFileFunc);
            thread.Start(tcpListener);
            thread.IsBackground = true;
        }

        public void SendFileFunc(object obj)
        {
            TcpListener tcpListener = obj as TcpListener;
            while (true)
            {
                try
                {
                    TcpClient tcpClient = tcpListener.AcceptTcpClient();
                    if (tcpClient.Connected)
                    {
                        NetworkStream stream = tcpClient.GetStream();
                        string fileName = "testfile.rar";

                        byte[] fileNameByte = Encoding.Unicode.GetBytes(fileName);

                        byte[] fileNameLengthForValueByte = Encoding.Unicode.GetBytes(fileNameByte.Length.ToString("D11"));
                        byte[] fileAttributeByte = new byte[fileNameByte.Length + fileNameLengthForValueByte.Length];
                       
                        fileNameLengthForValueByte.CopyTo(fileAttributeByte, 0);  //文件名字符流的長度的字符流排在前面。
                      
                        fileNameByte.CopyTo(fileAttributeByte, fileNameLengthForValueByte.Length);  //緊接著文件名的字符流
                     
                        stream.Write(fileAttributeByte, 0, fileAttributeByte.Length);
                        FileStream fileStrem = new FileStream(Application.StartupPath + "\\WebFile\\" + fileName, FileMode.Open);

                        int fileReadSize = 0;
                        long fileLength = 0;
                        while (fileLength < fileStrem.Length)
                        {
                            byte[] buffer = new byte[2048];
                            fileReadSize = fileStrem.Read(buffer, 0, buffer.Length);
                            stream.Write(buffer, 0, fileReadSize);
                            fileLength += fileReadSize;

                        }
                        fileStrem.Flush();
                        stream.Flush();
                        fileStrem.Close();
                        stream.Close();

                        TxtAddContent(string.Format("{0}文件發(fā)送成功", fileName));
                    }

                   
                }
                catch (Exception ex)
                {
                    
                }
            }
        }
    }
}

  客戶端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TCPReceiveFile
{
    public partial class Form1 : Form
    {
        public delegate void TxtReceiveAddContentEventHandler(string txtValue);
        public Form1()
        {
            InitializeComponent();
        }
        public void TxtReceiveAddContent(string txtValue)
        {
            if (txtReceive.InvokeRequired)
            {
                TxtReceiveAddContentEventHandler addContent = TxtReceiveAddContent;
                txtReceive.Invoke(addContent, new object[] { txtValue });
            }
            else
            {
                txtReceive.Text = txtValue + "\r\n" + txtReceive.Text;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.101"), 18001);
            TxtReceiveAddContent("連接中。。。。。");
            Thread th = new Thread(ReceiveFileFunc);
            th.Start(ipEndPoint);
            th.IsBackground = true;
        }

        private void ReceiveFileFunc(object obj)
        {
            IPEndPoint ipEndPoint = obj as IPEndPoint;
            TcpClient tcpClient = new TcpClient();
            try
            {
                tcpClient.Connect(ipEndPoint);
            }
            catch
            {
                TxtReceiveAddContent("連接失敗,找不到服務器!");
            }

            if (tcpClient.Connected)
            {
               
                NetworkStream stream = tcpClient.GetStream();
                if (stream != null)
                {

                    byte[] fileNameLengthForValueByte = Encoding.Unicode.GetBytes((256).ToString("D11"));
                    byte[] fileNameLengByte = new byte[1024];
                    int fileNameLengthSize = stream.Read(fileNameLengByte, 0, fileNameLengthForValueByte.Length);
                    string fileNameLength = Encoding.Unicode.GetString(fileNameLengByte, 0, fileNameLengthSize);
                    TxtReceiveAddContent("文件名字符流的長度為:" + fileNameLength);

                    int fileNameLengthNum = Convert.ToInt32(fileNameLength);
                    byte[] fileNameByte = new byte[fileNameLengthNum];

                    int fileNameSize = stream.Read(fileNameByte, 0, fileNameLengthNum);
                    string fileName = Encoding.Unicode.GetString(fileNameByte, 0, fileNameSize);
                    TxtReceiveAddContent("文件名為:" + fileName);

                    string dirPath = Application.StartupPath + "\\WebFile";
                    if(!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }
                    FileStream fileStream = new FileStream(dirPath + "\\" + fileName, FileMode.Create, FileAccess.Write);
                    int fileReadSize = 0;
                    byte[] buffer = new byte[2048];
                    while ((fileReadSize = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fileStream.Write(buffer, 0, fileReadSize);

                    }
                    fileStream.Flush();
                    fileStream.Close();
                    stream.Flush();
                    stream.Close();
                    tcpClient.Close();
                    TxtReceiveAddContent("接收成功");
                }
            }
        }
      
    }
}

  實例圖

以上就是C# TcpClient網(wǎng)絡編程傳輸文件的示例的詳細內(nèi)容,更多關(guān)于C# TcpClient傳輸文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C# MVC 使用LayUI實現(xiàn)下拉框二級聯(lián)動的功能

    C# MVC 使用LayUI實現(xiàn)下拉框二級聯(lián)動的功能

    這篇文章主要介紹了C# MVC 如何使用LayUI實現(xiàn)下拉框二級聯(lián)動,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下
    2020-06-06
  • c#橋接模式(bridge結(jié)構(gòu)模式)用法實例

    c#橋接模式(bridge結(jié)構(gòu)模式)用法實例

    這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細的分析了橋接模式的原理與用法實例,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)示例詳解

    C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)示例詳解

    這篇文章主要給大家介紹了關(guān)于C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-05-05
  • c# WPF設置軟件界面背景為MediaElement并播放視頻

    c# WPF設置軟件界面背景為MediaElement并播放視頻

    這篇文章主要介紹了c# WPF如何設置軟件界面背景為MediaElement并播放視頻,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-03-03
  • 解析.NET中幾種Timer的使用

    解析.NET中幾種Timer的使用

    本文主要對.NET中4個Timer類,及其用法進行梳理,具有很好參考價值,需要的朋友一起來看下吧
    2016-12-12
  • C#修改IIS站點framework版本號的方法

    C#修改IIS站點framework版本號的方法

    這篇文章主要介紹了C#修改IIS站點framework版本號的方法,涉及C#調(diào)用使用ASP.NET IIS注冊工具Aspnet_regiis.exe進行IIS站點framework版本號修改的方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • c# 并行的實現(xiàn)示例

    c# 并行的實現(xiàn)示例

    本文主要介紹了c# 并行的實現(xiàn)示例,我們使用?Parallel.ForEach?方法并結(jié)合?File.ReadAllLines?來提高讀取速度,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • C#?Web實現(xiàn)文件上傳的示例詳解

    C#?Web實現(xiàn)文件上傳的示例詳解

    這篇文章主要為大家詳細介紹了C#?Web實現(xiàn)文件上傳的相關(guān)知識,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • C#刪除字符串中重復字符的方法

    C#刪除字符串中重復字符的方法

    這篇文章主要介紹了C#刪除字符串中重復字符的方法,涉及C#針對字符串的遍歷及移除等操作的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-02-02
  • C#深拷貝方法探究及性能比較(多種深拷貝)

    C#深拷貝方法探究及性能比較(多種深拷貝)

    這篇文章主要介紹了C#中使用NetCDF存儲二維數(shù)據(jù)的讀寫操作簡單應用,探究了以下幾種C#對象深拷貝方式,同時簡單對比了以下列出的幾種深拷貝方式的速度,需要的朋友可以參考下
    2022-04-04

最新評論

烟台市| 琼海市| 邵东县| 庄浪县| 五河县| 衡山县| 施甸县| 石渠县| 桑日县| 河源市| 方城县| 赣州市| 巩义市| 黄大仙区| 五寨县| 吉木萨尔县| 浪卡子县| 从化市| 朝阳市| 保山市| 原阳县| 邢台县| 平果县| 新闻| 池州市| 双江| 利津县| 留坝县| 宜兴市| 柳州市| 东乡族自治县| 上林县| 绥芬河市| 乐至县| 广汉市| 阿拉尔市| 旬邑县| 旬邑县| 泽普县| 陇川县| 昭平县|