Error Recvfrom Bad Address
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site recvfrom bad file descriptor About Us Learn more about Stack Overflow the company Business Learn more
Recvfrom Error 10054
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Recvfrom Error 10035
Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Simple messaging application…getting
Recvfrom Error Codes
errno 14: bad address up vote 4 down vote favorite I am writing a simple messaging application in C using sockets. When I use function recvfrom, it returns -1 and sets errno = 14 which is Bad address (which I am printing at the end). The strange thing is that it still reads from the socket and gets the correct message. That is, the application sendto errno 14 is working perfectly and as expected except for that error. My question is this: Why do you think I am getting this error? I cannot think of any reason. I was using inet_pton to set peer->sin_addr but I was getting the same error. // socket file descriptor to send data through int recv_sock_fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); // fill in the peer's address, loopback in this case struct sockaddr_in *peer = malloc(sizeof(struct sockaddr_in)); peer->sin_family = AF_INET; peer->sin_port = htons(11110); char *new = &(peer->sin_addr); new[0] = 127; new[1] = 0; new[2] = 0; new[3] = 1; for (int i = 0; i < 8; i++) { peer->sin_zero[i] = NULL; } bind(recv_sock_fd, peer, sizeof(struct sockaddr_in)); // check to see if the socket has any data...code removed char buff[32] = {0}; errno = 0; int bytes_received = recvfrom(recv_sock_fd, buff, sizeof(buff), NULL, (struct sockaddr *)peer, sizeof(struct sockaddr_in)); printf("Bytes recieved: %d: %d : %s\n", bytes_received, errno, strerror(errno)); c sockets errno share|improve this question asked Nov 30 '11 at 1:32 mtahmed 1,78732037 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted Look at the signature of recvfrom(2): ssize_t recvfrom(int sockfd, void *
an error code of 14, EFAULT "Bad Address" - UNIX Programming I'll post the code at the bottom of the post. Whenever I try to retreive a udp packet sent via recv error bad address broadcast I get the errorcode EFAULT(bad address). The same program sends out the sendto man packets without any complaints but it fails when retreiving, I'm lost as to why it would report this error. It returns recvfrom c the number of bytes correctly but returns the char* buffer as NULL. Any help would be appriciated and I wouldn't be supprised if I was just missing something simple with broacasting as I http://stackoverflow.com/questions/8320090/simple-messaging-application-getting-errno-14-bad-address am still learning some aspects of socket programiing.Thanks in advance to all whom ... Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode July 3rd,04:20 PM #1 recvfrom returns with an error code of 14, EFAULT "Bad Address" I'll post the code at the bottom of the post. Whenever I try to retreive http://www.justskins.com/forums/recvfrom-returns-with-an-97652.html a udp packet sent via broadcast I get the errorcode EFAULT(bad address). The same program sends out the packets without any complaints but it fails when retreiving, I'm lost as to why it would report this error. It returns the number of bytes correctly but returns the char* buffer as NULL. Any help would be appriciated and I wouldn't be supprised if I was just missing something simple with broacasting as I am still learning some aspects of socket programiing.Thanks in advance to all whom reply and here is the code: int Communications::GetBroadcastSocket() { int toReturn; int on = 1; // flag to turn on socket options memset(&broadcast, 0, sizeof(broadcast)); broadcast.sin_family = AF_INET; broadcast.sin_port = PORTNUM + 1; broadcast.sin_addr.s_addr = inet_addr(BCAST_ADDR); if((toReturn = socket(AF_INET, SOCK_DGRAM, 0)) == -1) return -1; if( SetNonBlocking(toReturn) < 0 || setsockopt(toReturn, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0 || setsockopt(toReturn, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0 || bind(toReturn, (sockaddr *) &broadcast, sizeof(broadcast)) < 0) { close(toReturn); return -1; } return toReturn; } Chris Ritchey Guest July 3rd,05:29 PM #2 Re: recvfrom returns with an error code of 14, EFAULT "Bad Address" In article <480de79d.0307030820.7b959e89posting.google.com >, Chris Ritchey
the server code when im receiving the data from the client.i tried passing the structure and an integer alone ,but for both i got the same error. for structures: client: struct Data { char data1[255]; char data2[255]; int val1; }; struct Data https://www.daniweb.com/programming/software-development/threads/244341/recv-bad-address-error myData = { "Let us C", "YPK", 101 } ; int ch = send(create_socket,&myData,sizeof(struct Data), 0); struct Data { char data1[255]; char data2[255]; int val1; }; void *dptr1; struct Data *dptr; struct Data *dptr = (struct Data*)(dptr1); printf("The Elements of structure \n"); printf("Book- %s Author- %s Code- %d\n",dptr->data1,dptr->data2,dptr->val1); for integer data: client: int a=5; int ch = send(create_socket,&a,sizeof(int), 0); server: void *ptr; int *iptr; int ch=recv(new_socket,ptr,sizeof(int),0); iptr = (int*)(ptr); printf("Value of a is :%d\n",*iptr); recvfrom error c karthik karthik.c 6 48 posts since Feb 2009 Community Member 2Contributors 3Replies 4Views 6 YearsDiscussion Span 6 Years Ago Last Post by programmersbook 0 programmersbook 17 6 Years Ago did you try: int i=0; int ch=recv(new_socket,(void*)&i,sizeof(int),0); printf("Value of a is :%d\n", i); 0 Discussion Starter karthik.c 6 6 Years Ago did you try: int i=0; int ch=recv(new_socket,(void*)&i,sizeof(int),0); printf("Value of a is :%d\n", i); thanx programmersbook ,it worked !! first of all sorry,as instead of error recvfrom bad posting this ques in C-Forum i posted in C++ ... i cant understand what is wrong in my approach of receiving the datas in the recv(),as i declare one void pointer and typecast it to approp pointer before dereferencing and it worked for quite a no of times and then it was not workin.. so,is my way of getting datas using pointers is wrong? if i go by ur way, can u plz xplain how come "&i" (where i is a local variable in server code) get the value from client side or how value is passed from client's send() to server's recv(). thanx !! 0 programmersbook 17 6 Years Ago You welcome! if you have it like this: void *ptr; and give it to send, send will complain, because behind ptr is no memory allocated! c This question has already been answered. Start a new discussion instead. Message Insert Code Snippet Alt+I Code Inline Code Link H1 H2 Preview Submit your Reply Alt+S Ask a Different Software Development Question Related Articles Address Book 20 replies Hi guys, i have to create a program that would store a persons full name, phone number, address, and postal code into a file named … Invalid instruction error, help? 16 replies I'm making a program to add/sub/mult numbers from bin/oct/dec/hex format and output in whatever