udpSocket

来源:互联网 发布:外套淘宝详情页 编辑:程序博客网 时间:2024/06/06 00:52

//

//  ViewController.m

//  UdpSocket

//

//  Created by tangqinglong on 14-7-3.

//  Copyright (c) 2014 tangqinglong. All rights reserved.

//


#import "ViewController.h"

#import "AsyncUdpSocket.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutletUITextField *sendMessage;

@property (weak, nonatomic) IBOutletUITableView *tableView;

@property (weak, nonatomic) IBOutletUITextView *textView;

@property (strong,nonatomic)AsyncUdpSocket* udpSocket;

@property (weak, nonatomic) IBOutletUISwitch *onOffswitch;

@property (strong,nonatomic)NSMutableArray* hostOnlines;




@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.hostOnlines = [NSMutableArrayarray];

    // 三步操作

   self.udpSocket = [[AsyncUdpSocketalloc]initWithDelegate:self];

    // 绑定端口

    [self.udpSocketbindToPort:9000error:nil];

    // 设置为广播

    [self.udpSocketenableBroadcast:YESerror:nil];

    // 调用接收数据

    [self.udpSocketreceiveWithTimeout:-1tag:0];

    NSTimer* timer = [NSTimerscheduledTimerWithTimeInterval:2target:selfselector:@selector(checkHost)userInfo:nilrepeats:YES];

    //self.tableView.delegate =self;

}

-(void)checkHost

{  

    // NSString 转化为NSData方法

    NSData* data = [@"谁在线"dataUsingEncoding:NSUTF8StringEncoding];

    [self.udpSocketsendData:data toHost:@"255.255.255.255"port:9000withTimeout:-1tag:0];

    

}

// 实现接收数据方法

-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port

{

    

    NSString* message =[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];

   if (![host hasPrefix:@":"])

    {

       NSLog(@"%@:%@",host,message);

       if ([message isEqualToString:@"谁在线"])

        {

            //收到谁在线,应该回答对方,我在线

            NSData* data = [@"我在线"dataUsingEncoding:NSUTF8StringEncoding];

            [socksendData:data toHost:@"255.255.255.255"port:9000 withTimeout:-1 tag:0];

        }

       else if([messageisEqualToString:@"我在线"])

        {

            // 如果不存在host,将host加入hostOnlines

           if (![self.hostOnlinescontainsObject:host])

            {

                [self.hostOnlinesaddObject:host];

                [self.tableViewreloadData];

            }

        }

       else

        {

           self.textView.text = [self.textView.textstringByAppendingFormat:@"%@:%@\n",host,message];

        }

    }

    

    

    // 读后续数据

    [sock receiveWithTimeout:-1tag:0];

    return YES;

}

// 发送完成方法

-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag

{

    

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (IBAction)sendAction:(id)sender

{

    [self.sendMessageresignFirstResponder];

    

   NSString* message = self.sendMessage.text;

    NSData* data = [messagedataUsingEncoding:NSUTF8StringEncoding];

    [self.udpSocketsendData:data toHost:@"255.255.255.255"port:9000withTimeout:-1tag:0];

   

    

    

}

- (IBAction)valueChanged:(id)sender

{

    // on send message to everyone;

    

    

    

    // off send message

}

#pragma tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSLog(@"%d",self.hostOnlines.count);

    returnself.hostOnlines.count;

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{    

   return 1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

   static NSString *identyfier =@"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identyfier];

   if (!cell)

    {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identyfier];

    }

   NSLog(@"%lu", (long)indexPath.row);

   NSLog(@"%@",self.hostOnlines[indexPath.row]);

    cell.textLabel.text = [[self.hostOnlines[indexPath.row]componentsSeparatedByString:@"."]lastObject];

   return cell;


}

@end


0 0
原创粉丝点击