AsyncUdpSocket how to use receive

前端 未结 4 1943
甜味超标
甜味超标 2021-01-04 18:23

I am trying to get a program for iPhone running on the simulator. My problem is with receiving UDP data. I use asyncUdpSocket. If I make a socket and use sendData:(NSD

4条回答
  •  余生分开走
    2021-01-04 18:27

    Not sure if this will be helpful, but I had the same problem, and here is how I fixed it.

    In my case, the problem was that:

    [self.socket receiveWithTimeout:-1 tag:0];
    

    was located in the "wrong" place.

    If you call [self.socket receiveWithTimeout:-1 tag:0]; in the method didFinishLaunchingWithOptions, the socket won't work regardless of what you do (even if you try launching it in a new thread). To fix this issue, I made a button and moved the receiveWithTimeout call to a method called when the button is clicked. My guess is that ASyncUdpSocket doesn't like something about the thread handling in didFinishLaunchingWithOptions.

    I have posted my working sample code below (using XCode 5.1.1). These are the complete AppDelegate files for my Xcode project.

    AppDelegate.h

    #import 
    #import "AsyncUdpSocket.h"
    
    @interface AppDelegate : UIResponder 
    
    @property (strong, nonatomic) UIWindow *window;
    @property (strong, nonatomic) AsyncUdpSocket *udpSocket;
    @property (strong, nonatomic) UILabel *receiver;
    
    @end
    

    AppDelegate.m

    #import "AppDelegate.h"
    #import "AsyncUdpSocket.h"
    #import 
    #import 
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Create the main window
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
    
        // Create a label for showing received text
        self.receiver = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 80.0)];
        self.receiver.text  = @"No message, yet!";
        self.receiver.textColor       = [UIColor blackColor];
        [self.window addSubview:self.receiver];
    
        // Create a button for sending messages
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(80.0, 210.0, 160.0, 40.0)];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"Start Game" forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor blueColor]];
        [self.window addSubview:button];
    
        @try {
            self.udpSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
            if (![self.serverSocket bindToPort:9003 error:nil]) {
                NSLog(@"COULD NOT BIND TO PORT");
            }
            if (![self.udpSocket enableBroadcast:YES error:nil]) {
                NSLog(@"COULD NOT ENABLE BROADCASTING");
            }
        } @catch (NSException * e) {
            NSLog(@"Exception: %@", e);
        }
        return YES;
    }
    
    - (void)buttonClick:(UIButton*)button {
        NSData * data = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding];
        [self.udpSocket receiveWithTimeout:-1 tag:0];
        if (![self.udpSocket sendData:data toHost:@"127.0.0.1" port:9003 withTimeout:0.2 tag:1]) {
            NSLog(@"COULD NOT SEND DATA");
        } else {
            NSLog(@"Sent packet (from %@:%d to 127.0.0.1:9001)", self.udpSocket.localHost, self.udpSocket.localPort);
        }
    }
    
    - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port {
        NSLog(@"    Received data (from %@:%d) - %@", host, port, data);
        self.receiver.text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        [self.udpSocket receiveWithTimeout:-1 tag:0];
    
        return YES;
    }
    
    @end
    

    Hope this is helpful to someone.

提交回复
热议问题