2012-10-24 159 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