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

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 <= a / 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("读写完毕");
        }
    }
}

 

转载于:https://www.cnblogs.com/wanggonglei/p/7865881.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值