简体   繁体   English

C ++ - 初始化SOCKADDR_IN

[英]C++ - Initializing SOCKADDR_IN

I am working through some Static Analysis defects and one that is causing me an issue is this one. 我正在处理一些静态分析缺陷,并且导致我出现问题的是这一个。

SOCKADDR_IN m_stLclAddr;

SOCKADDR_IN is a member of the WinSock API SOCKADDR_IN是WinSock API的成员

The defect is saying that I have not initialized the following: 缺陷是说我没有初始化以下内容:

  • m_stLclAddr.sin_port m_stLclAddr.sin_port
  • m_stLclAddr.sin_zero m_stLclAddr.sin_zero
  • m_stLclAddr.sin_addr m_stLclAddr.sin_addr
  • m_stLclAddr.sin_family m_stLclAddr.sin_family

I am not very familiar familiar with the WinSock API but I have done a bit of research and I just want to know if the following line of code would initialize m_stLclAddr with default values?: 我对熟悉WinSock API并不是很熟悉,但我做了一些研究,我只是想知道以下代码行是否会使用默认值初始化m_stLclAddr?

m_stLclAddr = { 0 };

m_stLclAddr = {0} will set everything to zero the first time (not necessarily default values or what you actually want to do). m_stLclAddr = {0}将第一次将所有内容设置为零(不一定是默认值或您实际想要做的事情)。 memset(&m_stLclAddr, 0, sizeof(SOCKADDR_IN)); will set everything in m_stLclAddr to zero for not only initialization, but successive calls as well. 将m_stLclAddr中的所有内容设置为零,不仅用于初始化,还用于连续调用。

I would think you would want to do something like this: 我想你会想做这样的事情:

local_sin.sin_family = AF_INET;
local_sin.sin_port = htons (PORTNUM);
local_sin.sin_addr.s_addr = htonl (INADDR_ANY);

as shown here: http://msdn.microsoft.com/en-us/library/aa454002.aspx 如下所示: http//msdn.microsoft.com/en-us/library/aa454002.aspx

是的,使用{0}会将m_stLclAddr初始化为全零

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM