2 w2309711 w2309711 于 2017.01.04 17:40 提问

oc中,我用GCDAsyncSocket连上了socket,但是无法往服务器发送数据

#import "GCDSocketManager.h"
#import "GCDAsyncSocket.h"

#define SocketHost @"61.191.45.94"
#define SocketPort 8091

@interface GCDSocketManager()

//握手次数
@property(nonatomic,assign) NSInteger pushCount;

//断开重连定时器
@property(nonatomic,strong) NSTimer *timer;

//重连次数
@property(nonatomic,assign) NSInteger reconnectCount;

@end

@implementation GCDSocketManager

//全局访问点

  • (instancetype)sharedSocketManager { static GCDSocketManager *_instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; }

//可以在这里做一些初始化操作

  • (instancetype)init
    {
    self == [super init];
    if (self) {
    self.socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    }
    return self;
    }

#pragma mark 请求连接
//连接

  • (void)connectToServer {
    self.pushCount = 0;

    self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *error = nil;
    // [self.socket connectToHost:SocketHost onPort:SocketPort error:&error];
    [self.socket connectToHost:SocketHost onPort:SocketPort error:&error];
    if (error) {
    NSLog(@"SocketConnectError:%@",error);
    }
    }

#pragma mark 连接成功
//连接成功的回调

  • (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
    NSLog(@"socket连接成功");

    [self sendDataToServer];
    }

//连接成功后向服务器发送数据

  • (void)sendDataToServer {
    NSString *jsonData = @"kUOhLq1vFfPqzx5WtcJr0ZWmZ89uM9KEx4bthINy3HL2QD8oLjBGXhVv2gAlDjfwtTp0HrwnGLYxAloJ+ABzqzIS2OBG6MwDJLwQmJVPajgcNZhLZKFzmdvWbo+7HR+hSN2Co4DQRQfUm2n8HsDjAyNVUgpmO39NJ4XY085oGPE/dHkq/t5Y7A==";
    NSData *cmdData = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"socket发送数据--->%@",cmdData);
    //发送
    [self.socket writeData:cmdData withTimeout:-1 tag:0];

    //读取数据
    [self.socket readDataWithTimeout:-1 tag:100];
    }

//连接成功向服务器发送数据后,服务器会有响应

  • (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
    NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"收到的数据-->%@",msg);
    [self.socket readDataWithTimeout:-1 tag:200];

    //服务器推送次数
    self.pushCount++;

    //在这里进行校验操作,情况分为成功和失败两种,成功的操作一般都是拉取数据
    }

#pragma mark 连接失败
//连接失败的回调

  • (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err {
    NSLog(@"Socket连接失败");

    self.pushCount = 0;

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    NSString *currentStatu = [userDefaults valueForKey:@"Statu"];

    //程序在前台才进行重连
    if ([currentStatu isEqualToString:@"foreground"]) {

    //重连次数
    self.reconnectCount++;
    
    //如果连接失败 累加1秒重新连接 减少服务器压力
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 * self.reconnectCount target:self selector:@selector(reconnectServer) userInfo:nil repeats:NO];
    
    self.timer = timer;
    

    }
    }

//如果连接失败,5秒后重新连接

  • (void)reconnectServer {

    self.pushCount = 0;

    self.reconnectCount = 0;

    //连接失败重新连接
    NSError *error = nil;
    [self.socket connectToHost:SocketHost onPort:SocketPort error:&error];
    if (error) {
    NSLog(@"SocektConnectError:%@",error);
    }
    }

#pragma mark 断开连接
//切断连接

  • (void)cutOffSocket {
    NSLog(@"socket断开连接");

    self.pushCount = 0;

    self.reconnectCount = 0;

    [self.timer invalidate];
    self.timer = nil;

    [self.socket disconnect];
    }
    @end

sendDataToServer中我调用writeData给服务器发送数据无法发送

1个回答

yy35hh
yy35hh   2017.01.20 16:39

把tag都改成一致再试试

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!