recvfrom: Bad address, sendto: Address family not supported by protocol
You need to pass a valid pointer to recvfrom. (unsigned int *)sizeof(struct sockaddr_in)
is not a valid pointer.
Change
if(recvfrom(socket_fd, buffer, 2, 0, (struct sockaddr*)&dest_addr,
(unsigned int *)sizeof(struct sockaddr_in) == -1)
to e.g.
socklen_t len = sizeof dest_addr;
if(recvfrom(socket_fd, buffer, 2, 0,
(struct sockaddr*)&dest_addr, &len) == -1)
You are also constructing an invalid array that you send, Your array has a length of 1, but you tell sendto/recvfrom that it has a length of 2. So change
char buffer[1] = "s";
to
char buffer[] = "s";
(The string "s" is the character 's' and a 0 byte, so buffer
will have length 2 in the latter case.)
flumingo
I just love to explore the big world of computer science in every way since it has so many facetts and a big diversity - everybody is able to find an interesting spot.
Updated on June 05, 2022Comments
-
flumingo about 2 years
i'm trying to implement a little UDP-Server/Client Application in C and got two errors on Server-side: recvfrom: Bad address && sendto: Address family not supported by protocol. I searched for the mistake and googled for answers but, unfortunately, they wasn't really helpfully... maybe i'm casting a parameter in a wrong way and don't get it. I hope you can give me a hint :).
#include <unistd.h> #include <ctype.h> #include <sys/socket.h> #include <sys/types.h> #include <arpa/inet.h> #include <errno.h> #define BUFFSIZE 256 #define IP "127.0.0.1" #define PORT 7755 int main(void){ int socket_fd = socket(AF_INET, SOCK_DGRAM, 0); char buffer[1] = "s"; struct sockaddr_in src_addr; struct sockaddr_in dest_addr; src_addr.sin_family = AF_INET; src_addr.sin_port = htons(PORT); src_addr.sin_addr.s_addr = inet_addr(IP); if(socket_fd==-1) perror("socket"); if(bind(socket_fd, (struct sockaddr*)&src_addr, sizeof(src_addr))==-1) perror("bind"); if(recvfrom(socket_fd, buffer, 2, 0, (struct sockaddr*)&dest_addr, (unsigned int *)sizeof(struct sockaddr_in))==-1) perror("recvfrom"); if(sendto(socket_fd, buffer, 2, 0,(struct sockaddr*)&dest_addr, sizeof(dest_addr))==-1) perror("sendto"); if(close(socket_fd)==-1) perror("close"); return 0; }