아이폰에서 보낼 항목을 얻으려고 애 쓰고 있습니다. This Guide 처음에는 프로토콜을 변경하여 UDP 및 TCP에 사용한다고 생각 했으므로 cfSocketRef로 시작했지만 운이 없었습니다. .iOS 6에서 udp 패킷 보내기
다음은 BSD 소켓의 코드입니다. 아무것도 보내는 것 같지 않습니다. localhost : port에서 대기중인 java 소켓 서버가 있습니다.
아이디어가 있으십니까? 또는 작동하는 가이드/샘플 xcode 프로젝트 일 수 있습니다.
#import "ViewController.h"
#include <CFNetwork/CFNetwork.h> //temp //dont leave here put it in a header
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@interface ViewController()
@end
@implementation ViewController
static void socketCallback(CFSocketRef cfSocket, CFSocketCallBackType
type, CFDataRef address, const void *data, void *userInfo)
{
NSLog(@"socketCallback called");
}
//
- (void)viewDidLoad
{
[super viewDidLoad];
int sock = 0; /// ?
unsigned int echolen;
NSLog(@"starting udp testing");
cfSocketRef = CFSocketCreate(/*CFAllocatorRef allocator*/ NULL,
/*SInt32 protocolFamily*/ PF_INET,
/*SInt32 socketType*/ SOCK_DGRAM,
/*SInt32 protocol*/ IPPROTO_UDP,
/*CFOptionFlags callBackTypes*/ kCFSocketAcceptCallBack | kCFSocketDataCallBack,
/*CFSocketCallBack callout*/ (CFSocketCallBack)socketCallback,
/*const CFSocketContext *context*/ NULL);
struct sockaddr_in destination;
memset(&destination, 0, sizeof(struct sockaddr_in));
destination.sin_len = sizeof(struct sockaddr_in);
destination.sin_family = AF_INET;
NSString *ip = @"localhost";
destination.sin_addr.s_addr = inet_addr([ip UTF8String]);
destination.sin_port = htons(33033); //port
NSString *msg = @"message sent from iPhone";
/* server port */
setsockopt(sock,
IPPROTO_IP,
IP_MULTICAST_IF,
&destination,
sizeof(destination));
const char *cmsg = [msg UTF8String];
echolen = strlen(cmsg);
if (sendto(sock,
cmsg,
echolen,
0,
(struct sockaddr *) &destination,
sizeof(destination)) != echolen)
{
NSLog(@"did send");
}
else
{
NSLog(@"did not send");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
감사합니다 ... 127.0.0.1로 전송됩니다. '오류가 발생합니다.'- 눈에 띄게 다른 것이 있습니까? – Andrew
@Andy : 업데이트 된 답변을 참조하십시오. –
고맙습니다. 일을 끝내고 나서이 기회를 제공 할 것입니다. – Andrew