1. 程式人生 > >C#利用TCP傳送各種文件的腳本 listener&&client

C#利用TCP傳送各種文件的腳本 listener&&client

res flush div amp write over 圖片 sta get

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("讀寫完畢");
        }
    }
}

C#利用TCP傳送各種文件的腳本 listener&&client