UDPSocket
1258 단어 UDPSocket
- (void)viewDidLoad
{
[super viewDidLoad];
//
_sendSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
//
[_sendSocket bindToPort:5555 error:nil];
//
_recvSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
//
[_recvSocket bindToPort:6666 error:nil];
//
[_recvSocket receiveWithTimeout:-1 tag:0];
}
//
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
NSString* str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
_textView.text = [NSString stringWithFormat:@"%@%@:%@
",_textView.text,host,str];
//
[_recvSocket receiveWithTimeout:-1 tag:0];
return YES;
}
//
- (void)sendText:(id)sender{
NSData* data = [_sendField.text dataUsingEncoding:NSUTF8StringEncoding];
//
[_sendSocket sendData:data toHost:_ipField.text port:6666 withTimeout:-1 tag:0];
_textView.text = [NSString stringWithFormat:@"%@ :%@
",_textView.text,_sendField.text];
_sendField.text = @"";
}
//
- (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
}