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
- udpSocket
- UDPSocket
- java UDPSocket 简单示例
- Linux--UDPsocket文件传输
- Linux--UDPsocket传输图片
- udpSocket捕获ICMP不可达错误
- 黑马程序员:udpsocket介绍:DatagramSocket、DatagramPacket
- Delphi UDPSocket 发送Record 记录类型
- Android的NDK的UDPsocket通信
- Qt 的udpSocket通信及相关
- udpSocket捕获ICMP不可达错误
- QT中UDPSocket丢包问题
- QT中UDPSocket丢包问题(续)
- Qt 使用UDPSocket遇到的丢包问题
- [MFC学习笔记]--SOCKET编程只client端\UDPsocket通信
- UdpSocket如何实现通过代理接收多包数据
- QT下udpsocket一段时间接收不到数据的问题
- linux下使用udpsocket时遇到的问题
- IOS-Xcode5.1.1创建Xib工程
- UVa 10125 - Sumsets
- 读书笔记1——Apriori算法详解
- PHP 统计一维数组value相同的元素的个数num,并将其转化为下标为数字,值是value和num的二维数组
- 楼天城楼教主的acm心路历程(作为励志用)
- udpSocket
- 构造函数
- 润乾报表-Js正则表达式
- mysql分类查询每个分类的两条记录
- openstack笔记1
- python运行shell命令四种方法
- android wifi连接 有无密码均实机验证成功案例
- jeeshop1.1 Beta版本发布
- iOS如何实现传值---block传值