Windows Sockets

Windows Sockets == winsock

Documentation available at


What is winsock

Windows Sockets spec. defines a network programming interface to Microsoft Windows which is based on the "socket" paradigm.

Winsock was designed by a committee.

A standard API for all Windows TCP/IP programs.


How?

Windows DLL

Each vendor supplies its own WINSOCK.DLL

The DLL has to provide only the interface.

Actual network code can reside elsewhere.

Trumpet uses a windows program which is started by WINSOCK.DLL

PCTCP uses a network stack which is loaded at boot time.


What?

Winsock provides all of the "standard" Berkeley socket calls.

Additional functions were added for effiency reasons.

Only new mandatory calls:


MS Windows (tm)

Problems with Berkeley sockets under Windows:


Winsock differences

Asynchornous versions of blocking Berkeley socket calls:

Database lookup calls:

Berkeley                       Windows                                        
gethostbyaddr()                WSAAsyncGetHostByAddr()                        
gethostbyname()                WSAAsyncGetHostByName()                        
getprotobyname()               WSAAsyncGetProtoByName()                       
getprotobynumber()             WSAAsyncGetProtoByNumber()                     
getservbyname()                WSAAsyncGetServByName()                        
getservbyport()                WSAAsyncGetServByPort()                        

Other Windows-specific calls:


Winsock Data structures/types

SOCKET type

Typical BSD:

    s = socket(...);
    if (s == -1)
        { ... }

Winsock:

    s = socket(...);
    if (s == INVALID_SOCKET)
        { ... }

fd_set is NOT a bit-array under winsock.

MUST use FD_XXX calls (not macros...)


Errors

errno can lie!

Typical BSD:

    r = recv(...);
    if (r == -1 && errno == EWOULDBLOCK)
        {...}

Winsock:

    r = recv(...);
    if (r == -1 &&
            WSAGetLastError() == EWOULDBLOCK)
        {...}

Example WSA call

HANDLE PASCAL FAR WSAAsyncGetHostByAddr(
    HWND hWnd,
    unsigned int wMsg,
    const char FAR *addr,
    int len,
    int type,
    char FAR *buf,
    int buflen);
hWnd
The handle of the window which should receive a message when the asynchronous request completes.
wMsg
The message to be received when the asynchronous request completes.
addr
A pointer to the network address for the host. (network byte order!)
len
The length of the address
type
The type of the address (must be PF_INET) A pointer to the data area to receive the hostent data.
buf
Most be at least MAXGETHOSTSTRUCT bytes.
buflen
The size of data area buf above.