aa

//#define Sleep
#undef Sleep
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;

namespace Rocky
{
    public static class FileTransmitor
    {
        private const int BufferSize = 1024;
        public static readonly IPEndPoint TestIP;

        static FileTransmitor()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            TestIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 520);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            StreamWriter writer = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exec.log"), true, Encoding.Default);
            writer.WriteLine(e.ExceptionObject);
            writer.Dispose();
        }

        public static void Send(IPEndPoint ip, string path)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Connect(ip);
            byte[] buffer = new byte[BufferSize];
            using (FileStream reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                long send, length = reader.Length;
                sock.Send(BitConverter.GetBytes(length));
                string fileName = Path.GetFileName(path);
                sock.Send(buffer, 0, Encoding.Default.GetBytes(fileName, 0, fileName.Length, buffer, 0), SocketFlags.None);
                Console.WriteLine("Sending file:" + fileName + ".Plz wait...");
                sock.Receive(buffer);
                reader.Position = send = BitConverter.ToInt64(buffer, 0);
                int read, sent;
                while ((read = reader.Read(buffer, 0, BufferSize)) != 0)
                {
                    sent = 0;
                    while ((sent += sock.Send(buffer, sent, read, SocketFlags.None)) < read)
                    {
                        send += (long)sent;
#if Sleep
                        Thread.Sleep(200);
#endif
                        //Console.WriteLine("Sent " + send + "/" + length + ".");//进度
                    }
                }
                Console.WriteLine("Send finish.");
            }
        }

        public static void Receive(IPEndPoint ip, string path)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            sock.Bind(ip);
            sock.Listen(1);
            Socket client = sock.Accept();
            byte[] buffer = new byte[BufferSize];
            client.Receive(buffer);
            long receive, length = BitConverter.ToInt64(buffer, 0);
            string fileName = Encoding.Default.GetString(buffer, 0, client.Receive(buffer));
            Console.WriteLine("Receiveing file:" + fileName + ".Plz wait...");
            //string filePath = Path.Combine(path, fileName), tempExtension = ".temp", tempPath = filePath + tempExtension;
            FileInfo file = new FileInfo(Path.Combine(path, fileName));
            using (FileStream writer = file.Open(file.Exists ? FileMode.Append : FileMode.CreateNew, FileAccess.Write, FileShare.None))
            {
                receive = writer.Length;
                client.Send(BitConverter.GetBytes(receive));
                int received;
                while (receive < length)
                {
                    received = client.Receive(buffer);
                    if (received == 0)
                    {
                        Console.WriteLine("Send Stop.");
                        return;
                    }
                    writer.Write(buffer, 0, received);
                    writer.Flush();
                    receive += (long)received;
#if Sleep
                    Thread.Sleep(200);
#endif
                    //Console.WriteLine("Received " + receive + "/" + length + ".");//进度
                }
            }
            Console.WriteLine("Receive finish.");
        }
    }
}

http://topic.csdn.net/u/20100601/11/ceb73894-f4f1-41e3-a112-3e2c8d9d8af1.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值