本文主要是介绍C/C++ 设置Socket的IP_TOS选项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
IP TOS选项是指示IP报文转发的优先级,QOS控制的一种,常规的IP协议TOS都为0,就是普通报文。
设置:
IPV4/TOS设置(Socket),IPTOS_LOWDELAY 表示FLASH优先级(一般用在游戏,即时通信这块,因为需要包转发要足够快)
bool Socket::SetTypeOfService(int fd, int tos) noexcept {if (fd == -1) {return false;}if (tos < 0) {tos = IPTOS_LOWDELAY; }Byte b = tos; /* IPTOS_LOWDELAY */
#if defined(_MACOS)return ::setsockopt(fd, IPPROTO_IP, IP_TOS, (char*)&b, sizeof(b)) == 0;
#elsereturn ::setsockopt(fd, SOL_IP, IP_TOS, (char*)&b, sizeof(b)) == 0;
#endif}
设置
IPV4/IPV6 TOS设置(Socket)
void Socket::AdjustDefaultSocketOptional(int sockfd, bool in4) noexcept {if (sockfd != -1) {uint8_t tos = IPTOS_LOWDELAY;if (in4) {if (SOCKET_RESTRICTIONS_.IP_TOS_ON) {
#if defined(_MACOS)::setsockopt(sockfd, IPPROTO_IP, IP_TOS, (char*)&tos, sizeof(tos));
#else::setsockopt(sockfd, SOL_IP, IP_TOS, (char*)&tos, sizeof(tos));
#endif}#if IP_DONTFRAGMENTint dont_frag = IP_PMTUDISC_DO;::setsockopt(sockfd, IPPROTO_IP, IP_DONTFRAGMENT, (char*)&dont_frag, sizeof(dont_frag));
#elif IP_PMTUDISC_WANTint dont_frag = IP_PMTUDISC_WANT;::setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, &dont_frag, sizeof(dont_frag));
#endif}else {// linux-user: Add missing IP_TOS, IPV6_TCLASS and IPV6_RECVTCLASS sockopts// QEMU:// https://patchwork.kernel.org/project/qemu-devel/patch/20170311195906.GA13187@ls3530.fritz.box/
#if IPV6_TCLASSif (SOCKET_RESTRICTIONS_.IPV6_TCLASS_ON) {::setsockopt(sockfd, IPPROTO_IPV6, IPV6_TCLASS, (char*)&tos, sizeof(tos)); /* SOL_IPV6 */}
#endif#if IPV6_MTU_DISCOVER && IPV6_PMTUDISC_WANTint dont_frag = IPV6_PMTUDISC_WANT;::setsockopt(sockfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &dont_frag, sizeof(dont_frag));
#endif}#if defined(SO_NOSIGPIPE)int no_sigpipe = 1;::setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, sizeof(no_sigpipe));
#endif}}
这篇关于C/C++ 设置Socket的IP_TOS选项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!