Objective-C 字符串编码问题

发布于 2024-07-29 01:41:58 字数 5967 浏览 6 评论 0原文

好的,我对 C 套接字相当陌生,但我只需要执行一些简单的 sendto() 和 recvfrom() 调用,即可使用多播套接字通过网络获取字符串。 在环顾四周并阅读了几本指南(包括 Beej 的指南)后,我发现下面的代码可以侦听通过多播套接字发送的消息(这正是我所需要的)。 该程序在 main 中运行良好,但是当我将其放入项目中其他位置的方法(即名为“listenForPackets”的方法)并尝试在运行循环上的另一个线程中运行它时,就会出现问题。 经过调试后,问题归结为变量“mc_addr_str”,该变量在 main 方法中被分配给等于 argv[1]。

#include <sys/types.h>  // for type definitions
#include <sys/socket.h> // for socket API calls
#include <netinet/in.h> // for address structs
#include <arpa/inet.h>  // for sockaddr_in
#include <stdio.h>      // for printf() and fprintf()
#include <stdlib.h>     // for atoi()
#include <string.h>     // for strlen()
#include <unistd.h>     // for close()

#define MAX_LEN  1024   // maximum receive string size
#define MIN_PORT 1024   // minimum port allowed
#define MAX_PORT 65535  // maximum port allowed

int main(int argc, char *argv[]) {

    int sock;                     // socket descriptor
    int flag_on = 1;              // socket option flag
    struct sockaddr_in mc_addr;   // socket address structure
    char recv_str[MAX_LEN+1];     // buffer to receive string
    int recv_len;                 // length of string received
    struct ip_mreq mc_req;        // multicast request structure
    char* mc_addr_str;            // multicast IP address
    unsigned short mc_port;       // multicast port
    struct sockaddr_in from_addr; // packet source
    unsigned int from_len;        // source addr length

    // validate number of arguments
    if (argc != 3) {
        fprintf(stderr, 
                "Usage: %s <Multicast IP> <Multicast Port>\n", 
                argv[0]);
        exit(1);
    }

    mc_addr_str = argv[1];      // arg 1: multicast ip address
    mc_port = atoi(argv[2]);    // arg 2: multicast port number

    // validate the port range
    if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
        fprintf(stderr, "Invalid port number argument %d.\n",
                mc_port);
        fprintf(stderr, "Valid range is between %d and %d.\n",
                MIN_PORT, MAX_PORT);
        exit(1);
    }

    // create socket to join multicast group on
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
        perror("socket() failed");
        exit(1);
    }

    // set reuse port to on to allow multiple binds per host
    if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on, sizeof(flag_on))) < 0) {
        perror("setsockopt() failed");
        exit(1);
    }

    // construct a multicast address structure
    memset(&mc_addr, 0, sizeof(mc_addr));
    mc_addr.sin_family      = AF_INET;
    mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    mc_addr.sin_port        = htons(mc_port);

    // bind multicast address to socket
    if ((bind(sock, (struct sockaddr *) &mc_addr, sizeof(mc_addr))) < 0) {
        perror("bind() failed");
        exit(1);
    }

    // construct an IGMP join request structure
    mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
    mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

    // send an ADD MEMBERSHIP message via setsockopt
    if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0) {
        perror("setsockopt() failed");
        exit(1);
    }

    for (;;) {          // loop forever

        // clear the receive buffers & structs
        memset(recv_str, 0, sizeof(recv_str));
        from_len = sizeof(from_addr);
        memset(&from_addr, 0, from_len);

        // block waiting to receive a packet
        if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0, (struct sockaddr*)&from_addr, &from_len)) < 0) {
            perror("recvfrom() failed");
            exit(1);
        }

        // output received string
        printf("Received %d bytes from %s: ", recv_len, inet_ntoa(from_addr.sin_addr));
        printf("%s", recv_str);
    }

    // send a DROP MEMBERSHIP message via setsockopt
    if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0) {
        perror("setsockopt() failed");
        exit(1);
    }

    close(sock);  
}

现在,通过另一个 SO 成员的帮助,我有一个方法可以将 IP 地址作为 NSString 返回给我(我也在程序中的其他地方使用它,所以我需要让它返回 NSString)。

