对socket的了解: 建立socket连接,至少需要一对套接字。其中一个运行于客户端,称为ClientSocket,另一个运行于服务端,称为ServerSocket。套接字之间的连接过程分为三个步骤:服务端监听,客户端请求,连接确认。
Socket可以支持不同的传输层协议(TCP或UDP),当使用TCP协议进行连接时,该Socket连接就是一个TCP连接,UDP连接同理。
效果图如下:
1 > 先运行服务器,点击开始监听,会出现 “开放端口成功”几个字,说明端口已经连接。
IMG_7885.PNG2 > 运行客户端,默认自动连接设置好的自己的电脑IP地址,在输入1111111,点击发送之后,可以在上面看到111111的数据。
IMG_7892.PNG3 > 在服务端,输入22222数据,点击发送
IMG_7887.PNG4 > 回到客户端,可以看到接收到了服务端发送的22222数据,然后在在下面输入33333数据,点击发送按钮
IMG_7888.PNG4 > 在客户端,可以看到33333数据
IMG_7889.PNG ](http:https://img.haomeiwen.com/i2849255/1c0a4fdd6a06d6ef.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)5 > 回到服务端,输入444444数据,点击发送
IMG_7893.PNG6 > 回到客户端,可以看到接收到了服务端发送的444444数据,
IMG_7891.PNG这就是一个简单的socket通信实例
下面看看代码的实现:
首先建两个工程,分别写服务器端和客户端的,然后得引入CocoaAsyncSocket这个类库。客户端的IP地址要写自己电脑的,服务器端和客户端的端口号要相同。
一 : 服务器端:
//导入封装好的socket头文件
#import "GCDAsyncSocket.h"
#define portValue @"8080"
... 定义的属性:
@interface ViewController ()<GCDAsyncSocketDelegate>
//服务器socket(开放端口,监听客户端socket的链接)
@property (nonatomic ,strong) GCDAsyncSocket * serverSocket;
//客户端socket
@property (nonatomic ,strong) GCDAsyncSocket * clientSocket;
@property (weak, nonatomic) IBOutlet UILabel * showContentMessage;
@property (weak, nonatomic) IBOutlet UITextField * serverTextField;
@property (weak, nonatomic) IBOutlet UITextField * sendMessageContent;
@end
...在viewDidLoad初始化socket
//初始化socket
serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
...开始监听
//开始监听(端口开放情况)
- (IBAction)startReceiveConnect:(id)sender {
NSError * error = nil;
BOOL result = [serverSocket acceptOnPort:portValue.integerValue error:&error];
if (result && error == nil) {
//开发端口成功
showContentMessage.text = @"开放端口成功";
}else{
NSLog(@"error = %@",error);
//开发端口成功
showContentMessage.text = @"开放端口失败";
}
}
...发送消息
//连接成功后,服务端发送消息
- (IBAction)startSendMessage:(id)sender {
NSData * data = [sendMessageContent.text dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"sendMessageContent.text = %@",sendMessageContent.text);
//withTimeout -1:无穷大,一直等 tag:消息标记
[clientSocket writeData:data withTimeout:-1 tag:0];
}
...实现接收消息的代理
#pragma mark -- GCDAsyncSocketDelegate
-(void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket{
//保存客户端的socket
self.clientSocket = newSocket;
[self.clientSocket readDataWithTimeout:-1 tag:0];
}
二 : 客户端:
//导入封装好的socket头文件
#import "GCDAsyncSocket.h"
#define IPAdress @"10.0.2.100"
#define portValue @"8080"
... 定义的属性:
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,GCDAsyncSocketDelegate,UITextFieldDelegate>
//保存所发消息
@property (nonatomic ,strong) NSMutableArray * dataArray;
//客户端socket
@property (nonatomic ,strong) GCDAsyncSocket * clinetSocket;
@property (weak, nonatomic) IBOutlet UITextField * clientTextField;
@property (weak, nonatomic) IBOutlet UITableView * clientTableView;
@end
@implementation ViewController
@synthesize clinetSocket,clientTableView,clientTextField;
-(NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
...在viewDidLoad初始化socket和tableview
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[clientTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableView"];
clientTableView.delegate = self;
clientTableView.dataSource = self;
clientTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
clientTextField.delegate = self;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGestureRecognizer];
// [self.dataArray addObjectsFromArray:@[@"嗨,你好!",@"你好",@"最近过的好吗?",@"嗯,还不错,你呢?",@"还行。"]];
//初始化客户端socket
clinetSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
//连接服务器
[clinetSocket connectToHost:IPAdress onPort:portValue.integerValue withTimeout:-1 error:0];
}
... UITableViewDelegate
#pragma mark -- UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"tableView" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (_dataArray.count > 0) {
if (indexPath.row % 2 == 0) {
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
}else{
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
}
cell.textLabel.text = _dataArray[indexPath.row];
}
return cell;
}
...GCDAsynSocket Delegate
#pragma mark - GCDAsynSocket Delegate
#pragma mark 连接成功
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
[self.clinetSocket readDataWithTimeout:-1 tag:0];
}
#pragma mark 断开连接
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
if (err) {
NSLog(@"断开失败 : err = %@",err);
}else{
NSLog(@"正常断开");
}
}
//接收服务端发送过来的消息,并讲数据保存在data数组里面,以便显示
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString * text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"text = %@",text);
[self.dataArray addObject:text];
[clinetSocket readDataWithTimeout:-1 tag:0];
[clientTableView reloadData];
}
//发送消息,并讲数据保存在data数组里面,以便显示
- (IBAction)sendClientMessage:(id)sender {
NSLog(@"clientTextField.text = %@",clientTextField.text);
NSData * data = [clientTextField.text dataUsingEncoding:NSUTF8StringEncoding];
[self.dataArray addObject:clientTextField.text];
[clinetSocket writeData:data withTimeout:-1 tag:0];
[clientTableView reloadData];
}
- (void)keyboardHide:(UITapGestureRecognizer*)tap{
[clientTextField resignFirstResponder];
}
网友评论