Socket客户端(三)
来源:互联网 发布:java中的接口定义 编辑:程序博客网 时间:2024/06/03 20:26
1、将GCDAsyncSocket框架导入项目中
2、代码实现如下:
#import <Foundation/Foundation.h>@interface MessageModel : NSObject@property (nonatomic, strong) NSString *type;@property (nonatomic, strong) NSString *context;@end
#import <UIKit/UIKit.h>@interface MessageCell : UITableViewCell@property (nonatomic, strong) UILabel *message;@end
#import "MessageCell.h"@implementation MessageCell-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.message = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, [UIScreen mainScreen].bounds.size.width - 40, 20)]; [self.contentView addSubview:self.message]; } return self;}- (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state}
#import "ViewController.h"#import "GCDAsyncSocket.h"#import "MessageCell.h"#import "MessageModel.h"#define kWidth self.view.frame.size.width#define kHeight self.view.frame.size.height@interface ViewController ()<GCDAsyncSocketDelegate,UITableViewDataSource,UITableViewDelegate>@property (nonatomic, strong) GCDAsyncSocket *socket;@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) UITextField *textFiled;@property (nonatomic, strong) UIButton *sendBtn;@property (nonatomic, strong) NSMutableArray *dataSource;@end@implementation ViewController-(NSMutableArray *)dataSource{ if (!_dataSource) { self.dataSource = [NSMutableArray array]; } return _dataSource;}- (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,kWidth ,kHeight - 40) style:UITableViewStyleGrouped]; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; self.textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, kHeight - 40, kWidth - 100, 30)]; self.textFiled.backgroundColor = [UIColor grayColor]; [self.view addSubview:self.textFiled]; self.sendBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.sendBtn.frame = CGRectMake(kWidth - 100, kHeight - 40, 80, 30); [self.sendBtn setTitle:@"发送" forState:UIControlStateNormal]; self.sendBtn.backgroundColor = [UIColor orangeColor]; [self.sendBtn addTarget:self action:@selector(handleSenderAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.sendBtn]; //连接到聊天服务器 GCDAsyncSocket *socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)]; [socket connectToHost:@"192.168.0.187" onPort:5239 error:nil]; self.socket = socket; [self.tableView registerClass:[MessageCell class] forCellReuseIdentifier:@"cell"];}-(void)handleSenderAction{ if (self.textFiled.text.length >0) { [self.socket writeData:[self.textFiled.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; //添加到数据源 MessageModel *model = [[MessageModel alloc] init]; model.type = @"s"; model.context = [NSString stringWithFormat:@"ME::%@",self.textFiled.text]; [self.dataSource addObject:model]; //刷新 [self.tableView reloadData]; } self.textFiled.text = @"";}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataSource.count;}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; MessageModel *mmodel = self.dataSource[indexPath.row]; cell.message.text = mmodel.context; if ([mmodel.type isEqualToString:@"r"]) { cell.message.textAlignment = NSTextAlignmentLeft; }else{ cell.message.textAlignment = NSTextAlignmentRight; } return cell;}-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{ //1、读取数据 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",string); //2、添加到数据源 MessageModel *model = [[MessageModel alloc] init]; model.type = @"r"; model.context = [NSString stringWithFormat:@"Other::%@",string]; [self.dataSource addObject:model]; //3、刷新数据 [[NSOperationQueue mainQueue]addOperationWithBlock:^{ [self.tableView reloadData]; }]; //4、每次接受完数据,都要再次监听数据 [sock readDataWithTimeout:-1 tag:0];}-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{ NSLog(@"连接主机成功"); //监听数据 [self.socket readDataWithTimeout:-1 tag:0];}-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{ NSLog(@"连接主机失败%@",err);}@end
3、连接主机
4、不同客户端之间互相发送信息
1 0
- Socket客户端(三)
- socket编程(三)---- 简单的客户端
- socket实现客户端与服务端通信(三)服务端升级
- C# 多线程学习(三)Socket 服务器与客户端通信
- Java Socket学习(三)——客户端之间通信
- PC客户端与Android服务端的Socket同步通信(三)
- Unix网络编程学习日记(三):多进程全双工socket客户端的实现
- C++网络(三)用Socket API建立简易TCP服务端、客户端
- Java Socket编程系列(三)开发支持单客户端访问的Server
- socket(TCP)客户端程序
- socket(UDP)客户端程序
- Socket(Tcp)通信客户端
- Socket(Udp)通信客户端
- socket 流程(服务器,客户端)
- Socket传输(下)----客户端
- c++Socket客户端 (vs2013)
- socket编程(三)
- socket(三)
- 全球酒店详情查看API接口-基于极核数据
- TortoiseGit海龟使用SSH key
- anfengde--epub ui
- Qt及Nsis设置开机自启动
- CODE[VS] 1197 Vigenère密码(模拟
- Socket客户端(三)
- 解决loadrunner在脚本回放时长时间等待
- js变量
- Java中如何得到程序的执行时间
- Mysql-5.7.15解压版(免安装)使用
- C++使用 Git 生成编译版本号
- Spring Boot中的注解
- Java gc(垃圾回收)机制
- C实现大数据文件的内存映射机制