* Socket就是为网络服务提供的一种机制
//1.建立一个连接
NSString*host = @"127.0.0.1";
;
CFReadStreamRefreadStream;
//输入流
CFWriteStreamRefwriteStream;
//输出流
CFStreamCreatePairWithSocketToHost(NULL,(__bridgeCFStringRef)host,port, &readStream, &writeStream);
// 将C语言输入输出流转换成OC对象
_inputStream= (__bridgeNSInputStream*)(readStream);
_outputStream= (__bridgeNSOutputStream*)(writeStream);
//
设置代理
_inputStream.delegate=self;
_outputStream.delegate=self;
//把输入输出流添加到主运行循环
//不添加的话代理可能不被调用
[_inputStreamscheduleInRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];
[_outputStreamscheduleInRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];
//
打开输入输出流
[_inputStream
open];
[_outputStream
open];
- (void)stream:(NSStream*)aStream handleEvent:(NSStreamEvent)eventCode
{
NSLog(@"%@",aStream);
// NSStreamEventOpenCompleted = 1UL << 0,
流打开成功
// NSStreamEventHasBytesAvailable = 1UL << 1,
有字节可读
// NSStreamEventHasSpaceAvailable = 1UL << 2,
可以发送字节
// NSStreamEventErrorOccurred = 1UL << 3,
连接出错
// NSStreamEventEndEncountered = 1UL << 4
连接结束
switch(eventCode) {
caseNSStreamEventOpenCompleted:
NSLog(@"流打开成功");
break;
caseNSStreamEventHasBytesAvailable:
NSLog(@"有字节可读");
[selfreadData];
break;
caseNSStreamEventHasSpaceAvailable:
NSLog(@"可以发送字节");
break;
caseNSStreamEventErrorOccurred:
NSLog(@"连接出错");
break;
caseNSStreamEventEndEncountered:
NSLog(@"连接结束");
//关闭输入输出流
[_inputStreamclose];
[_outputStreamclose];
//从主运行循环移除
[_inputStreamremoveFromRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];
[_outputStreamremoveFromRunLoop:[NSRunLoopmainRunLoop]forMode:NSDefaultRunLoopMode];
break;
default:
break;
}
}
- (void)login
{
// 2.聊天
// 2.1登录
//
发送用户名
//
登录指令
NSString*loginStr =
@"iam:zhangsan";
//
把Str转成NSData
NSData*data= [loginStr dataUsingEncoding:NSUTF8StringEncoding];
[_outputStreamwrite:data.bytesmaxLength:data.length];
}
#pragma mark 读了服务器返回的数据
-(void)readData{
//建立一个缓冲区可以放1024个字节
];
// 返回实际装的字节数
NSIntegerlen = [_inputStreamread:bufmaxLength:sizeof(buf)];
//把字节数组转化成字符串
NSData*data = [NSDatadataWithBytes:buflength:len];
// 从服务器接收到的数据
NSString*recStr= [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];
NSLog(@"gdgdfgdf%@",recStr);
[selfreloadDataWithText:recStr];
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
NSString*text = textField.text;
NSLog(@"%@",text);
//
聊天信息
NSString*msgStr = [NSStringstringWithFormat:@"msg:%@",text];
//把Str转成NSData
NSData*data= [msgStr dataUsingEncoding:NSUTF8StringEncoding];
//
刷新表格
[selfreloadDataWithText:msgStr];
//
发送数据
[_outputStreamwrite:data.bytesmaxLength:data.length];
//
发送完数据,清空textField
textField.text=
nil;
return
YES;
}
-(void)reloadDataWithText:(NSString*)text{
[self.chatMsgsaddObject:text];
[self.tableViewreloadData];
// 数据多,应该往上滚动
NSIndexPath*lastPath = [NSIndexPathindexPathForRow:self.chatMsgs.count-
];
[self.tableViewscrollToRowAtIndexPath:lastPathatScrollPosition:UITableViewScrollPositionBottomanimated:YES];
}
#pragma mark 表格的数据源
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return
self.chatMsgs.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
staticNSString*ID =
@"Cell";
UITableViewCell*cell= [tableView dequeueReusableCellWithIdentifier:ID];
cell.textLabel.text=
self.chatMsgs[indexPath.row];
return cell;
}