Winsock Basics and Beyond

Author

Reads 9.2K

Ethernet Cables Plugged in Network Switch
Credit: pexels.com, Ethernet Cables Plugged in Network Switch

Winsock is a programming interface that allows your computer to communicate with the internet, and it's used by many different types of software.

Winsock is a part of the Windows operating system, and it's been around since Windows 95.

To use Winsock, you need to create a socket, which is a endpoint for communication between your program and the internet.

A socket is created by calling the socket function, which returns a socket descriptor that you can use to send and receive data.

Winsock uses a connection-oriented protocol, which means that a connection is established before data is sent.

For another approach, see: Do Iphones Use Sms

What is Winsock

Winsock is a programming interface that allows developers to create network-capable applications. It's a key part of Microsoft's Windows operating system.

Winsock enables programmers to transmit application data across the wire, independent of the network protocol being used. This means applications can work with different protocols without requiring significant changes.

Winsock follows the Windows Open System Architecture (WOSA) model. It defines a standard service provider interface (SPI) between the application programming interface (API) and the protocol stacks.

Here's an interesting read: Global Network

Credit: youtube.com, Winsock Meaning

The sockets paradigm, first popularized by Berkeley Software Distribution (BSD) UNIX, is used by Winsock. This paradigm has been adapted for Windows in Windows Sockets 1.1, which Winsock 2 applications are backward compatible with.

Winsock programming previously centered around TCP/IP. However, some programming practices that worked with TCP/IP do not work with every protocol.

Implementation Details

To implement Winsock, you'll need to create two Visual Studio solutions, one for the server and one for the client. Make sure to add the win2_32.lib dependency to both solutions.

For the server, you'll need to add a header file called stdafx.h to the project properties. This file is crucial for setting up the server.

To link the DLL and check the server's functionality, follow the steps outlined below:

  • Step 1: Linking DLL and checking.
  • Step 2: Create socket
  • Step 3: Bind the socket
  • Step 4: Listen on port
  • Step 5: Accept function

The accept function is a blocking function that waits for a client to establish a connection with the socket. This function call will create a fork of the server socket, connect it with the client, and free the original server socket for new connections.

Curious to learn more? Check out: Self Hosted Chat Server

How It Works

Credit: youtube.com, 5 Implementation Details

To create a socket, you need to follow a series of steps.

First, you need to create two Visual Studio solutions, one for the server and one for the client. Then, you need to add a header file named stdafx.h to both solutions.

The process of linking a DLL involves adding a specific library, win2_32.lib, to the project properties in Visual Studio.

Here are the steps to create a socket:

1. Linking DLL and checking

2. Create socket

3. Bind the socket

4. Listen on port

5. Accept function

The Accept function is a blocking function that waits until a client has established a connection with the socket, and then it accepts the connection and moves on.

The Accept function returns a value of type socket, which is a fork of the server socket that connects it with the client. The original server socket is now free for new connections.

The client side socket and connect function will automatically bind the socket and IP provided.

Intriguing read: Azure Visual Studio

Specifications

Credit: youtube.com, What Is An Implementation Specification? - SecurityFirstCorp.com

Winsock's specifications have undergone significant changes since its first release in 1992. Version 1.0 defined the basic operation of Winsock, keeping it very close to the existing interface of Berkeley sockets to simplify porting of existing applications.

The first version added a few Windows-specific extensions, mainly for asynchronous operations with message-based notifications. This allowed developers to write more efficient and responsive network code.

Winsock 2 was a major upgrade, released in 1993, which added support for protocol-independent name resolution, asynchronous operations with event-based notifications and completion routines, layered protocol implementations, multicasting, and quality of service. This version also formalized support for multiple protocols, including IPX/SPX and DECnet.

Here's a brief overview of the key features introduced in Winsock 2:

  • Protocol-independent name resolution
  • Asynchronous operations with event-based notifications and completion routines
  • Layered protocol implementations
  • Multicasting
  • Quality of service
  • Support for multiple protocols, including IPX/SPX and DECnet

The subsequent versions, 2.0.x, had internal draft status and were not announced as public standards. It wasn't until January 1996 that the first public release of the Winsock 2 specification, version 2.1.0, was made available.

Check this out: Onedrive Business Plan 2

Background

Soldiers Sitting beside Containers and Using Computers
Credit: pexels.com, Soldiers Sitting beside Containers and Using Computers

Early Microsoft operating systems offered limited networking capability, based on NetBIOS.

