正如我对man页面的理解,当设置MSG_DONTROUTE时,数据包只应发送到直接连接到源的目标。 但是,当我尝试将数据包发送到间接连接目标(需要路由)时,send()仍会返回正值。我是否误解了什么,谢谢?
int main (int argc, char *argv[]) {
const char* ip = argv[1] ? argv[1] : "74.125.31.100"; // google
int fd = socket (AF_INET, SOCK_STREAM, 0);
if (fd == -1)
ErrAndExit("socket");
struct sockaddr_in addr;
memset (&addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons (80);
if (inet_pton (AF_INET, ip, &addr.sin_addr) != 1)
ErrAndExit ("inet_pton");
if (connect (fd, (struct sockaddr*) &addr, sizeof(addr)) == -1)
ErrAndExit("connect");
// int optval = 1;
// int optlen = sizeof(optval);
// if (setsockopt (fd, SOL_SOCKET, SO_DONTROUTE, (void*) &optval, optlen) == -1)
// ErrAndExit ("setsockopt");
ssize_t written = send (fd, "apple", 6, MSG_DONTROUTE);
if (written == -1)
ErrAndExit ("send");
else
printf ("wrote %ld bytes\n\n", written);
return 0;
}