Giter VIP home page Giter VIP logo

Comments (10)

fstanley avatar fstanley commented on May 27, 2024

The following very simple example worked for me. Verified with wireshark

if (!udpSocket) {
    udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    [udpSocket enableBroadcast:YES error:nil];
}

NSData *data = [@"Test Message" dataUsingEncoding:NSUTF8StringEncoding];

[udpSocket sendData:data toHost:@"255.255.255.255" port:12345 withTimeout:-1 tag:tag++];

from cocoaasyncsocket.

horhe17 avatar horhe17 commented on May 27, 2024

Thank you, i will try your code. And how do u receive answer?

from cocoaasyncsocket.

horhe17 avatar horhe17 commented on May 27, 2024

i'm initializing udpSocket from other controller, may be this is the problem, when i put my send data code with initialization code everything works, if i put it somewhere else to other method - no, just doesnt send.

from cocoaasyncsocket.

horhe17 avatar horhe17 commented on May 27, 2024

send data to exact IP works, but doesnt work to broadcast IP

from cocoaasyncsocket.

horhe17 avatar horhe17 commented on May 27, 2024

My code for initialize:

self.udpSocket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
self.udpSocket.delegate  = self;

NSError *error = nil;

if (![self.udpSocket bindToPort:port error:&error]) { NSLog(@"Error starting server (bind): %@", error); return; }

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

NSLog(@"Udp Echo server started on port %hu", [udpSocket localPort]);
isRunning = YES;
// --------------------

My code to send broadcast:

NSString mysubnet = [[NSString alloc] initWithFormat: @"%i.%i.%i.255", myip1,myip2,myip3];
NSData
datatosend=[UDPString dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"udpSocket sendData %@ %i",mysubnet, port);
// [udpSocket beginReceiving:nil];
[ self.udpSocket  enableBroadcast:YES error:nil];

//[udpSocket joinMulticastGroup:mysubnet error:nil];
[ self.udpSocket sendData:datatosend toHost:mysubnet port:port withTimeout:-1 tag:0];
[ self.udpSocket enableBroadcast:NO error:nil];

I marked if i try to send data (not broadcasting) it sends but doesn't receive anything.

from cocoaasyncsocket.

fstanley avatar fstanley commented on May 27, 2024

Did you implement the didReceiveData delegate function? If so, does the function ever get accessed? If didReceiveData is not accessed, sniff your packets on the network to verify they are indeed being sent.

from cocoaasyncsocket.

horhe17 avatar horhe17 commented on May 27, 2024

i think i found the problem, i created several instances of one class and it made big problems, i also downloaded last version.

from cocoaasyncsocket.

stevek4444 avatar stevek4444 commented on May 27, 2024

Hi fstanley,

I am having a problem. When I try to use GCDAsyncUdpSocket via the simple code you provided above on April 11, 2012 to broadcast I keep getting a socket close error indicating that the network can't be reached.

Am I missing some setup code not shown?

Note: I am working on a project that needs to work on legacy OS. The iPad has iOS 8.1.3; XCODE 6.2.

On a related side note: My project needs to do a broadcast for part of its function but once a client is identified, the app works fine with normal GCDAsyncUdpSocket functions. I have a BSD sockets solution that generates the proper client response to the broadcast, however, I can't get my app to hear the client replies.

Here is my BSD code that works:

int service_port = 8023;
NSString *          rawMsg = @"ucget serial_number\r";
const char * theAddr;
NSData *            theData;

// Create a socket
int socketfd = socket( AF_INET, SOCK_DGRAM, 0 );

if(localIpAddress == nil) localIpAddress = [self getIPAddress];
theAddr = [localIpAddress UTF8String];

struct sockaddr_in ip4addr;
ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(service_port);
ip4addr.sin_addr.s_addr = inet_addr(theAddr);
memset(ip4addr.sin_zero, '\0', sizeof ip4addr.sin_zero);

int ret = bind(socketfd, (struct sockaddr*)&ip4addr, sizeof ip4addr);

// Enable broadcast
int broadcastEnable=1;
ret = setsockopt(socketfd, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));

//turn non-blocking
ret = fcntl(socketfd, F_SETFL, O_NONBLOCK);

//udp broadcast
struct sockaddr_in address; // Make an endpoint
address.sin_family = AF_INET;
address.sin_port = htons(service_port);
address.sin_addr.s_addr = inet_addr("255.255.255.255");
memset(address.sin_zero, '\0', sizeof address.sin_zero);

theData = [rawMsg dataUsingEncoding:NSUTF8StringEncoding];
assert(theData != nil);
ret = sendto(socketfd, [theData bytes], [theData length], 0, (const struct sockaddr *) &address, sizeof(address));

NSLog(@"sendto: bytes sent = %zd", ret);

ret = close(socketfd);
assert(ret == 0);

What I am looking for is code that will receive the data the client sends back resulting from the above BSD socket broadcast.

Thank you

from cocoaasyncsocket.

kingctan avatar kingctan commented on May 27, 2024

Error in send() function. when use multicast in ios version higher than 12.3.

from cocoaasyncsocket.

github-actions avatar github-actions commented on May 27, 2024

This issue has been marked as stale, it will be closed automatically if there is no further activity.

from cocoaasyncsocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.