IOS开发中socket第三方库 AsyncSocket的问题

blue1990 发布于 2013/11/07 16:12
阅读 3K+
收藏 0

在一个ios应用中我使用socket第三方库 AsyncSocket来实现手机客户端和PC服务器端的通信。服务器可以收到客户端发来信息,也有返回信息,但是客户端却没有收到这个返回的信息,这个为什么呢?下面是一部分代码



#import "ViewController.h"

@implementation ViewController
@synthesize socket;
@synthesize host;
@synthesize message;
@synthesize port;
@synthesize status;
@synthesize button;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
-(void)addText:(NSString *)str
{
    status.text = [status.text stringByAppendingFormat:@"%@\n",str];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    host.text = @"61.191.200.214";
    port.text = @"9631";

    [button setTitle:@"连接" forState:UIControlStateNormal];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)viewDidUnload
{
    [self setHost:nil];
    [self setMessage:nil];
    [self setStatus:nil];
    [self setPort:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
    [self addText:[NSString stringWithFormat:@"连接到:%@",host]];
    [socket readDataWithTimeout:-1 tag:0];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
}
- (IBAction)send:(id)sender { //开始按钮
    //连接服务器端
    socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    NSError *err = nil;
    if(![socket connectToHost:host.text onPort:[port.text intValue] error:&err])
    {
        [self addText:err.description];
    }else
    {
        [self addText:[NSString stringWithFormat: @"打开%@端口",socket.localHost]];
    }
        
    //发送数据
    if (message.text.length!=0){
        NSString* str_send=[[NSString alloc] initWithString:[NSString stringWithFormat:@"send%@",message.text]];
        [socket writeData:[str_send dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:-1];
        [self addText:[NSString stringWithFormat:@"发送:%@",message.text]];
            
    }else{
        NSString* str_send= @"receive";
        [socket writeData:[str_send dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:-1];
        [message resignFirstResponder];
        [socket readDataWithTimeout:-1 tag:0];
    }
}

-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
    NSString *receive = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [self addText:[NSString stringWithFormat:@"%@:%@",sock.connectedHost,receive]];
    [socket readDataWithTimeout:-1 tag:0];
}

@end

加载中
0
blue1990
blue1990
大神们,小弟紧急求助啊
0
蔡成汉
蔡成汉
没有设置delegate,self.socket.delegate=self;
0
feng_blog
feng_blog
这么多代码,谁有时间看啊。我总结过这个内容,挺详细的,自己看吧:http://my.oschina.net/u/2448717/blog/499784
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部