0

According to MSDN (VS2012 version), sockaddr_in is defined in winsock2.h. There are no exceptions stated (similar to what we see with Windows Phone and some API calls). Yet when I try to compile a file which uses sockaddr_in and includes winsock2.h, I get a compile error (below).

Notice that I don't receive a winsock2.h not found error (or similar). Also note that the same code compiles on Windows Phone 8.

What does one need to include for WinRT?

        cl /Fotmp32dll\bss_conn.obj  -Iinc32 -Itmp32dll -DOPENSSL_THREADS  -W3 -
Gs0 -GF -Gy -nologo -DOPENSSL_SYSNAME_WINRT -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -D_
CRT_SECURE_NO_DEPRECATE -I\usr\local\ssl\fips-2.0/include /nologo /D NDEBUG /D _
USRDLL /D _WINDLL /D WINAPI_FAMILY=WINAPI_PARTITION_APP /FI SDKDDKVer.h /FI wina
pifamily.h -DOPENSSL_NO_RC5 -DOPENSSL_NO_MD2 -DOPENSSL_NO_KRB5 -DOPENSSL_NO_ENGI
NE -DOPENSSL_NO_HW -DOPENSSL_FIPS -DOPENSSL_NO_JPAKE -DOPENSSL_NO_STATIC_ENGINE
/Zi /Fdtmp32dll/lib  -DOPENSSL_BUILD_SHLIBCRYPTO -c .\crypto\bio\bss_conn.c
bss_conn.c
.\crypto\bio\bss_conn.c(95) : error C2079: 'them' uses undefined struct 'sockadd
r_in'
.\crypto\bio\bss_conn.c(207) : error C2224: left of '.sin_family' must have stru
ct/union type
.\crypto\bio\bss_conn.c(207) : error C2065: 'AF_INET' : undeclared identifier
.\crypto\bio\bss_conn.c(208) : error C2224: left of '.sin_port' must have struct
/union type
.\crypto\bio\bss_conn.c(208) : warning C4013: 'htons' undefined; assuming extern
 returning int
.\crypto\bio\bss_conn.c(214) : error C2224: left of '.sin_addr' must have struct
/union type
.\crypto\bio\bss_conn.c(214) : warning C4013: 'htonl' undefined; assuming extern
 returning int
.\crypto\bio\bss_conn.c(217) : warning C4013: 'socket' undefined; assuming exter
n returning int
.\crypto\bio\bss_conn.c(217) : error C2065: 'AF_INET' : undeclared identifier
.\crypto\bio\bss_conn.c(217) : error C2065: 'SOCK_STREAM' : undeclared identifie
r
.\crypto\bio\bss_conn.c(217) : error C2065: 'IPPROTO_TCP' : undeclared identifie
r
.\crypto\bio\bss_conn.c(218) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(220) : warning C4013: 'WSAGetLastError' undefined; assum
ing extern returning int
.\crypto\bio\bss_conn.c(260) : warning C4013: 'connect' undefined; assuming exte
rn returning int
.\crypto\bio\bss_conn.c(366) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(379) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(383) : warning C4013: 'shutdown' undefined; assuming ext
ern returning int
.\crypto\bio\bss_conn.c(384) : warning C4013: 'closesocket' undefined; assuming
extern returning int
.\crypto\bio\bss_conn.c(385) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(422) : warning C4013: 'WSASetLastError' undefined; assum
ing extern returning int
.\crypto\bio\bss_conn.c(423) : warning C4013: 'recv' undefined; assuming extern
returning int
.\crypto\bio\bss_conn.c(447) : warning C4013: 'send' undefined; assuming extern
returning int
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 11.0
\VC\bin\x86_ARM\cl.EXE"' : return code '0x2'
Stop.
jww
  • 97,681
  • 90
  • 411
  • 885
  • 3
    Winsock is not permitted in a WinRT app, along with many other winapi functions. It is not compatible with the app container sandbox. You must use the WinRT replacement classes instead. Beware that local loopback is not permitted either. – Hans Passant Aug 08 '13 at 11:25
  • Thanks Hans. My biggest gap seems to be quality documentation. Where does one find the documentation? I would love to get my hands on something written to the quality of Richter's [Advanced Windows](http://www.amazon.com/Advanced-Windows-Jeffrey-Richter/dp/1572315482) series for WinRT and Windows Phone 8. The two Windows Phone books I have leave a lot to be desired (and don't cross-over well into WinRT at times). – jww Aug 08 '13 at 12:57
  • A quick question Hans: "Winsock is not permitted in a WinRT app" - we are porting a library, not building an app. Is there anything we should be doing differently to get the library ported since this is not app development? (Just asking for completeness). – jww Aug 09 '13 at 02:07
  • 1
    No, makes no difference. – Hans Passant Aug 09 '13 at 04:18
  • This looks like good news, Hans. I work exclusively with federal and corporate enterprises. I think we can avoid the Windows App Store restrictions by side loading apps. See, for example, [Sideload Windows Store Apps](http://technet.microsoft.com/en-us/windows/jj874388.aspx). Without the restrictions, we will be on-par with Microsoft and its internal use of the standard runtimes (gotta love vertical monopolies). – jww Aug 10 '13 at 01:56
  • 1
    I suppose that gives you the option to have Obama make a call, but no, this is enforced at runtime as well. – Hans Passant Aug 10 '13 at 07:11
  • :) Fortunately, Microsoft is working with us on this port. I hope I missed something obvious and they have a solution. I'll ask after they give me the answer to "what API family declares should I use when compiling on the command line." – jww Aug 11 '13 at 14:46
  • 1
    FYI winsock was not blocked at runtime, only during Store submission. The header partitioning made by default made it impossible to compile as well. – Peter Torr - MSFT May 26 '15 at 01:59

3 Answers3

4

The definition of sockaddr_in is in the file winsock2.h. In my situation, I got the compiler error "error C2065: 'sockaddr_in' : undeclared identifier", so I open the file winsock2.h to check out the definition of sockaddr_in, then I found out the reason.

/*
 * Socket address, internet style.
 */
struct sockaddr_in {
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};

Above code is definition of sockaddr_in. I define my variables in the wrong way:

sockaddr_in sin;

The right way is:

struct sockaddr_in sin;
MrCoder
  • 41
  • 3
1

Note that as of Visual Studio 2013 Update 4, winsock is available to use in Windows Store apps. (This doesn't apply to the original question, but I'm adding this in case other people find this question in the future).

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
0

That's a linker error, not a compiler error.

.h files aren't involved in the linking process. You need to include some .lib file that has that in it.

// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib

http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx

edit: oops, yes it is the compiler. Still, check out the source code I linked to

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • Thanks xaxxon. According to [Alternatives to Windows APIs in Windows Store apps](http://msdn.microsoft.com/en-us/library/windows/apps/hh464945.aspx), Winsock is not available. My results seem to confirm the lack of Winsock (along with Han's comments). I'm going to ask Microsoft about this. Windows RT sucks, and the lack of clear documentation really sucks. – jww Aug 11 '13 at 14:09