LoginSignup
0
0

More than 1 year has passed since last update.

sendmsg: Invalid argument

Last updated at Posted at 2021-08-08

概要

UDPでsendmsgをしようとした際に以下のエラーが出力されたのでその対応メモ

sendmsg: Invalid argument

詳細

以下はエラーが発生した際のsendmsgする関数の一部抜粋です。

void sender(struct addrinfo *res_local, struct addrinfo *res_remote, unsigned int usecs) {

    int sock;
    unsigned char buf[BUF_LEN] = {"0"};
    unsigned char buf2[BUF_LEN] = {"0"};

    sock = socket(res_remote->ai_family, res_remote->ai_socktype, res_remote->ai_protocol);

    struct iovec iov[2];
    iov[0].iov_base = (void*) buf;
    iov[0].iov_len = sizeof(buf);
    iov[1].iov_base = (void*) buf2;
    iov[1].iov_len = sizeof(buf2);

    struct msghdr msg;

    msg.msg_name = (void *) res_remote->ai_addr;
    msg.msg_namelen = res_remote->ai_addrlen;

    msg.msg_iov = iov;
    msg.msg_iovlen = 2;

    while (1) {
        errno = 0;
        int k = sendmsg(sock, &msg, 0); 
        if (k < 1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
                perror("sendmsg EAGAIN?");
            } else {
                perror("sendmsg");
                return;
            }   
        }   
        fprintf(stderr,".");
        usleep(usecs);
    }   

    close(sock);

}

msghdr構造体を宣言時に初期化をすれば、「Invalid argument」が出なくなったことを確認しました。

struct msghdr msg = {};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0