SO_BINDTODEVICE 套接字网络接口绑定选项
SO_PRIORITY 套接字优先级选项
通过调用setsocketopt函数来设置一系列socket选项
/**
*SO_BINDTODEVICE 套接字网络接口绑定选项
*SO_PRIORITY 套接字优先级选项
*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/ioctl.h> /*ioctl命令*/
#include<netinet/if_ether.h> /*ethhdr结构*/
#include<net/if.h> /*ifreq结构*/
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>
#include<netinet/in.h> /*in_addr结构*/
#include<netinet/ip.h> /*iphdr结构*/
#include<netinet/udp.h> /*udphdr结构*/
#include<netinet/tcp.h> /*tcphdr结构*/
int main(int argc, char *argv[]){
int err;
int s = socket(AF_INET , SOCK_STREAM , 0);
char ifname[] = "eno1"; /*绑定网卡名称*/
struct ifreq if_etho1; /*绑定网卡结构*/
strncpy(if_etho1.ifr_name , ifname , IFNAMSIZ); /*将网卡名称放到结构成员ifr_name中*/
err = setsockopt(s , SOL_SOCKET , SO_BINDTODEVICE , (char*)&if_etho1 , sizeof(if_etho1)); /*将s绑定到网卡etho1上*/
if(err){//失败
printf("setsockopt SO_BINDTODEVCIE failure \n");
}else{ //成功
printf("setsockopt SO_BINDTODEVCIE success \n");
}
int opt = 6; /*优先级 0 ~ 6 :6 最高,优先处理*/
err = setsockopt(s , SOL_SOCKET , SO_PRIORITY , &opt , sizeof(opt));/*设置s的优先级*/
if(err){//失败
printf("setsockopt SO_PRIORITY failure \n");
}else{ //成功
printf("setsockopt SO_PRIORITY success \n");
}
return 0;
}