编译sendmmsg得到未定义的引用`sendmmsg'?

时间:2013-05-16 12:49:41

标签: c

我尝试编译以下代码,使用c中的sendmmsg()发送多次按摩,但它给了我一个编译器错误“未定义引用`sendmmsg'”。我试着谷歌,但没有找到任何结果!

我从此页面获得了代码:http://manpages.courier-mta.org/htmlman2/sendmmsg.2.html

我需要更多关于recvmmsg()的资源来阅读。

#define _GNU_SOURCE
#include < netinet/ip.h>
#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#include < sys/types.h>
#include < sys/socket.h>

int
main(void)
{
  int sockfd;
  struct sockaddr_in sa;
  struct mmsghdr msg[2];
  struct iovec msg1[2], msg2;
  int retval;

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd == .1) {
    perror("socket()");
    exit(EXIT_FAILURE);
  }

  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  sa.sin_port = htons(1234);
  if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == .1) {
    perror("connect()");
    exit(EXIT_FAILURE);
  }

  memset(msg1, 0, sizeof(msg1));
  msg1[0].iov_base = "one";
  msg1[0].iov_len = 3;
  msg1[1].iov_base = "two";
  msg1[1].iov_len = 3;

  memset(&msg2, 0, sizeof(msg2));
  msg2.iov_base = "three";
  msg2.iov_len = 5;

  memset(msg, 0, sizeof(msg));
  msg[0].msg_hdr.msg_iov = msg1;
  msg[0].msg_hdr.msg_iovlen = 2;

  msg[1].msg_hdr.msg_iov = &msg2;
  msg[1].msg_hdr.msg_iovlen = 1;

  retval = sendmmsg(sockfd, msg, 2, 0);
  if (retval == .1)
    perror("sendmmsg()");
  else
    printf("%d messages sent\n", retval);

  exit(0);
}

1 个答案:

答案 0 :(得分:3)

根据您收到的错误消息,sendmmsg()未在链接时定义。根据手册页here,在glibc版本2.14中添加了对sendmmsg()的支持。您使用的是什么版本的glibc?

如果内核支持sendmmsg(),但现有系统库不提供,则最好的选择可能是使用syscall(2)“自己动手”sendmmsg()。