An Intro to Windows Socket (Winsock2) Programming 1

 

 

 

 

 

This Winsock 2 tutorial is an excerpt from a complete hands-on tutorial which can be found at Windows Winsock 2 Network Programming. The .NET version with C++, VB .NET and C# code samples can be found at .NET network programming.

 

Introduction

 

The following figure shows the TCP/IP stack for computer communication. Starting with the Open System Interconnection (OSI) seven layers and the TCP/IP stack. The Winsock is a library used for network communication programming for Windows.

 

7

Application

e.g. HTTP, SMTP, SNMP, FTP, Telnet, SSH and Scp, NFS, RTSP etc.

6

Presentation

e.g. XDR, ASN.1, SMB, AFP etc.

5

Session

e.g. TLS, SSH, ISO 8327 / CCITT X.225, RPC, NetBIOS, ASP etc.

4

Transport

e.g. TCP, UDP, RTP, SCTP, SPX, ATP etc.

3

Network

e.g. IP/IPv6, ICMP, IGMP, X.25, CLNP, ARP, RARP, BGP, OSPF, RIP, IPX, DDP etc.

2

Data Link

e.g. Ethernet, Token ring, PPP, HDLC, Frame relay, ISDN, ATM, 802.11 Wi-Fi, FDDI etc.

1

Physical

e.g. wire, radio, fiber optic etc.

 

The following figure shows the 4 layers of TCP/IP suite.

 

4

Application layer

BGP, FTP, HTTP, HTTPS, IMAP, IRC, NNTP, POP3, RTP, SIP, SMTP, SNMP, SSH, SSL, Telnet, UUCP, Finger, Gopher, DNS, RIP, Traceroute, Whois, IMAP/IMAP4, Ping, RADIUS, BGP etc.

3

Transport layer

DCCP, OSPF, SCTP, TCP, UDP, ICMP etc.

2

Network/Internet layer

IPv4, IPv6, ICMP, ARP, IGMP etc

1

Physical/ Data

Link layer

Ethernet, Wireless (WAP, CDPD, 802.11, Wi-Fi), Token ring, FDDI, PPP, ISDN, Frame Relay, ATM, SONET/SDH, xDSL, SLIP etc.

RS-232, EIA-422, RS-449, EIA-485 etc.

 

Commonly, the top three layers of the OSI model (Application, Presentation and Session) are considered as a single Application layer in the TCP/IP suite and the bottom two layers as well considered as a single Network Access layer. The following Figure illustrates the TCP/IP communication layer for two network cards (Network Interface Card – NIC).

 

Windows Socket (Winsock2) Programming and  C Language: The TCP/IP flow of packet illustration

 

Winsock is a standard application programming interface (API) that allows two or more applications (or processes) to communicate either on the same machine or across a network and is primarily designed to foster data communication over a network. It is important to understand that Winsock is a network programming interface and not a protocol. Winsock provides the programming interface for applications to communicate using popular network protocols such as Transmission Control Protocol/Internet Protocol (TCP/IP) and Internetwork Packet Exchange (IPX). The Winsock interface inherits a great deal from the BSD Sockets implementation on UNIX platforms. In Windows environments, the interface has evolved into a truly protocol-independent interface, especially with the release of Winsock 2.

The examples presented in this chapter help to provide an understanding of the Winsock calls that are required for accepting connections, establishing connections, and sending and receiving data. Because the purpose of this chapter is to learn these fundamental Winsock calls, the examples presented use straight blocking Winsock calls. You can differentiate the two functions with the WSA prefix. If Winsock 2 updated or added a new API function in its specification, the function name is prefixed with WSA. For example, the Winsock 1 function to create a socket is simply socket(). Winsock 2 introduces a newer version named WSASocket() that is capable of using some of the new features made available in Winsock 2. There are a few exceptions to this naming rule. WSAStartup(), WSACleanup(), WSARecvEx(), and WSAGetLastError() are in the Winsock 1.1 specification. If you fail to find the related Winsock headers in your machine or programming environment, you may want to read Visual .NET doc 1 and Visual .NET doc 2 articles first. Those articles also show how to add the Winsock link library, ws2_32.lib to the Visual C++ project.

 

 

 

 

< Windows Socket 2 (Winssock2) | Win32 Programming | Winsock2 2 >