Created
February 1, 2014 13:24
-
-
Save garyjohnson/8752318 to your computer and use it in GitHub Desktop.
UDP Multicast Chat for Different Mobile Platforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ViewController.h" | |
| #import <netinet/in.h> | |
| #import <sys/socket.h> | |
| #import <CFNetwork/CFSocketStream.h> | |
| #include <arpa/inet.h> | |
| @interface ViewController() | |
| -(void)openSocket; | |
| @end | |
| @implementation ViewController | |
| @synthesize inputTextField; | |
| @synthesize outputTextView; | |
| -(IBAction)onClick:(id)sender{ | |
| struct sockaddr_in addr; | |
| addr.sin_family = AF_INET; | |
| addr.sin_port = htons(3007); | |
| inet_pton(AF_INET, "224.109.108.107", | |
| &addr.sin_addr); | |
| inputTextField.text = [inputTextField.text stringByAppendingString:@"\r\n"]; | |
| NSData* input = [inputTextField.text dataUsingEncoding:NSUTF8StringEncoding]; | |
| NSData* data = [NSData dataWithBytes:&addr length:sizeof(addr)]; | |
| [socket sendData:input toAddress:data withTimeout:999999 tag:0]; | |
| inputTextField.text = @""; | |
| //[socket sendData:data withTimeout:9999999 tag:0]; | |
| } | |
| - (void)didReceiveMemoryWarning | |
| { | |
| [super didReceiveMemoryWarning]; | |
| } | |
| #pragma mark - View lifecycle | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| [self openSocket]; | |
| } | |
| -(void)openSocket{ | |
| dispatch_queue_t queue = dispatch_queue_create("com.sharkfist.*", 0); | |
| socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:queue]; | |
| //socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self]; | |
| NSError* error = nil; | |
| //[socket bindToAddress:[@"224.109.108.107:3007" dataUsingEncoding:NSUTF8StringEncoding] error:&error]; | |
| [socket bindToPort:3007 error:&error]; | |
| if(error != nil){ | |
| NSLog(@"%@", [error description]); | |
| } | |
| [socket joinMulticastGroup:@"224.109.108.107" error:&error]; | |
| if(error != nil){ | |
| NSLog(@"%@", [error description]); | |
| } | |
| [socket enableBroadcast:YES error:&error]; | |
| dispatch_sync(queue, ^{}); | |
| if(error != nil){ | |
| NSLog(@"%@", [error description]); | |
| } | |
| [socket beginReceiving:&error]; | |
| if(error != nil){ | |
| NSLog(@"%@", [error description]); | |
| } | |
| } | |
| - (void)udpSocket:(GCDAsyncUdpSocket *)sock didConnectToAddress:(NSData *)address{ | |
| NSLog(@"didConnectToAddress"); | |
| } | |
| - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotConnect:(NSError *)error{ | |
| NSLog(@"didNotConnect"); | |
| } | |
| - (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tag{ | |
| NSLog(@"didSendDataWithTag"); | |
| } | |
| - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{ | |
| NSLog(@"%@", error.description); | |
| NSLog(@"didNotSendDataWithTag"); | |
| } | |
| - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data | |
| fromAddress:(NSData *)address | |
| withFilterContext:(id)filterContext{ | |
| NSLog(@"didReceiveData"); | |
| NSString* input = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
| if(input != nil){ | |
| if(outputTextView != nil){ | |
| if(outputTextView.text == nil){ | |
| outputTextView.text = @""; | |
| } | |
| dispatch_queue_t mainQueue = dispatch_get_main_queue(); | |
| dispatch_async(mainQueue, ^(void) | |
| { | |
| outputTextView.text = [outputTextView.text stringByAppendingString:input]; | |
| }); | |
| } | |
| } | |
| } | |
| - (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError *)error{ | |
| NSLog(@"socket close with error"); | |
| } | |
| - (void)viewDidUnload | |
| { | |
| [super viewDidUnload]; | |
| } | |
| - (void)viewWillAppear:(BOOL)animated | |
| { | |
| [super viewWillAppear:animated]; | |
| } | |
| - (void)viewDidAppear:(BOOL)animated | |
| { | |
| [super viewDidAppear:animated]; | |
| } | |
| - (void)viewWillDisappear:(BOOL)animated | |
| { | |
| [super viewWillDisappear:animated]; | |
| } | |
| - (void)viewDidDisappear:(BOOL)animated | |
| { | |
| [super viewDidDisappear:animated]; | |
| } | |
| - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
| { | |
| // Return YES for supported orientations | |
| if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { | |
| return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); | |
| } else { | |
| return YES; | |
| } | |
| } | |
| -(void)dealloc{ | |
| [socket release]; | |
| [super dealloc]; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| using System.Windows.Documents; | |
| using System.Windows.Input; | |
| using System.Windows.Media; | |
| using System.Windows.Media.Animation; | |
| using System.Windows.Shapes; | |
| using Microsoft.Phone.Controls; | |
| using System.Text; | |
| namespace War | |
| { | |
| public partial class MainPage : PhoneApplicationPage | |
| { | |
| public MainPage() | |
| { | |
| InitializeComponent(); | |
| Open(); | |
| } | |
| private UdpAnySourceMulticastClient _client; | |
| private Byte[] _outputBuffer = new Byte[1024]; | |
| private Byte[] _inputBuffer = new Byte[1024]; | |
| private void Open() | |
| { | |
| _client = new UdpAnySourceMulticastClient(IPAddress.Parse("224.109.108.107"), 3007); | |
| _client.BeginJoinGroup(result => | |
| { | |
| _client.EndJoinGroup(result); | |
| Receive(); | |
| }, null); | |
| } | |
| private void Receive() | |
| { | |
| Array.Clear(_outputBuffer, 0, _outputBuffer.Length); | |
| _client.BeginReceiveFromGroup(_outputBuffer, 0, _outputBuffer.Length, | |
| result => | |
| { | |
| IPEndPoint source; | |
| _client.EndReceiveFromGroup(result, out source); | |
| Byte[] recievedCopy = new Byte[_outputBuffer.Length]; | |
| _outputBuffer.CopyTo(recievedCopy, 0); | |
| HandleReceive(recievedCopy); | |
| Receive(); | |
| }, null); | |
| } | |
| private void Send(String input) | |
| { | |
| Array.Clear(_inputBuffer, 0, _inputBuffer.Length); | |
| Encoding.UTF8.GetBytes(input, 0, input.Length, _inputBuffer, 0); | |
| _client.BeginSendToGroup(_inputBuffer, 0, _inputBuffer.Length, | |
| result => | |
| { | |
| _client.EndSendToGroup(result); | |
| }, null); | |
| } | |
| private void HandleReceive(byte[] received) | |
| { | |
| String outputText = Encoding.UTF8.GetString(received, 0, received.Length); | |
| Dispatcher.BeginInvoke(() => | |
| { | |
| output.Text += outputText; | |
| }); | |
| } | |
| private void Button_Click(object sender, RoutedEventArgs e) | |
| { | |
| Send(input.Text + Environment.NewLine); | |
| input.Text = ""; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment