admin 管理员组文章数量: 1086019
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
|
1 Answer
Reset to default 2bind
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.
本文标签: cSend data from a socket to its own bound IP addressStack Overflow
版权声明:本文标题:c - Send data from a socket to its own bound IP address - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744012832a2518468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
htonl(ip)
is wrong. You can't pass achar*
string tohtonl()
as it expects an integer. Useinet_addr()
instead, eg:sock_addr.sin_addr.s_addr = inet_addr(ip);
Also, yourget_sock_name()
function is notreturn
'ing anything, which is undefined behavior. Your compiler should be warning you about that. – Remy Lebeau Commented Mar 29 at 19:24