C#利用TCP传送各种文件的脚本 listener&&client

2021-04-27 03:27

阅读:413

标签:length   客户端   nbsp   imp   gen   读取   options   c#   ref   

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UDP
{
    class Program
    {
        static void Main(string[] args)
        {

          

            //服务器端口 用TCP传文件

            TcpListener listen = new TcpListener(6666);
            listen.Start();

            TcpClient client = listen.AcceptTcpClient();
            NetworkStream ns = client.GetStream();
            StreamWriter sw = new StreamWriter(ns);
            BufferedStream bs = new BufferedStream(ns);
         
            bool judge = client.Connected;              
            if (judge)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("客户端链接成功"+"\n"+"开始传输文件请等待...");
            }

            while (client!=null)
            {

                string filepath = @"E:\练习\视频练习\ConsoleApplication10\ConsoleApplication10\bin\Debug\ConsoleApplication10.exe";
                string ext = Path.GetExtension(filepath);               


                FileStream fs = File.OpenRead(filepath);
                
                sw.WriteLine(fs.Length.ToString()+ext);                
                sw.Flush();

                byte[] b = new byte[1024];
                int length = b.Length;
                while ((length = fs.Read(b, 0, length))>0)
                {
                    bs.Write(b,0,length);
                    bs.Flush();
            
                }
                Console.WriteLine("字节流发送完毕");
               
                bs.Close();                   
                break;
            }
           















            //用UDP传文件
            //IPEndPoint myip = new IPEndPoint(IPAddress.Parse("192.168.50.244"), 5000);

            //UdpClient cs = new UdpClient(myip);

            //FileStream fr = File.OpenRead(@"E:\C#小本本.txt");

            //byte[] b = new byte[fr.Length];
            //IPEndPoint aimpoint = new IPEndPoint(IPAddress.Parse("192.168.50.196"), 5000);
            //fr.Read(b, 0, b.Length);
            //cs.Send(b, b.Length, aimpoint);
            //Console.WriteLine("发送完成");

        }
    }
}

 

 

客户端

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            //用TCP协议传文件  (用户端)
            TcpClient tc = new TcpClient("192.168.50.244", 6666); //向服务器发送请求
            NetworkStream ns = tc.GetStream();  //请求成功后获得流

            BufferedStream sr = new BufferedStream(ns);  //开启缓冲流
            StreamReader sread = new StreamReader(ns);  //开启读取服务器流文件

            string str = sread.ReadLine(); //读取服务发送的文件大小
            Console.WriteLine(str);   //打印此文件大小

            //string ext =str.Substring(str.Length-4);
            string[] strs = str.Split(new string[] {"." },StringSplitOptions.RemoveEmptyEntries); //分割文件大小和扩展名
            string fileLength = strs[0]; //文件大小
            string ext = strs[1];  //扩展名

            FileStream fs = File.OpenWrite(@"D:\小软件5."+ext);  //新建文件的地址
           
            
            long a = long.Parse(fileLength);  //将字符串形式的文件大小转换成long型用于判断
            string qqq = a.ToString(); //获得服务器端发送字符串的长度
            int index = 0;
            byte[] b = new byte[1024];   //建立一个接受缓冲字节流的数组
            int length = b.Length;         
            while (index 1024 + qqq.Length - 1)   //判断传入长度(整个文件的长度等于每次传入的次数加上一开始多读取的字符串的长度)  
            {
                if (index >= qqq.Length - 1)  //当长度大于字符串长度是开始读取(目的是为了不让字符串的字节流干扰整个文件的字节流)
                {
                    sr.Read(b, 0, length); //读一次b数组的字节流
                  
                    fs.Write(b, 0, length); //写一次b数组的字节流到文件的地址去
                }
                index++;                          
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("over");









            //用UDP协议传文件
            //IPEndPoint udpPoint = new IPEndPoint(IPAddress.Parse("192.168.50.196"), 5000);
            //UdpClient udp = new UdpClient(udpPoint);
            //IPEndPoint Sendip = new IPEndPoint(IPAddress.Any, 0);
            //Console.WriteLine(11);
            //string str = Encoding.Default.GetString(recvData);
            //FileStream fs = File.OpenWrite(@"D:\图片.txt");
            //Console.WriteLine("!@#$%^&*(");
            //byte[] recvData = udp.Receive(ref Sendip);
            //Console.WriteLine("1234567890-");
            //fs.Write(recvData, 0, recvData.Length);
            //fs.Close();
            //Console.WriteLine("读写完毕");
        }
    }
}

 

C#利用TCP传送各种文件的脚本 listener&&client

标签:length   客户端   nbsp   imp   gen   读取   options   c#   ref   

原文地址:http://www.cnblogs.com/wanggonglei/p/7865881.html


评论


亲,登录后才可以留言!