-(NSString *)getIPAddress {
    NSString *address = @"error";
    struct ifaddrs *interfaces; // = NULL;
    struct ifaddrs *temp_addr; // = NULL;
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0)  
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL)  
        {
            if(temp_addr->ifa_addr->sa_family == AF_INET)
            {
                // Check if interface is en0 which is the wifi connection on the iPhone  
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])  
                {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }

    // Free memory
    freeifaddrs(interfaces); 
    return address; 
}

我以为我可以做一个简单的小转换

mc_addr_str = (someConversion)getIPAddress;

到目前为止我所拥有的是:

NSString *ipAddress = [[NSString alloc] initWithString:self.getIPAddress];

mc_addr_str = [ipAddress cStringUsingEncoding:[NSString defaultCStringEncoding]];

当我这样做时,程序会进行setsockopt调用,然后失败,错误代码为-1(我假设这是一个通用错误代码,让程序知道发生了不好的事情并且需要中止)。 另外,当我在上一个语句中分配 mc_addr_str 时,我不确定

warning: assignment discards qualifiers from pointer target type

现在的问题是从哪里产生的。 我在分配 mc_addr_str 期间是否出现转换错误,或者我是否使用了错误的编码? 任何意见表示赞赏!

Ok, I'm fairly new to C sockets but I just need to do some simple sendto() and recvfrom() calls to get a string out across a network, using multicast sockets. After looking around and reading several guides (including Beej's), I found the code below which does the job of listening for messages sent over a multicast socket (which is what I need). The program runs fine when it is in main, but my problem arises when I put it in a method (i.e. a method called "listenForPackets") elsewhere in my project and attempt to run it in another thread on a runloop. After debugging though, the problem comes down to the variable "mc_addr_str" which is assigned to equal argv[1] in the main method.

#include <sys/types.h>  // for type definitions
#include <sys/socket.h> // for socket API calls
#include <netinet/in.h> // for address structs
#include <arpa/inet.h>  // for sockaddr_in
#include <stdio.h>      // for printf() and fprintf()
#include <stdlib.h>     // for atoi()
#include <string.h>     // for strlen()
#include <unistd.h>     // for close()

#define MAX_LEN  1024   // maximum receive string size
#define MIN_PORT 1024   // minimum port allowed
#define MAX_PORT 65535  // maximum port allowed

int main(int argc, char *argv[]) {

    int sock;                     // socket descriptor
    int flag_on = 1;              // socket option flag
    struct sockaddr_in mc_addr;   // socket address structure
    char recv_str[MAX_LEN+1];     // buffer to receive string
    int recv_len;                 // length of string received
    struct ip_mreq mc_req;        // multicast request structure
    char* mc_addr_str;            // multicast IP address
    unsigned short mc_port;       // multicast port
    struct sockaddr_in from_addr; // packet source
    unsigned int from_len;        // source addr length

    // validate number of arguments
    if (argc != 3) {
        fprintf(stderr, 
                "Usage: %s <Multicast IP> <Multicast Port>\n", 
                argv[0]);
        exit(1);
    }

    mc_addr_str = argv[1];      // arg 1: multicast ip address
    mc_port = atoi(argv[2]);    // arg 2: multicast port number

    // validate the port range
    if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
        fprintf(stderr, "Invalid port number argument %d.\n",
                mc_port);
        fprintf(stderr, "Valid range is between %d and %d.\n",
                MIN_PORT, MAX_PORT);
        exit(1);
    }

    // create socket to join multicast group on
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
        perror("socket() failed");
        exit(1);
    }

    // set reuse port to on to allow multiple binds per host
    if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on, sizeof(flag_on))) < 0) {
        perror("setsockopt() failed");
        exit(1);
    }

    // construct a multicast address structure
    memset(&mc_addr, 0, sizeof(mc_addr));
    mc_addr.sin_family      = AF_INET;
    mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    mc_addr.sin_port        = htons(mc_port);

    // bind multicast address to socket
    if ((bind(sock, (struct sockaddr *) &mc_addr, sizeof(mc_addr))) < 0) {
        perror("bind() failed");
        exit(1);
    }

    // construct an IGMP join request structure
    mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
    mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

    // send an ADD MEMBERSHIP message via setsockopt
    if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0) {
        perror("setsockopt() failed");
        exit(1);
    }

    for (;;) {          // loop forever

        // clear the receive buffers & structs
        memset(recv_str, 0, sizeof(recv_str));
        from_len = sizeof(from_addr);
        memset(&from_addr, 0, from_len);

        // block waiting to receive a packet
        if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0, (struct sockaddr*)&from_addr, &from_len)) < 0) {
            perror("recvfrom() failed");
            exit(1);
        }

        // output received string
        printf("Received %d bytes from %s: ", recv_len, inet_ntoa(from_addr.sin_addr));
        printf("%s", recv_str);
    }

    // send a DROP MEMBERSHIP message via setsockopt
    if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0) {
        perror("setsockopt() failed");
        exit(1);
    }

    close(sock);  
}

