Getting Started with Winsock
The following is a step-by-step guide to getting started with Windows Sockets programming. It is designed to provide an understanding of basic Winsock functions and data structures, and how they work together.
The client and server application that is used for illustration is a very basic client and server. More advanced code examples are included in the samples included with the Microsoft Windows Software Development Kit (SDK).
The first few steps are the same for both client and server applications.
The following sections describe the remaining steps for creating a Winsock client application.
- Creating a Socket for the Client
- Connecting to a Socket
- Sending and Receiving Data on the Client
- Disconnecting the Client
The following sections describe the remaining steps for creating a Winsock server application.
- Creating a Socket for the Server
- Binding a Socket
- Listening on a Socket
- Accepting a Connection
- Receiving and Sending Data on the Server
- Disconnecting the Server
The complete source code for these basic examples.
- Running the Winsock Client and Server Code Sample
- Complete Winsock Client Code
- Complete Winsock Server Code
Several more advanced Winsock client and server samples are included with the Windows SDK. By default, the Winsock sample source code is installed in the following directory by the Windows SDK for Windows 7:
C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\NetDs\winsock
On earlier versions of the Windows SDK, the version number in the above path would change. For example, the Winsock sample source code is installed in the following default directory by the Windows SDK for Windows Vista
C:\Program Files\Microsoft SDKs\Windows\v6.0\Samples\NetDs\winsock
The more advanced samples listed below in order from higher to lower performance are found in the following directories:
This directory contains three sample programs that use I/O completion ports. The programs include a Winsock server (iocpserver) that uses the WSAAccept function, a Winsock server (iocpserverex) that uses the AcceptEx function, and a simple multithreaded Winsock client (iocpclient) used to test either of these servers. The server programs support multiple clients connecting via TCP/IP and sending arbitrary sized data buffers which the server then echoes back to the client. For convenience, a simple client program, iocpclient, was developed to connect and continually send data to the server to stress it using multiple threads. Winsock servers that use I/O completion ports provide the most performance capability.
This directory contains a sample server program that uses overlapped I/O. The sample program uses the AcceptEx function and overlapped I/O to handle multiple asynchronous connection requests from clients effectively. The server uses the AcceptEx function to multiplex different client connections in a single-threaded Win32 application. Using overlapped I/O allows for greater scalability.
This directory contains a basic sample program that demonstrates the use of the WSAPoll function. The combined client and server program are non-blocking and use the WSAPoll function to determine when it is possible to send or receive without blocking. This sample is more for illustration and is not a high-performance server.
This directory contains three basic sample programs that demonstrate the use of multiple threads by a server. The programs include a simple TCP/UDP server (simples), a TCP-only server (simples_ioctl) that uses the select function in a Win32 console application to support multiple client requests, and a client TCP/UDP program (simplec) for testing the servers. The servers demonstrates the use of multiple threads to handle multiple client requests. This method has scalability issues since a separate thread is created for each client request.
This directory contains a basic sample server and client program. The server demonstrates the use of either non-blocking accept using the select function or asynchronous accept using the WSAAsyncSelect function. This sample is more for illustration and is not a high-performance server.
Build date: 11/29/2012