May 1995/WinJES — A Windows Sockets Example/Sidebar

Understanding Sockets and Protocols


What are Windows Sockets?

The Windows Sockets API and interfaces is an open network programming interface for Microsoft Windows. The Windows Sockets API is implemented by multiple TCP/IP vendors for Windows. Sockets are an abstraction, a method of communications between programs using the TCP/IP protocol stack. A socket is a bidirectional connection between two programs, which may be running on the same or different computers. The Windows Sockets interface is an adaptation of Berkeley sockets (as found in many UNIX variants), revised to perform politely in a Windows environment. The Windows Sockets dynamic link library (DLL) is responsible for notifying a program when a socket needs attention.

UDP and TCP

Socket-based programs are asynchronous, and messages can arrive at any time. A program defines its own protocol to determine what messages are valid from a partner (or proposed partner) at any time.

Windows Sockets use either TCP (Transmission Control Protocol) or UDP (User Datagram Protocol.) TCP provides reliable, two-way, connection-based communications, while UDP provides connectionless, "unreliable" communications. TCP programs are much like a phone call — once a connection is made, the programs converse, knowing the partner is there. UDP is like mailing a letter — you know the message was sent, but you have no way of knowing it arrived (unless your partner sends you another message to confirm.) I have always considered the word "unreliable" a bit excessive; on a local network, I have never lost a UDP packet.

TCP/IP Addresses

Regardless of the protocol used (TCP or UDP), Windows Sockets uses the TCP/IP addressing scheme. TCP/IP uses a 32-bit address to identify a machine. The machine address is usually expressed in dotted decimal notation, for example, my machine address at work is 198.63.66.15. Since multiple programs can execute simultaneously on the same machine, each program is assigned a port for connections. Some programs (such as mail or file transfers) have well-known ports, which are the same on all machines running TCP/IP. Other ports are available for user programs. Port numbers below 1,024 are reserved for well-known ports. In many UNIX implementations, ports below 4,096 are reserved for programs running with root privilege. User-defined ports must be at or below port number 65,535. Usually the server will define the port number.

Because people deal with names more easily than numbers, TCP/IP has facilities to give machines names as well as numeric addresses. The simplest method is a hosts file, which is a list of addresses with the corresponding names. Larger installations use DNS (Domain Name Services), which is a centralized method of resolving names into valid addresses. A DNS server is a machine on the network that runs DNS and responds to name-to-address translation requests.

WinJES uses the Windows Sockets call gethostbyname to translate names into address information. A program can find the name of the machine it's on by calling gethostname. Many server programs call gethostname to avoid hardcoding the machine name or address in the code. The Windows Sockets library routines are usually written to look up names in the local hosts file first, and then go to a DNS server to resolve a name. However, different implementations have different defaults, so you should check the vendor notes to find the specific search order your implementation uses.

UDP Servers

A UDP server waits for messages to arrive on a predetermined port, and then processes the message. The contents of the message are specific to the server program. When a message arrives, the server uses recvfrom to retrieve the message and the address of the machine that sent it. To send a reply to the client, a UDP server can call sendto, passing the same socket address that the recvfrom call returned.

A UDP server does not usually have a conversation with a client. UDP is a good protocol for inbound messages that will cause the server to perform an action. Since delivery is not guaranteed, it is possible for a server to miss a request. If message loss cannot be tolerated, or you need a two-way (request/reply) conversation, use TCP instead.

TCP Servers

A TCP server listens for clients to connect to its port. Once a client connects, the server program accepts the conversation. The accept call returns a socket which the server uses to converse with the client. The server's original socket is returned to a listening state for more inbound connections.

In Windows, a TCP server can create a new window to deal with each inbound connection. The new window would deal with the client that connected, and the server window would continue to listen for more requests.

Inter-machine Communications

Because different machines store integer data differently, the Windows Sockets library provides routines to translate from local form to a known format and back. Before transferring integers across the network, a program can call htons (Host-to-Network-Short) for 16-bit values or htonl (Host-To-Network-Long) for 32-bit values to receive a network-neutral value. The recepient then calls ntohs or ntohl to translate the data to its local form. It is important to know how your C compiler defines an int, a short, and a long in bytes, since it varies by platform.

The WinJES Protocol

WinJES has a very simple protocol defined on top of UDP datagrams. The datagram must contain the following WinJES command structure:

typedef struct _Message
{
char szCommand[MAXBUFFER];
char szHostName[MAXHOST];
} Message;
The WinJES server listens for messages to arrive on UDP port 10000. The port number is defined in the WINJES.H file. WinJES servers expect a client to send a single datagram to port 10000.