Now, via help from another SO member, I have a method that will return the IPaddress to me as a NSString (I'm using it elsewhere in my program also, so I need to keep it returning NSString).

-(NSString *)getIPAddress {
    NSString *address = @"error";
    struct ifaddrs *interfaces; // = NULL;
    struct ifaddrs *temp_addr; // = NULL;
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0)  
    {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL)  
        {
            if(temp_addr->ifa_addr->sa_family == AF_INET)
            {
                // Check if interface is en0 which is the wifi connection on the iPhone  
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])  
                {
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }

    // Free memory
    freeifaddrs(interfaces); 
    return address; 
}

I thought I could just do a simple little conversion

mc_addr_str = (someConversion)getIPAddress;

What I have so far is:

NSString *ipAddress = [[NSString alloc] initWithString:self.getIPAddress];

mc_addr_str = [ipAddress cStringUsingEncoding:[NSString defaultCStringEncoding]];

When I do this, the program makes it to the setsockopt call and then fails with an error code of -1 (I assume that's a general error code that lets the program know something bad happened and needs to abort). Also, when I am assigning mc_addr_str in the previous statement, I get

warning: assignment discards qualifiers from pointer target type

I'm not sure where my problem is arising from now. Do I have a casting error during the assignment to mc_addr_str or did I use the wrong encoding? Any input is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-08-05 01:41:58

从 getIPAddresss() 获得的地址是接口的地址,而不是多播组的地址。 这是您的示例中使用地址的方式,您能发现问题吗?

// construct an IGMP join request structure
mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
mc_req.imr_interface.s_addr = htonl(INADDR_ANY)

您将多播地址设置为 mc_addr_str 中的地址,该地址保存接口地址。 这看起来不太对吧? ;)

您应该执行以下操作:

// construct an IGMP join request structure
mc_req.imr_multiaddr.s_addr = inet_addr("225.0.0.37");
mc_req.imr_interface.s_addr = inet_addr(mc_addr_str);

从 224.0.0.0 到 239.255.255.255 的地址范围是为多播地址保留的。 但不要使用 224.0.0.0 到 224.0.0.255,因为这些是为多播路由信息保留的。 如果您使用任何其他地址,您的 setsockopt() 将会像您的示例中那样失败。

The address you get from getIPAddresss() is the address of the interface, not of the multicast group. Here is how the address is being used in your example, can you spot the problem?

// construct an IGMP join request structure
mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
mc_req.imr_interface.s_addr = htonl(INADDR_ANY)

You set the multicast address to the address in mc_addr_str, which holds the interface address. That doesn't seem right does it? ;)

Here's what you should do:

// construct an IGMP join request structure
mc_req.imr_multiaddr.s_addr = inet_addr("225.0.0.37");
mc_req.imr_interface.s_addr = inet_addr(mc_addr_str);

The address range from 224.0.0.0 to 239.255.255.255 is reserved for multicast addresses. Don't use 224.0.0.0 to 224.0.0.255 though since these are reserved for multicast routing information. If you use any other addresses your setsockopt() will fail the way it does in your example.

白馒头 2024-08-05 01:41:58

cStringUsingEncoding 返回一个 const char *,而您的 mc_add_str 是一个 char *。 这就是编译器发出警告的原因。

cStringUsingEncoding returns a const char *, and your mc_add_str is a char *. That's why the compiler is issuing a warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文