最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Send data from a socket to its own bound IP address - Stack Overflow

programmeradmin4浏览0评论

Is it possible to send data from a socket to it's own bound address without getting the bound address of the socket using getsockname?

int create_socket() {
    // Create UDP Socket
    const int sockfd = socket(AF_INET, SOCK_DGRAM, 0)
    if (sock_fd == -1) {
        perror("Can't create socket");
        exit(EXIT_FAILURE);
    }

    return sock_fd;
}

void configure_socket(const int sock_fd, const char* const ip, const unsigned short port) {
    struct sockaddr_in sock_addr;
    memset(&sock_addr, 0, sizeof sock_addr);
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_addr.s_addr = htonl(ip);
    sock_addr.sin_port = htons(port);

    // Bind socket
    if ((bind(sockfd, (struct sockaddr*)&sock_addr, sizeof sock_addr)) == -1) {
        close(sockfd);
        perror("Can't bind");
        exit(EXIT_FAILURE);
    }
}

sockaddr_in get_sock_name(const int sock_fd) {
    struct sockaddr_in remote_addr;
    socklen_t addr_len = sizeof remote_addr;
    getsockname(sock_fd, (struct sockaddr*)&remote_addr, &addr_len);
}

void send_data(const int sock_fd, const char* const buf, const int buf_len, const struct sockaddr_in* remote_addr, socklen_t addr_len) {
    sendto(sock_fd, buf, buf_len, 0, (const struct sockaddr*)&remote_addr, addr_len);   
}

void send_data_to_self(const int sock_fd, const char* const buf, const int buf_len) {
    // Is there a more direct way of sending data to self, 
    // instead of first getting socket bound address and then sending data
    const struct sockaddr_in remote_addr = get_sock_name(sock_fd);
    sendto(sock_fd, buf, buf_len, 0, (const struct sockaddr*)&remote_addr, sizeof remote_addr); 
}

void recv_data(const int sock_fd, char* const buf, const int buf_len) {
    recvfrom(sock_fd, buf, buf_len, 0, NULL, NULL);
}

int main() {
    const int sock_fd = create_socket();
    
    configure_socket(sock_fd, "192.168.1.1", 23456);

    char send_buf[64 * 1024];
    send_data_to_self(sock_fd, send_buf, 1024);

    char recv_buf[64 * 1024];
    recv_data(sock_fd, recv_buf, sizeof recvbuf);

    return EXIT_SUCCESS;
}

Is it possible to send data from a socket to it's own bound address without getting the bound address of the socket using getsockname?

int create_socket() {
    // Create UDP Socket
    const int sockfd = socket(AF_INET, SOCK_DGRAM, 0)
    if (sock_fd == -1) {
        perror("Can't create socket");
        exit(EXIT_FAILURE);
    }

    return sock_fd;
}

void configure_socket(const int sock_fd, const char* const ip, const unsigned short port) {
    struct sockaddr_in sock_addr;
    memset(&sock_addr, 0, sizeof sock_addr);
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_addr.s_addr = htonl(ip);
    sock_addr.sin_port = htons(port);

    // Bind socket
    if ((bind(sockfd, (struct sockaddr*)&sock_addr, sizeof sock_addr)) == -1) {
        close(sockfd);
        perror("Can't bind");
        exit(EXIT_FAILURE);
    }
}

sockaddr_in get_sock_name(const int sock_fd) {
    struct sockaddr_in remote_addr;
    socklen_t addr_len = sizeof remote_addr;
    getsockname(sock_fd, (struct sockaddr*)&remote_addr, &addr_len);
}

void send_data(const int sock_fd, const char* const buf, const int buf_len, const struct sockaddr_in* remote_addr, socklen_t addr_len) {
    sendto(sock_fd, buf, buf_len, 0, (const struct sockaddr*)&remote_addr, addr_len);   
}

void send_data_to_self(const int sock_fd, const char* const buf, const int buf_len) {
    // Is there a more direct way of sending data to self, 
    // instead of first getting socket bound address and then sending data
    const struct sockaddr_in remote_addr = get_sock_name(sock_fd);
    sendto(sock_fd, buf, buf_len, 0, (const struct sockaddr*)&remote_addr, sizeof remote_addr); 
}

void recv_data(const int sock_fd, char* const buf, const int buf_len) {
    recvfrom(sock_fd, buf, buf_len, 0, NULL, NULL);
}

int main() {
    const int sock_fd = create_socket();
    
    configure_socket(sock_fd, "192.168.1.1", 23456);

    char send_buf[64 * 1024];
    send_data_to_self(sock_fd, send_buf, 1024);

    char recv_buf[64 * 1024];
    recv_data(sock_fd, recv_buf, sizeof recvbuf);

    return EXIT_SUCCESS;
}
Share Improve this question asked Mar 29 at 16:07 HarryHarry 3,2481 gold badge24 silver badges46 bronze badges 1
  • 2 htonl(ip) is wrong. You can't pass a char* string to htonl() as it expects an integer. Use inet_addr() instead, eg: sock_addr.sin_addr.s_addr = inet_addr(ip); Also, your get_sock_name() function is not return'ing anything, which is undefined behavior. Your compiler should be warning you about that. – Remy Lebeau Commented Mar 29 at 19:24
Add a comment  | 

1 Answer 1

Reset to default 2

bind sets the IP address used to receive packets on the socket. The interface used to send a packet is determined by the routing table. Packets destined for the same machine will use the loopback interface. Since the dest IP is the same as the bound value, it will be received by the socket.

I am not sure why you want to call getsockname for the socket which you just called bind, but it is a valid way to get the (same) address.

Edit: After fixing the various typos, I did receive 1024 bytes using your code.

发布评论

评论列表(0)

  1. 暂无评论