添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am always getting no bytes sent, with an errno of 22 (EINVAL, Invalid Argument) with this code. The destination_host is set elsewhere and known to be valid, so I really don't see what is going on. MAXMSGSIZE is 1000. No errors, or warnings. I am compiling with -Wall -Werror -pedantic

char *data_rec;
u_int data_len;
int sockfd;
uint16_t *ns;
struct sockaddr_in address;
struct sockaddr *addr;
char *ip;
int i;
int errno;
int bytes_sent;
data_len = MAXMSGSIZE;
data_rec = malloc(sizeof(char)*MAXMSGSIZE);
ns = malloc(MAXMSGSIZE*sizeof(uint16_t));
ip = malloc(MAXMSGSIZE*sizeof(char));
data_rec = "some random test stuff";
sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(sockfd<0) {
    printf("socket() failed\n");
inet_ntop(AF_INET,destination_host->h_addr,ip,MAXMSGSIZE);
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(theirPort);
address.sin_addr.s_addr = (unsigned long)ip;
addr = (struct sockaddr*)&address;
bind(sockfd,addr,sizeof(address));
/*Convert the message to uint16_t*/
for(i=0; i<MAXMSGSIZE; i++) {
    ns[i] = htons(data_rec[i]);
/* send the message */
bytes_sent = sendto(sockfd, ns, data_len, MSG_DONTWAIT, addr, sizeof(addr));
if(bytes_sent == -1) {
    printf("Error sending: %i\n",errno);
                Also, it seems that you declare errno and never assign to it. Maybe you wanted to write extern int errno?
– vadipp
                Dec 10, 2015 at 9:02

You're giving the wrong size for the address. addr is really a struct sockaddr_in, not a struct sockaddr.

Change the last parameter of sendto to sizeof(address)

Odd how this worked. An example I was following used the size of the sockaddr_in struct and worked fine. – dc. Oct 24, 2010 at 19:17

inet_ntop probably isn't what you want - it converts from network (i.e. wire) format into presentation format (i.e. "1.2.3.4"). Try:

address.sin_addr.s_addr = *((unsigned long *)destination_host->h_addr);
                In fairness, nos's answer is more likely to fix the error you're seeing, but my answer explains your next problem: why the receiver isn't seeing anything :)
– SimonJ
                Oct 24, 2010 at 1:46
                It needs to be sizeof(address), doesn't it?  And that's what the accepted answer says.  The trouble with sizeof(*addr) is that it is sizeof(struct sockaddr), but the variable is actually a struct sockaddr_in.  The sockets interface is pretty peculiar, especially the way it handles different types and sizes of addresses.
– Jonathan Leffler
                Dec 13, 2016 at 5:45
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.