Microsoft didn't support the TCP/IP protocol stack at that time, which led to university groups and commercial vendors introducing their own TCP/IP products for MS-DOS.

These vendors, including the PC/IP group at MIT, FTP Software, Sun Microsystems, Ungermann-Bass, and Excelan, often provided TCP/IP as part of a hardware/software bundle.

The drawback was that each vendor used their own API, making it difficult for independent software developers to create networking applications that would work with any vendor's underlying TCP/IP implementation.

The Windows Sockets project originated in a Birds of a Feather session held at Interop '91 in San Jose on October 10, 1991.

The first edition of the specification was authored by Martin Hall, Mark Towfiq of Microdyne, Geoff Arnold of Sun Microsystems, and Henry Sanders and J Allard of Microsoft, with assistance from many others.

The specification was copyrighted by the five authors as individuals to avoid potential anti-trust issues.

Microsoft Implementation

Credit: youtube.com, Lab11 Network Programming

Microsoft implemented Winsock in various ways over the years. Version 1.1 was supplied as an add-on package for Windows for Workgroups, code named Snowball.

Microsoft did supply an implementation of Winsock 2, but only for Windows releases starting from Windows 98, Windows NT 4.0, and onwards.

Here's a brief rundown of when Winsock was included in Microsoft operating systems:

  • Winsock 2 was an integral component of Windows 98, Windows NT 4.0, and all subsequent Windows releases.
  • Winsock 2 was supplied in an add-on package for Windows 95.
  • Recent versions of Winsock 2.x have been delivered with new Windows releases or as part of service packs.

Using Winsock

Winsock has become a standard accepted API for network layers, with several network providers agreeing to support it.

Before its introduction, every network had to develop their own interface libraries, which was a hassle to deal with.

Winsock makes a clear distinction of tasks within the network vendor's software and the application, introducing convenient standardization in developing these APIs and applications.

This standardization has made it easier for developers to create applications that can communicate with various networks.

Ports

Using Winsock, you'll need to decide on a port to share data between the client and server. Each machine has almost 65000 ports available, which is a 16-bit address.

Credit: youtube.com, C++ Network Programming Part 1: Sockets

The client needs to know the port of the server, and the server needs to know the port of the client. This is a crucial step in establishing a connection.

A 16-bit address can accommodate up to 65,535 possible ports, which is why you have so many options to choose from.

Coding Implementation

To set up your Winsock project, you'll need to create two Visual Studio solutions: one named server and another named client. Make sure to add a header file named stdafx.h to both solutions in Visual Studio's Header files.

In the project properties, go to the Linker > additional dependencies > edit and add win2_32.lib to both solutions.

Here's a quick rundown of the steps to follow:

  • Create two Visual Studio solutions: server and client
  • Add win2_32.lib to both solutions in the project properties
  • Add a header file named stdafx.h to both solutions in Visual Studio's Header files

Run Time Requirements

Windows Sockets 2 can be used on all Windows platforms.

The documentation clearly notes any platform restrictions or capabilities that may be limited.

Advanced Sample Apps

Advanced Sample Apps are available on GitHub, offering a range of Winsock client and server sample apps that can help you improve your skills and learn from others.

Credit: youtube.com, Windows : WinSock.h & WinSock2.h which to use?

These sample apps are categorized by performance, with the highest-performance servers using I/O completion ports. This approach allows for multiple clients to connect and send data to the server, which then echoes it back to the client.

The sample apps are organized into folders, each containing a specific type of server or client program. For example, the "iocp" folder contains three sample programs that use I/O completion ports, while the "overlap" folder contains a sample server program that uses overlapped I/O.

Here's a breakdown of the sample apps by folder:

Each sample app has its own strengths and weaknesses, and can be used to learn specific techniques or to improve your overall understanding of Winsock programming.

Celebrating Success

Winsock's open-source nature has allowed it to remain fresh and adaptable even after a quarter of a century.

This has led to a wide range of modifications and changes, making it a staple in the tech industry.

Winsock has permeated most computer systems, ensuring its continued relevance for years to come.

Its robust nature has made it a reliable choice for developers and users alike.

You can rest assured that Winsock is here to stay for the coming years.

Viola Morissette

Assigning Editor

Viola Morissette is a seasoned Assigning Editor with a passion for curating high-quality content. With a keen eye for detail and a knack for identifying emerging trends, she has successfully guided numerous articles to publication. Her expertise spans a wide range of topics, including technology and software tutorials, such as her work on "OneDrive Tutorials," where she expertly assigned and edited pieces that have resonated with readers worldwide.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.