获取tcp缓冲区大小 - C++

#include "stdafx.h"
#include <WinSock2.h>
#include <iostream>

#pragma comment(lib,"ws2_32.lib")

int main()
{
	WORD vVersionRequested;
	WSADATA wsaData;
	vVersionRequested = MAKEWORD(1, 1);
	WSAStartup(vVersionRequested, &wsaData);//WSAStartup就是为了向操作系统说明,我们要用哪个库文件,让该库文件与当前的应用程序绑定,从而就可以调用该版本的socket的各种函数了。

	SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);

	int optVal = 0;
	int optLen = sizeof(optVal);

	getsockopt(sockClient, SOL_SOCKET, SO_RCVBUF, (char *)&optVal, &optLen);

	std::cout << "Recv len = " << optVal << std::endl;
	getsockopt(sockClient, SOL_SOCKET, SO_SNDBUF, (char *)&optVal, &optLen);
	std::cout << "send len = " << optVal << std::endl;
	//设置buff
	optVal = 1024*100;
	setsockopt(sockClient, SOL_SOCKET, SO_RCVBUF, (char*)&optVal, optLen);
	setsockopt(sockClient, SOL_SOCKET, SO_SNDBUF, (char*)&optVal, optLen);

	// 再次获取sockClient1对应的内核接收缓冲区大小
	getsockopt(sockClient, SOL_SOCKET, SO_RCVBUF, (char*)&optVal, &optLen);
	printf("sockClient, recv buf is %d\n", optVal);
	getsockopt(sockClient, SOL_SOCKET, SO_SNDBUF, (char *)&optVal, &optLen);
	std::cout << "send len = " << optVal << std::endl;

	closesocket(sockClient);
	WSACleanup();

    return 0;
}

 

发布了74 篇原创文章 · 获赞 56 · 访问量 15万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览