iOS 네트워크 통신 04
4104 단어 단체 채팅방
- (void)viewDidLoad {
[super viewDidLoad];
//1.
//1.1 socket
GCDAsyncSocket *clientSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
self.clientSocket = clientSocket;
//1.2
NSError *erro = nil;
[clientSocket connectToHost:@"172.21.62.9" onPort:5288 error:&erro];
if (!erro) {
NSLog(@"%@",erro);
}
//2.
}
2.프록시 방법을 통해 링크 상태 보기
#pragma mark --
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
NSLog(@" ");
//
[sock readDataWithTimeout:-1 tag:0];
}
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
NSLog(@" %@",err);
}
3.프록시 방법을 통해 서버 측의 메시지를 읽다
//
-(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"===%@",str);
NSLog(@"%@",[NSThread currentThread]);
if (str) {
[self.dataSources addObject:str];
//
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
NSLog(@"%@",[NSThread currentThread]);
});
// [[NSOperationQueue mainQueue] addOperationWithBlock:^{
// [self.tableView reloadData];
// }];
}
//
[sock readDataWithTimeout:-1 tag:0];
}
4.클라이언트 인터페이스 메시지
//
- (IBAction)didSendButton:(id)sender {
NSString *str = self.textField.text;
if (str.length == 0) {
return;
}
//
[self.dataSources addObject:str];
//
// dispatch_async(dispatch_get_main_queue(), ^{
// [self.tableView reloadData];
// });
[self.tableView reloadData];
//
[self.clientSocket writeData:[str dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
}
5.tableView 셀을 통해 데이터 표시
#pragma mark -- tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSources.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];
}
//
cell.textLabel.text = self.dataSources[indexPath.row];
return cell;
}
2. 서버 측
1.서버 오픈
/**
* 10086 :5288
*/
-(void)start{
// Socket :
GCDAsyncSocket *serverSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
// 10086
NSError *error = nil;
[serverSocket acceptOnPort:5288 error:&error];
if (!error) {
NSLog(@" ");
}else{
NSLog(@" :%@",error);
}
self.serverSocket = serverSocket;
}
2.클라이언트를 연결하는 Socket
#pragma mark -- Socket
-(void)socket:(GCDAsyncSocket *)serverSocket didAcceptNewSocket:(GCDAsyncSocket *)clientSocket{
NSLog(@"serve:%@",serverSocket);
NSLog(@"client:%@ host:%@,port:%d",clientSocket,clientSocket.connectedHost,clientSocket.connectedPort);
//1. Socket
[self.clientSockets addObject:clientSocket];
//2.
[clientSocket readDataWithTimeout:-1 tag:0];
NSLog(@" %ld ",self.clientSockets.count);
}
3.고객이 요청한 데이터 읽기
#pragma mark --
-(void)socket:(GCDAsyncSocket *)clientSocket didReadData:(NSData *)data withTag:(long)tag{
//
for (GCDAsyncSocket *socket in self.clientSockets) {
if (socket != clientSocket) {//
[socket writeData:data withTimeout:-1 tag:0];
}
}
#warning ,
[clientSocket readDataWithTimeout:-1 tag:0];
}