使用socket(AsyncSocket、GCDAsyncSocket)进行文件的断点上传操作

https://github.com/potato512/SYDemo_SYSocket

通常使用网络进行文件的断点续传时,都是文件的断点下载,很少有文件的断点上传的例子。现在通过socket方式做了一个文件的断点上传。socket使用了AsyncSocket,或是GCDAsyncSocket。

AsyncSocket:http://code.google.com/p/cocoaasyncsocket/

GCDAsyncSocket:https://github.com/robbiehanson/CocoaAsyncSocket

在使用时,先将socket进行了封装。然后再使用封装类。

封装类文件

// .h文件
//
//  SocketManager.h
//  zhangshaoyu
//
//  Created by zhangshaoyu on 15/9/22.
//  Copyright (c) 2015年 zhangshaoyu. All rights reserved.
//  socket断点上传文件

#import <Foundation/Foundation.h>

/// 掉线类型(服务端掉线,或用户主动退出)
typedef NS_ENUM(NSInteger, SocketDisconnectType)
{
    /// 服务器掉线,默认为0
    SocketOfflineByServer = 0,
    
    /// 用户主动cut
    SocketOfflineByUser = 1
};

@interface SocketManager : NSObject

/// 掉线类型
@property (nonatomic, assign) SocketDisconnectType disconnecType;

/// 文件路径
@property (nonatomic, copy) NSString *filePath;

/// 单例
+ (SocketManager *)sharedSocket;

/// socket连接
- (void)socketConnectWithHost:(NSString *)host port:(UInt16)port;

/// socket连接断开
- (void)socketDisconnect;

/// GCDSocket连接
- (void)GCDSocketConnectWithHost:(NSString *)host port:(UInt16)port;;

/// GCDSocket连接断开
- (void)GCDSocketDisconnect;

@end
// .m文件
//
//  SocketManager.m
//  zhangshaoyu
//
//  Created by zhangshaoyu on 15/9/22.
//  Copyright (c) 2015年 zhangshaoyu. All rights reserved.
//

#import "SocketManager.h"
#import "AsyncSocket.h"
#import "GCDAsyncSocket.h"

unsigned long long OffSet = 1024 * 200;

static NSInteger const timeWithout = -1;
static NSInteger const tagWriteData = 1;
static NSInteger const tagReadData = 2;

static NSString *const keyOffset = @"keyOffset";
static NSString *const keySouceId = @"keySouceId";

@interface SocketManager () <AsyncSocketDelegate, GCDAsyncSocketDelegate>

@property (nonatomic, strong) GCDAsyncSocket *GCDSocket;
@property (nonatomic, strong) AsyncSocket *socket;  // socket
@property (nonatomic, copy) NSString *socketHost;   // socket的Host
@property (nonatomic, assign) UInt16 socketPort;    // socket的prot
@property (nonatomic, strong) NSTimer *socketTimer; // 计时器

@property (nonatomic, assign) BOOL isUploadHead;                // 文件上传头文件协议
@property (nonatomic, assign) unsigned long long filelength;    // 文件上传大小
@property (nonatomic, strong) NSString *fileName;               // 文件上传名称
@property (nonatomic, strong) NSString *
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值