c - How to create socket via socket() on MSVC? -


I am porting the Linux app under Win32 (msvc 9.0) and after finishing it, I'm experiencing the default behavior as simple as socket () .

  #include & lt; Winsock2.h & gt; # Include & lt; Ws2tcpip.h & gt; Int main (int argc, char ** argv) {int subsock; If ((subsock = socket (PF_INET, SOCK_STREAM, 0)) & lt; 0) {printf ("failed to open socket (rate value =% d) \ n", subsock); }}   

It prints failed to open socket (rate value = -1) What is wrong every time and how to open socket on win32?

You need to start winsock before starting WSAS socket calls will be successful.

  WORD wVersionRequested; WSADATA wsaData; Intel mistake; / * Use the MacDayDard (Kambyte, Hibiite) macro declared in the WifiHeAs. * / WVersionRequested = MAKEWORD (2, 2); Err = wsasartartup (wVersionRequested, and wsaData) if (error! = 0) {yourError! For more information:  

Just request the 2.2 version and be done with it.

Comments