recvfrom: Bad address, sendto: Address family not supported by protocol

[亡魂溺海] 提交于 2019-12-25 00:35:39

问题


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;

}

回答1:


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.)




回答2:


Quoting from Fedora 18 Beta recv/recvmsg/recv from main page:

ssize_t recv(int sockfd, void *buf, size_t len, int flgags);  
ssize_t recvfrom(int sockfd, void*buf, size_t len, int flags,  
       struct sockaddr *src_addr, socklen_t* addrlen); 

recvfrom should be adjusted from:

   recvfrom(socket_fd, buffer, 2, 0, (struct sockaddr*)&dest_addr, 
             (unsigned int *)sizeof(struct sockaddr_in) == -1)

To:

   recvfrom (socket_fd, buffer, sizeof(buffer), 0, 
            (struct sockaddr*)&dest_addr, &addrlen);
  • You dont initialize dest_addr
  • An array (eg _buffer_) is always a pointer type
  • Buffer must be FAR larger (dynamically sized) in a production program or buffer's fixed size will be a target for stack smashing (a security hole).


来源:https://stackoverflow.com/questions/13898207/recvfrom-bad-address-sendto-address-family-not-supported-by-protocol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!