I want to send OSC messages from iphone to another programme (max/msp) by creating and connecting to a udp socket. this works from the iphone simulator, i.e. when both apps are running on the same computer but not when i install the app on the phone itself.
I think the problem could be with specifying the IP of the remote computer. I am using the sockaddr_in struct to specify IP and port info. when i run the code in the simulator it is fine to specify the IP as INADDR_ANY:
sin_addr.s_addr = INADDR_ANY;
when i run it on the device i'm trying to convert my IP into a hexidecimal number and specifying that instead of INADDR_ANY. This doesn't work for either the simulator or the device.
The console shows that the the socket is connecting and sending data fine but the remote programme (max/msp) doesn't receive any data at all.
I have tried importing the right frameworks so that it should work on both device and simulator.
the full code follows:
import "UDPSocketCreate.h"
#implementation UDPSocketCreate
-(id)init
{
in_addr_t myAddress = 0xC0A80145;
if(self =[super init])
{
//addr is an instance variable of type struct sockaddr_in
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = PF_INET;
addr.sin_port = htons(3333);
addr.sin_addr.s_addr = myAddress;INADDR_ANY
connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
OSC_initBuffer(&myOSCBuff, sizeof(packetBuffer), packetBuffer);
NSString *address = #"/test";
const char *utf8Address = [address UTF8String];
int addressResult = OSC_writeAddress(&myOSCBuff, (char*)utf8Address);
}
return self;
}
CFSocketRef udpSocket;
// this method is called from app delegate after init
-(void)createUDPSocketRef
{
udpSocket = CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketWriteCallBack, myCallBack, NULL);
if(udpSocket == NULL)
{
NSLog(#"socket create failed");
return;
}
CFRunLoopSourceRef runLoopSrceRef = CFSocketCreateRunLoopSource(NULL, udpSocket, 1);
CFRunLoopRef rl = CFRunLoopGetCurrent();
CFRunLoopAddSource(rl, runLoopSrceRef, kCFRunLoopCommonModes);
}
// pressing a button on the UI triggers this method
-(void)bang
{
int myInt = 1;
int writeRestult = OSC_writeIntArg(&myOSCBuff, myInt);
int buffDoneResult;
if (buffDoneResult = OSC_isBufferDone(&myOSCBuff))
{
NSLog(#"valid message in buff");
char *pack = OSC_getPacket(&myOSCBuff);
int packSize = OSC_packetSize(&myOSCBuff);
CFDataRef OSCPacketWithAddressTest = CFDataCreate(NULL, pack, packSize);
CFSocketError sendError = CFSocketSendData(udpSocket, connectAddr, OSCPacketWithAddressTest, 30);
NSLog(#"send error: %d", sendError);
}
OSC_resetBuffer(&myOSCBuff);
NSString *address = #"/test";
const char *utf8Address = [address UTF8String];
int addressResult = OSC_writeAddress(&myOSCBuff, (char*)utf8Address);
}
#end
any help would be greatly appreciated
Change;
in_addr_t myAddress = 0xC0A80145
to
in_addr_t myAddress = inet_addr("192.168.1.2");
or whatever that IP is.
S.
Unless I misunderstood you are trying to connect with INADDR_ANY as server address. INADDR_ANY is only for listening server to tell the IP stack that it wants to listen on any network interface (versus a specific interface on a multi-homed machine.) The client needs explicit server address of the server to send packets to. Look into inet_pton function for how to convert IP address from character string to network representation.
Related
I'm trying to retrieve the server IP address using gethostbyname(required from me)
here is the part of the client application for connecting to the server
struct hostent *SN = gethostbyname(argv[1]);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);
server_addr.sin_addr.s_addr = htonl(*SN->h_addr); //I have a doubt about this line
int serverfd=0;
serverfd = connect(SocketD, (struct sockaddr*) &server_addr,
sizeof(server_addr));
where argv[1] is the name of the server, currently localhost.
I tried also this line of code
server_addr.sin_addr = *(struct in_addr*)SN->h_addr;
Could anyone please tell me what is wrong with my code?
I am receiving multiple messages through a socket using:
result = recvmmsg(socket_, &messages_[0], MAX_NUM_MSG, MSG_WAITFORONE, NULL);
And I want get the source address and port, but I am getting an error in the struct assignment when I try:
msg = &messages_[0];
***struct sockaddr *src = &msg->msg_hdr.msg_name;***
srcport = ntohs(src->sin_port);
srcaddr = ntohl(src->sin_addr.s_addr);
invalid conversion from ‘void**’ to ‘sockaddr*'
The recvmmsg system call is an extension of recvmsg. As described in recvmsg: The msg_name field points to a caller-allocated buffer that is used to return the source address
That means you should preallocate memory space for msg_name by yourself, and also you should specify msg_namelen, please try:
sockaddr_in addrs[MAX_NUM_MSG];
for (int i = 0; i < MAX_NUM_MSG; ++i) {
messages_[i].msg_hdr.msg_name = &addrs[i];
messages_[i].msg_hdr.msg_namelen = sizeof(sockaddr_in);
}
So that you can access address when you have at least one message by doing (Remember to use sockaddr_in but not sockaddr):
struct sockaddr_in *src = messages_[0].msg_hdr.msg_name;
I use following code to print all interface and it's mac address
- ( void )interfaceInfo{
int mib[6];
size_t len;
char *buf;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST;
char name[128];
memset(name, 0, sizeof(name));
for (int i=1; i<20; i ++) {
if (if_indextoname(i, name)) {
printf("%s ",name);
}else{
continue;
}
if ((mib[5] = if_nametoindex(name)) == 0) {
printf("Error: if_nametoindex error\n");
return NULL;
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1\n");
return NULL;
}
if ((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. error!\n");
return NULL;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
free(buf);
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *macString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
printf(" %s\n",[macString cStringUsingEncoding:NSUTF8StringEncoding]);
free(buf);
}
return nil;
}
I run the code on iPhone 5 and the output is
lo0 00:00:00:00:00:00
pdp_ip0 00:00:00:00:00:00
pdp_ip1 00:00:00:00:00:00
pdp_ip2 00:00:00:00:00:00
pdp_ip3 00:00:00:00:00:00
ap1 EA:8D:28:44:32:2F
en0 E8:8D:28:44:32:2F
en1 EA:8D:28:44:32:31
awdl0 4A:79:85:44:5B:4D
//I faked parts data
I wanna know what's the pdp_ip? and what's the ap1,en1?
I find out en0 is wifi hardware mac address
Does ap1 and en1 is virtual interface?
Thank you!
ap1, en0, en1 are names of the interfaces on iOS as well as on Mac. If you type in Terminal on Mac ifconfig you would get the same, en0, en1, etc.
pdp_ip interfaces are those that are used for 3G and cellular data, while ap1 is used to represent currently active data connection, Wi-Fi, cellular data or bluetooth.
lo = localhost
en = ethernet
ap = Probably for access point (if you are acting as a wifi host)
pdp_ip = maybe PDS data packet? PDS is the Phone Data Service, the data portion of GSM. Since there are four, I might postulate that PDS has the capability to offer four discrete channels.
From my research it appears (i.e. I haven't found any confirming documentation) that if the code above returns more than one "awdl0" entry then Wi-Fi is enabled. Similarly, more than one "pdp_ip0" entry indicates that cellular data is enabled. Other libraries (most notably Reachability) can then be used to indicate that a data connection has been made using either of the above.
I am a newbie to objective C networking. I have put together the code below for a simple WOL application. I can successfully create a socket, and then set an address (I only am able to set the local machine address but thats another question).
However when trying to send the data using CFSocketSendData, it does not send the data(Data not sent message displayed).
Am I using CFSocketSendData correctly, or is there a problem elsewhere?
Any help will be greatly appreciated, thanks.
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT); //port
inet_aton(IP, &addr.sin_addr);//IP is the network IP of the machine e.g 192.168.0.2
NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];
if (CFSocketSetAddress(WOLsocket, (CFDataRef)address) != kCFSocketSuccess){
NSLog(#"Address could not be set!");
}
else{
NSLog(#"Address set");
char ethadd []= "helloworld";
CFDataRef Data = CFDataCreate(NULL, (const UInt8*)ethadd, sizeof(ethadd));
if (CFSocketSendData(WOLsocket, NULL, Data, 0) < 0){
NSLog(#"Data could not be sent!");
}
else NSLog(#"Data Sent");
}
You didn't include the CFSocketCreate(...) code in your snippet, but I assume that you chose the defaults, the PF_INET family and the TCP protocol.
With the TCP protocol, setting the address is not enough, you need to establish a
connection to your destination endpoint.
First call the function...
CFSocketConnectToAddress (WOLsocket, address, 10.0);
then send your data...
CFSocketSendData(WOLsocket, NULL, Data, 0)
I have the following code that should retrive all DNS A records for a URL. When I call this code from the UI, it works fine and returns all the A records, but, when this code is called from some other async thread it always returns only one IP (one A record), Any ideas why?
this is the code:
Boolean result;
CFHostRef hostRef = NULL;
CFArrayRef addresses = NULL;
NSString *hostname = [NSString stringWithCString:server];
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
if (result == TRUE) {
addresses = CFHostGetAddressing(hostRef, &result);
}
}
if (result == TRUE)
{
NSLog(#"ResolveDNS - Resolved");
for (CFIndex count = 0; count < CFArrayGetCount(addresses); count++)
{
char addr[256];
struct sockaddr_in *sa = (struct sockaddr_in *)
CFDataGetBytePtr((CFDataRef)CFArrayGetValueAtIndex(addresses, count));
// inet_ntop will correctly display both IPv4 and IPv6 addresses
if (inet_ntop(sa->sin_family, &sa->sin_addr, addr, sizeof(addr)))
printf("%s:%d \n", addr, ntohs(sa->sin_port));
[ipTmpArray addObject:[NSString stringWithCString:addr]];
}
the same occurs on all my iOS devices (all on 4.2 or 4.3), but not on my Simulator.
And if I launch again the same method I sometime get more IP, sometime even all of them.
I suspect it's due to iOS, some kind of Memory optimisation when they record the previous resolutions.