[分享]更新上一次发的socket传大文件(加了断点续传)

小范f-li.cn 2010-06-01 11:00:04
上一贴:http://topic.csdn.net/u/20100531/17/dbfbbe52-3c44-4105-b13a-b05380f063d2.html

核心代码

//#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.");
}
}
}


下次更新:多线程多通道传输。 :)
...全文
449 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
haiben080808 2011-08-22
  • 打赏
  • 举报
回复
p​u​b​l​i​c s​t​a​t​i​c b​y​t​e[​]​ ​C​o​m​p​r​e​s​s​(b​y​t​e[​]​ ​b​y​t​e​s​)
​ ​ ​ ​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ u​s​i​n​g ​(​M​e​m​o​r​y​S​t​r​e​a​m​ ​m​s​ = n​e​w ​M​e​m​o​r​y​S​t​r​e​a​m​(​)​)
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​{
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​G​Z​i​p​S​t​r​e​a​m​ ​C​o​m​p​r​e​s​s​ = n​e​w ​G​Z​i​p​S​t​r​e​a​m​(​m​s​,​ ​C​o​m​p​r​e​s​s​i​o​n​M​o​d​e​.​C​o​m​p​r​e​s​s​)​;

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​C​o​m​p​r​e​s​s​.​W​r​i​t​e​(​b​y​t​e​s​,​ 0,​ ​b​y​t​e​s​.​L​e​n​g​t​h​)​;

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​C​o​m​p​r​e​s​s​.​C​l​o​s​e​(​)​;

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ r​e​t​u​r​n ​m​s​.​T​o​A​r​r​a​y​(​)​;

​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​}
​ ​ ​ ​ ​ ​ ​ ​}

周药师 2010-06-03
  • 打赏
  • 举报
回复
不如做个资源 让网友去下载直接看结果
huwen7565833 2010-06-03
  • 打赏
  • 举报
回复
楼主好人啊!!!
这段时间正在为大文件的传输速度慢烦劳!!
小范f-li.cn 2010-06-01
  • 打赏
  • 举报
回复
测试代码:
server:
            FileTransmitor.Receive(FileTransmitor.TestIP, @"E:\");//接受放到那个盘里 保存路径=这个参数+发送到文件名。
Console.ReadLine();

client:
            Console.WriteLine("Plz type in a file path.");
string file = Console.ReadLine();
FileTransmitor.Send(FileTransmitor.TestIP, file);
Console.ReadLine();
hyblusea 2010-06-01
  • 打赏
  • 举报
回复
继续期待楼主的佳作
jsonzbc 2010-06-01
  • 打赏
  • 举报
回复
多谢lz分享....学习。。。
无心雨云 2010-06-01
  • 打赏
  • 举报
回复
学习中
sf

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