Creating a Basic Winsock Application

ms737629.wedge(en-us,VS.85).gifTo create a basic Winsock application

  1. Create a new empty project.
  2. Add an empty C++ source file to the project.
  3. Ensure that the build environment refers to the Include, Lib, and Src directories of the Microsoft Windows Software Development Kit (SDK) or the earlier Platform Software Development Kit (SDK).
  4. Ensure that the build environment links to the Winsock Library file WS2_32.lib. Applications that use Winsock must be linked with the WS2_32.lib library file.
  5. Begin programming the Winsock application. Use the Winsock API by including the Winsock 2 header files. The Winsock2.h header file contains most of the Winsock functions, structures, and definitions. The Ws2tcpip.h header file contains definitions introduced in the WinSock 2 Protocol-Specific Annex document for TCP/IP that includes newer functions and structures used to retrieve IP addresses.

    Note  Stdio.h is used for standard input and output, specifically the printf() function.


#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main() {
  return 0;
}

Note  

The Iphlpapi.h header file is required if an application is using the IP Helper APIs. When the Iphlpapi.h header file is required, the #include line for the Winsock2.h header this file should be placed before the #include line for the Iphlpapi.h header file.

The Winsock2.h header file internally includes core elements from the Windows.h header file, so there is not usually an #include line for the Windows.h header file in Winsock applications. If an #include line is needed for the Windows.h header file, this should be preceded with the #define WIN32_LEAN_AND_MEAN macro. For historical reasons, the Windows.h header defaults to including the Winsock.h header file for Windows Sockets 1.1. The declarations in the Winsock.h header file will conflict with the declarations in the Winsock2.h header file required by Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header. An example illustrating this is shown below.

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
int main() {
  return 0;
}

Next Step: Initializing Winsock

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags : winsock


Community Content

earlitow
Resolving "...error LNK2028:...WSAStartup(..."
In VC, I had to add "WS2_32.lib" to the linker dependencies ( Project>Properties>Linker>Input).
This removed errors like
"...error LNK2028: unresolved token (0A000024) "extern "C" int __stdcall WSAStartup(..."
Add the library to the dependencies in "corewin_express.vsprops".
See also VC's help, "How to: Use Visual C++ Express Edition with the Microsoft Platform SDK".
Tags : sdk winsock

ddaS-edEn
WS2_32.lib Linking in VC++ 6

In Visual C++ 6, to link to WS2_32.lib:

  1. Project > Settings
  2. In Settings For, select All Configurations
  3. In Category, select Input.
  4. In Object/library modules, type WS2_32.lib

Else the following error occurs during Linking:

Linking...
<project_name>.obj : error LNK2001: unresolved external symbol __imp__WSAStartup@8
Debug/<project_name>.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Tags : error

Thomas Lee
WS2_32.lib problem?

In VC++ express version, I have added "WS2_32.lib" to the linker dependencies ( Project>Properties>Linker>Input).

When I compile the Winsock client example, I still got the following error.

fatal error C1083: Cannot open include file: 'winsock.h': No such file or directory

What is the possible problem?

[tfl - 30 04 09] You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio  : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public     : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&


T.Brooks
linking to WS2_32.lib easiest way
#pragma comment(lib, "ws2_32.lib")

Tags :

themeghnath
#pragma still doesn't help
I am using Visual C++ 2008 Express Edition and have added the lines :
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wsock32.lib")

and it still shows the error :
1>Linking...
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\Indrajeet\Documents\Visual Studio 2008\Projects\Sockets\Debug\Sockets.exe : fatal error LNK1120: 1 unresolved externals



Tags : lnk2019

Page view tracker