Problem in detecting devices using UDP broadcast

5 pts.
Tags:
Apple iOS
UDP
I'm working with an iOS(Objective-C) application that detects devices and displays them in a tableview. The problem with the current application is that it only reads one device randomly even if there are multiple devices available. I think that once it detects a device it stops detecting the others. I've posted this problem in stack overflow but no one answered it.
Here is the code:
struct sockaddr_in broadcastAddr;
    memset(&broadcastAddr, 0, sizeof broadcastAddr);
    broadcastAddr.sin_family = AF_INET;
    inet_pton(AF_INET, "255.255.255.255", &broadcastAddr.sin_addr); // Set the broadcast IP address
    broadcastAddr.sin_port = htons(6789);

    const char *request = "";

    NSMutableArray *ipArr = [[NSMutableArray alloc] init];
    NSMutableArray *dContentArr = [[NSMutableArray alloc] init];

    ret = sendto(sd, request, strlen(request), 0, (struct sockaddr*)&broadcastAddr, sizeof(broadcastAddr));
    if (ret < 0) {
        close(sd);
        [self.spin stopAnimating];
        return;
    }

    received = recvfrom(sd, message, BUFF_SIZE, 0, (struct sockaddr *)&fromAddr, &fromAddrLen);
    if (received == -1) {
        close(sd);
        [self.spin stopAnimating];
        return;
    }

    message[received] = 0;
    NSString *content = [NSString stringWithUTF8String:message];
    [dContentArr addObject:content];

    char display[30] = {0};
    inet_ntop(AF_INET, &fromAddr.sin_addr.s_addr, display, sizeof display);

    NSString *ip = [NSString stringWithCString:display encoding:NSASCIIStringEncoding];
    [ipArr addObject:ip];

    if(ipArr.count > 0) {
        ipArray = [[NSOrderedSet orderedSetWithArray:ipArr] array];
        contentArray = [[NSOrderedSet orderedSetWithArray:dContentArr] array];
    }
    close(sd);
and Here is the code that returns the number of rows
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([ipArray count] == 0)
        return 1;
    else
        return [ipArray count];
}


Software/Hardware used:
iOS iPhone
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: