View previous topic :: View next topic |
Author |
Message |
ivan47 n00b
Joined: 27 Jan 2008 Posts: 7
|
Posted: Thu Feb 21, 2008 10:28 pm Post subject: unable to listen for udp packets at specific ip [SOLVED] |
|
|
Hi all, I'm trying to run a UDP-listener, but also want to set my IP, rather than have it automatically filled. I'm getting the following error when I try to set the IP manually - "bind: cannot assign requested address". This depends on setting the s_addr struct. I've commented out a couple of the other things I've tried.
Code: |
int sockfd;
struct sockaddr_in my_addr; //my address info
struct sockaddr_in their_addr; // connector's address information
socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];
// create a socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
/******************** PROBLEM CODE ***********************/
my_addr.sin_addr.s_addr = inet_addr("192.168.0.125");
//OTHER THINGS I'VE TRIED:
// inet_pton(AF_INET,"192.168.0.125",&my_addr.sin_addr.s_addr);
// though the following doesn't error, it doesn't accomplish what i want since
//i want to set the listener to listen for packets sent to 192.168.0.125
// my_addr.sin_addr.s_addr=INADDR_ANY;
/********************END PROBLEM CODE ***********************/
printf("hex s_addr = %x\n",my_addr.sin_addr.s_addr);
memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
// bind the socket to the port
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof their_addr;
if ((numbytes = recvfrom(sockfd, &buf, MAXBUFLEN-1 , 0,
(struct sockaddr *)&their_addr, &addr_len)) == -1) {
perror("recvfrom");
exit(1);
}
buf[numbytes] = '\0';
close(sockfd);
|
Help would be much appreciated. _________________ --ivan
Last edited by ivan47 on Fri Feb 22, 2008 11:54 pm; edited 1 time in total |
|
Back to top |
|
|
UberLord Retired Dev
Joined: 18 Sep 2003 Posts: 6835 Location: Blighty
|
|
Back to top |
|
|
ivan47 n00b
Joined: 27 Jan 2008 Posts: 7
|
Posted: Fri Feb 22, 2008 9:12 pm Post subject: |
|
|
thanks, but it didn't work either. i tried the inet_aton with setting SO_REUSEADDR. any other ideas?
i should be able to set it to whatever ip i want, right? it shouldn't fail to bind the socket if i do something like:
Code: |
inet_aton("111.111.111.111",&my_addr.sin_addr)
|
That's not what I did, I did 192.168.0.130, but still, even with a garbage address it shouldn't break, right? _________________ --ivan |
|
Back to top |
|
|
ivan47 n00b
Joined: 27 Jan 2008 Posts: 7
|
Posted: Fri Feb 22, 2008 11:54 pm Post subject: |
|
|
So, as it turns out, i have no idea what i'm doing. I guess your network card needs to be set to the IP address you're trying to listen at...who woulda guessed.
Code: |
ifconfig(IPADDRESS)
then inet_aton(IPADDRESS,&my_addr.sin_addr) |
now it works... _________________ --ivan |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|