2012-10-24 157 views
2

我很努力地从iPhone发送任何东西,我跟着This Guide起初,我用cfSocketRef开始,因为我以为你通过改变协议将它们用于UDP和TCP,但我没有运气。在iOS中发送udp数据包6

所以这里是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 

回答

3

第一个问题:你忘了创建套接字:

if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { 
    NSLog(@"Failed to create socket, error=%s", strerror(errno)); 
} 

这部分不工作:

NSString *ip = @"localhost"; 
destination.sin_addr.s_addr = inet_addr([ip UTF8String]); 

inet_addr转换的点符号表示的IPv4地址字符串,如“127.0.0.1”。

要将主机名(例如“localhost”)转换为IP地址,必须使用gethostbynamegetaddrinfo(后者适用于IPv4和IPv6)。

检查返回值sendto时出现另一个错误:sendto返回成功情况下发送的字节数,错误情况下返回(-1)。因此,它应该是这样的:

if (sendto(sock, ...) == -1) { 
    NSLog(@"did not send, error=%s",strerror(errno)); 
} else { 
    NSLog(@"did send"); 
} 

检查的errno值会很快发现你的问题。如果你忘了创建套接字,该错误信息是

没发,对非套接字错误=插座操作

备注:

  • cfSocketRef是完全未使用在你的功能。
  • 为什么设置IP_MULTICAST_IF插座选项?据我所知,单播消息不需要这个,只能用于多播消息。
+0

谢谢...发送到127.0.0.1我得到'错误案例' - 还有什么明显的明显吗? – Andrew

+1

@安迪:请参阅我的更新回答。 –

+0

谢谢..我会在下班后给这个镜头。 – Andrew