在c语言中如何屏蔽一段程序,如何在用C语言关闭TCP程序中的Nagle算法

TCP为了防止在网络中过多的小分组会导致阻塞,因此提供了Nagle算法:要求一个TCP连接上最多只能有一个未被确认的未完成的小分组,在该分组的确认到达之前不能发送其他的小分组。相反,TCP收集这些少量的分组,并在确认到来时以一个分组的方式发出去。Nagle算法会导致自己写的TCP程序中,小分组不能及时的发送出去。

现在我写了个基于TCP的socket程序,希望AP往client端发送1000个16字节的报文。结果发现client端接收到的报文并不是每次大小都是82字节(82字节是因为有66字节的TCP和IP头等,外加16字节我自己的数据),会出现大小为98字节的数据(98字节是因为有66字节的TCP和IP头等,外加两个16字节的自己的数据)。如下图所示:

6ac351bc6365a4e5e48b5f0c4464073d.png

这时就想起了关闭TCP中的Nagle算法。于是在我的TCP发送的Socket程序中加入如下代码(记得还要导入头文件: #include ):

/* Disable the Nagle (TCP No Delay) algorithm */

//sock_cli是自己定义的socket套接字

int flag = 1;

int ret = setsockopt( sock_cli, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag) );

if (ret == -1) {

printf("Couldn‘t setsockopt(TCP_NODELAY)\n");

exit(-1);

}

重新编译并且运行,抓包后如下图所示:

129e62506076476a878c6e530e94b068.png

可以发现不会在出现小分组等待的现象,说明关闭了Nagle算法。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MICROSOFT FOUNDATION CLASS LIBRARY : ShieldPower AppWizard has created this ShieldPower application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your ShieldPower application. ShieldPower.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. ShieldPower.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CShieldPowerApp application class. ShieldPower.cpp This is the main application source file that contains the application class CShieldPowerApp. ShieldPower.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. ShieldPower.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\ShieldPower.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file ShieldPower.rc. res\ShieldPower.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one dialog class: ShieldPowerDlg.h, ShieldPowerDlg.cpp - the dialog These files contain your CShieldPowerDlg class. This class defines the behavior of your application's main dialog. The dialog's template is in ShieldPower.rc, which can be edited in Microsoft Visual C++. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ShieldPower.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file. ///////////////////////////////////////////////////////////////////////////// Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you will need to copy the corresponding localized resources MFC42XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC42DEU.DLL contains resources translated to German.) If you don't do this, some of the UI elements of your application will remain in the language of the operating system. /////////////////////////////////////////////////////////////////////////////

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值