0

I'm trying to start off and create a simple UDP server and client using CocoaASyncSocket. I've pretty much replicated the code in the examples from here

UPDATE:

I've tried running the provided examples but they don't seem to work with the host address + port that I provided. What would I fill in for those (on the client end) if my client + server are on the same wifi?

My Server:

-(void)startServer{
    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *error = nil;

    if (![udpSocket bindToPort:PORT error:&error])
    {
        NSLog(@"Error starting server (bind): %@", error);
        return;
    }
    if (![udpSocket beginReceiving:&error])
    {
        [udpSocket close];
        NSLog(@"Error starting server (recv): %@", error);
        return;
    }

    NSLog(@"Udp Echo server started on %@:%i",[udpSocket localHost_IPv4],[udpSocket localPort]);

    isRunning = YES;

}

My Client:

-(void)setupSocket{
    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *error = nil;


    if (![udpSocket bindToPort:HOST_PORT error:&error])
    {
        NSLog(@"Error binding: %@", error);
        return;
    }
    if (![udpSocket beginReceiving:&error])
    {
        [udpSocket close];
        NSLog(@"Error receiving: %@", error);
        return;
    }

    NSLog(@"ready");
}

- (IBAction)send:(id)sender
{

    NSString *msg = @"testerrrr";

    NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
    [udpSocket sendData:data toHost:HOST_IP port:HOST_PORT withTimeout:-1 tag:0];
    NSLog(@"sent message to server");
}

I don't believe that my issue is in the initialization of the server or the socket, but perhaps in what my chosen IP + PORT's are. Right now I am running the server on iOS simulator and the client on a real device. When sending data from the device, I can see that packets are going out, but none are being received by the server.

I have it set up to bind port 1234, and that the client sends it to the server's address which I got from "whatismyip.com". I also tried reversing the roles, but still no success.

Additionally, when I tried sending data to itself (server -> server for example), that worked, but between the two no luck!

This seems like a pretty straightforward task but haven't been able to get it to work. Appreciate the help!

1 Answer 1

0

As I initially suspected, the issue was with the address i was entering on behalf of the client. The rule to thumb is:

  1. If S + C are running on same device. Use "localhost" as host address
  2. If S + C are running on different devices, but on same wifi connection. Do IFConfig on the server and use the IP found there.
  3. If S + C are running on different devices and do not share the same connection, you need to forward the port from your router to your computer.

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.