• 大小: 1.98MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-08-14
  • 语言: C#
  • 标签: C#  断点续传  socket  

资源简介

文章《C# 基于 socket 的断点续传功能的实现》中的示例代码。 具体见blog:http://blog.csdn.net/shi0090/article/details/46854457

资源截图

代码片段和文件信息

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;

namespace tcpclient
{
    class Program
    {
        private static byte[] _buffer = new byte[102400];
        private static int _receiveCount = 0;
        private static string _receiveString = string.Empty;

        static void Main(string[] args)
        {
            try
            {
                IPAddress ip = IPAddress.Parse(“127.0.0.1“);
                IPEndPoint ep = new IPEndPoint(ip 9110);
                using (Socket transferSocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp))
                {
                    transferSocket.Connect(ep);
                    Console.WriteLine(“connect success...“);

                    ReceiveConsole(transferSocket);
                    ReceiveConsole(transferSocket);
                    while (true)
                    {
                        var input = Console.ReadLine();
                        transferSocket.Send(Encoding.UTF8.GetBytes(input));
                        Thread.Sleep(100);
                        if (input == “yes“)
                            break;
                        else if (input == “no“)
                        {
                            ReceiveConsole(transferSocket);
                            return;
                        }
                        else
                            ReceiveConsole(transferSocket);
                    }

                    ReceiveConsole(transferSocket);
                    var downloadPath = string.Empty;
                    while (true)
                    {
                        var input = Console.ReadLine();
                        try
                        {
                            DirectoryInfo di = new DirectoryInfo(input);
                            if (!di.Exists)
                            {
                                Console.WriteLine(“path is invalid please reset it.“);
                                continue;
                            }
                            downloadPath = input;
                            break;
                        }
                        catch
                        {
                            Console.WriteLine(“path is invalid please reset it.“);
                        }
                    }

                    transferSocket.Send(Encoding.UTF8.GetBytes(“yes“));
                    Thread.Sleep(100);                   

                    while (true)
                    {
                        if (ReceiveConsole(transferSocket) == “send argument“)
                            break;
                    }

                    var tempCount = 0;
                    var bpp = new BreakPointPost();
                    while (tempCount < 5)
                    {
                        SetBreakPointPostArg(bpp ReceiveConsole(transferSocket));
      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-07-12 22:10  socket_backpointpost(console)\
     文件        6148  2015-07-12 22:12  socket_backpointpost(console)\.DS_Store
     目录           0  2015-07-12 22:21  __MACOSX\
     目录           0  2015-07-12 22:21  __MACOSX\socket_backpointpost(console)\
     文件         120  2015-07-12 22:12  __MACOSX\socket_backpointpost(console)\._.DS_Store
     文件        1449  2015-07-12 22:08  socket_backpointpost(console)\socket_backpointpost.sln
     目录           0  2015-07-12 22:08  socket_backpointpost(console)\tcpclient\
     文件        7902  2015-07-12 22:12  socket_backpointpost(console)\tcpclient\Program.cs
     目录           0  2015-07-12 22:21  __MACOSX\socket_backpointpost(console)\tcpclient\
     文件         406  2015-07-12 22:12  __MACOSX\socket_backpointpost(console)\tcpclient\._Program.cs
     目录           0  2015-07-12 22:08  socket_backpointpost(console)\tcpclient\Properties\
     文件        1324  2015-07-12 22:08  socket_backpointpost(console)\tcpclient\Properties\AssemblyInfo.cs
     文件        2395  2015-07-12 22:08  socket_backpointpost(console)\tcpclient\tcpclient.csproj
     目录           0  2015-07-12 22:10  socket_backpointpost(console)\tcpserver\
     文件        6148  2015-07-12 22:10  socket_backpointpost(console)\tcpserver\.DS_Store
     目录           0  2015-07-12 22:21  __MACOSX\socket_backpointpost(console)\tcpserver\
     文件         120  2015-07-12 22:10  __MACOSX\socket_backpointpost(console)\tcpserver\._.DS_Store
     文件        8944  2015-07-12 22:08  socket_backpointpost(console)\tcpserver\Program.cs
     目录           0  2015-07-12 22:08  socket_backpointpost(console)\tcpserver\Properties\
     文件        1324  2015-07-12 22:08  socket_backpointpost(console)\tcpserver\Properties\AssemblyInfo.cs
     文件        2461  2015-07-12 22:08  socket_backpointpost(console)\tcpserver\tcpserver.csproj
     文件     2058993  2015-07-12 22:08  socket_backpointpost(console)\tcpserver\鑷敱闂?zip

评论

共有 条评论