Comments (10)
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.
Thank you, i will try your code. And how do u receive answer?
from cocoaasyncsocket.
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.
send data to exact IP works, but doesnt work to broadcast IP
from cocoaasyncsocket.
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.
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.
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.
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.
Error in send() function. when use multicast in ios version higher than 12.3.
from cocoaasyncsocket.
This issue has been marked as stale, it will be closed automatically if there is no further activity.
from cocoaasyncsocket.
Related Issues (20)
- If two apps use the same port, I get an error. HOT 1
- Is this still maintained? HOT 1
- Is this library supported for iOS 13+ ? HOT 2
- Error Domain=NSPOSIXErrorDomain Code=60 "Operation timed out" HOT 1
- [!] Error installing CocoaAsyncSocket [!] /usr/bin/git clone https://github.com/robbiehanson/CocoaAsyncSocket.git /var/folders/44/pdz2cl0561z4jh2377tl293h0000gp/T/d20221207-3976-gkz985 --template= --single-branch --depth 1 --branch 7.6.5 HOT 1
- π¦ ALL IN ONEπ
- crashed on iOS 16.1, SIGTRAP HOT 2
- App crash in Xcode 14 and iOS 16 HOT 6
- how to creat a unix domain socket on iOS device HOT 1
- the app references non-public selectors in cocoaAsyncSocket HOT 1
- [GCDAsyncSocket closeWithError:] crash, iOS16 HOT 4
- Not able to build the framework after upgrading Xcode to 14.3 HOT 1
- Not work with bitcode enable HOT 2
- GCDAsyncSocket with local ip address changed HOT 5
- socket gets disconnected with nil error, as soon as didAccept delegate is called HOT 2
- Privacy Manifest HOT 6
- Is there any plan to update TLS API to Network.framework? HOT 2
- crash about (null) in @objc TcpClient.socket(_:didRead:withTag:) () in Xcode HOT 1
- Crash: Thread 8: EXC_BAD_ACCESS (code=1, address=0x555555555555555d) HOT 5
- Increase macOS deployment target HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cocoaasyncsocket.