 |
Windows Sockets
Windows Sockets
An Open Interface for
Network Programming under
Microsoft Windows
Version 1.1
20 January 1993
Note
This HTML version of the Windows Socket 1.1 Specification is provided by
Mark Towfiq. It may be freely redistributed, either as provided or in modified
form. Winsock providers may integrate it into their product documentation
without incurring any obligation.
Acknowledgements
I am indebted and thankful to:
Mark Towfiq (towfiq@East.Sun.Com)
Table of Contents
Authors and Copyright
Copyright (c) 1992 by Martin Hall, Mark Towfiq
Geoff Arnold, David Treadwell and Henry Sanders
All rights reserved.
This document may be freely redistributed in any form, electronic or otherwise,
provided that it is distributed in its entirety and that the copyright
and this notice are included. Comments or questions may be submitted via
electronic mail to WinSock@MailBag.Intel.Com.
Requests to be added to the Windows Sockets mailing list should be addressed
to MajorDomo@MailBag.Intel.Com.
This specification, archives of the mailing list, and other information
on Windows Sockets are available via anonymous FTP from the host SunSite.UNC.Edu,
directory /pub/micro/pc-stuff/ms-windows/winsock. Questions about products
conforming to this specification should be addressed to the vendors of
the products.
Acknowledgments
The authors would like to thank their companies for allowing them the time
and resources to make this specification possible: JSB Corporation, Microdyne
Corporation, FTP Software, Sun Microsystems, and Microsoft Corporation.
Special thanks should also be extended to the other efforts contributing
to the success of Windows Sockets. The original draft was heavily influenced
by existing specifications offered and detailed by JSB Corporation and
NetManage, Inc. The "version 1.0 debate" hosted by Microsoft in Seattle
allowed many of the members of the working group to hash out final details
for 1.0 vis-a-vis.
Sun Microsystems was kind enough to allow first time implementors to
"plug and play" beta software during the first Windows Sock-A-Thon of Windows
Sockets applications and implementations at Interop Fall '92. Microsoft
has shared WSAT (the Windows Sockets API Tester) with other Windows Sockets
implementors as a standard Windows Sockets test suite to aid in testing
their implementations. Finally, Sun Microsystems and FTP Software plan
to host the Windows Sock-A-Thon II in Boston February '93.
Without the contributions of the individuals and corporations involved
in the working group, Windows Sockets would never have been as thoroughly
reviewed and completed as quickly. In just one year, several competitors
in the networking business developed a useful specification with something
to show for it! Many thanks to all which participated, either in person
or on e-mail to the Windows Sockets effort. The authors would like to thank
everyone who participated in any way, and apologize in advance for anyone
we have omitted.
List of contributors:
Martin Hall (Chairman) JSB Corporation martinh@jsbus.com
Mark Towfiq (Coordinator) Microdyne Corporation towfiq@microdyne.com
Geoff Arnold (Editor 1.0) Sun Microsystems geoff@east.sun.com
David Treadwell (Editor 1.1) Microsoft Corporation davidtr@microsoft.com
Henry Sanders Microsoft Corporation henrysa@microsoft.com
J. Allard Microsoft Corporation jallard@microsoft.com
Chris Arap-Bologna Distinct chris@distinct.com
Larry Backman FTP Software backman@ftp.com
Alistair Banks Microsoft Corporation alistair@microsoft.com
Rob Barrow JSB Corporation robb@jsb.co.uk
Carl Beame Beame & Whiteside beame@mcmaster,ca
Dave Beaver Microsoft Corporation dbeaver@microsoft.com
Amatzia BenArtzi NetManage, Inc. amatzia@netmanage.com
Mark Beyer Ungermann-Bass mbeyer@ub.com
Nelson Bolyard Silicon Graphics, Inc. nelson@sgi.com
Pat Bonner Hewlett-Packard p_bonner@cnd.hp.com
Derek Brown FTP Software db@wco.ftp.com
Malcolm Butler ICL mcab@oasis.icl.co.uk
Mike Calbaum Frontier Technologies mike@frontiertech.com
Isaac Chan Microsoft Corporation isaacc@microsoft.com
Khoji Darbani Informix khoji@informix.com
Nestor Fesas Hughes LAN Systems nestor@hls.com
Karanja Gakio FTP Software karanja@ftp.com
Vikas Garg Distinct vikas@distinct.com
Gary Gere Gupta ggere@gupta.com Jim
Gilroy Microsoft Corporation jamesg@microsoft.com
Bill Hayes Hewlett-Packard billh@hpchdpc.cnd.hp.com
Paul Hill MIT pbh@athena.mit.edu
Tmima Koren NetManage, Inc. tmima@netmanage.com
Hoek Law Citicorp law@dcc.tti.com
Graeme Le Roux Moresdawn P/L - Kevin Lewis Novell kevinl@novell.com
Roger Lin 3Com roger_lin@3mail.3com.com
Terry Lister Hewlett-Packard tel@cnd.hp.com
Jeng Long Jiang Wollongong long@twg.com
Lee Murach Network Research lee@nrc.com
Pete Ostenson Microsoft Corporation peteo@microsoft.com
David Pool Spry, Inc. dave@spry.com
Bob Quinn FTP Software rcq@ftp.com Glenn
Reitsma Hughes LAN Systems glennr@hls.com
Brad Rice Age rice@age.com Allen Rochkind
3Com - Jonathan Rosen IBM jrosen@vnet.ibm.com
Steve Stokes Novell stoke@novell.com
Joseph Tsai 3Com joe_tsai@3mail.3com.com
James Van Bokkelen FTP Software jbvb@ftp.com
Miles Wu Wollongong wu@twg.com Boris Yanovsky
NetManage, Inc. boris@netmanage.com
Introduction
What is Windows Sockets
The Windows Sockets specification defines a network programming interface
for Microsoft Windows which is based on the "socket" paradigm popularized
in the Berkeley Software Distribution (BSD) from the University of California
at Berkeley. It encompasses both familiar Berkeley socket style routines
and a set of Windows-specific extensions designed to allow the programmer
to take advantage of the message-driven nature of Windows.
The Windows Sockets Specification is intended to provide a single
API to which application developers can program and multiple network software
vendors can conform. Furthermore, in the context of a particular version
of Microsoft Windows, it defines a binary interface (ABI) such that an
application written to the Windows Sockets API can work with a conformant
protocol implementation from any network software vendor. This specification
thus defines the library calls and associated semantics to which an application
developer can program and which a network software vendor can implement.
Network software which conforms to this Windows Sockets specification
will be considered "Windows Sockets Compliant". Suppliers of interfaces
which are "Windows Sockets Compliant" shall be referred to as "Windows
Sockets Suppliers". To be Windows Sockets Compliant, a vendor must implement
100% of this Windows Sockets specification.
Applications which are capable of operating with any "Windows
Sockets Compliant" protocol implementation will be considered as having
a "Windows Sockets Interface" and will be referred to as "Windows Sockets
Applications".
This version of the Windows Sockets specification defines and
documents the use of the API in conjunction with the Internet Protocol
Suite (IPS, generally referred to as TCP/IP). Specifically, all Windows
Sockets implementations support both stream (TCP) and datagram (UDP) sockets.
While the use of this API with alternative protocol stacks is
not precluded (and is expected to be the subject of future revisions of
the specification), such usage is beyond the scope of this version of the
specification.
Berkeley Sockets
The Windows Sockets Specification has been built upon the Berkeley Sockets
programming model which is the de facto standard for TCP/IP networking.
It is intended to provide a high degree of familiarity for programmers
who are used to programming with sockets in UNIX and other environments,
and to simplify the task of porting existing sockets-based source code.
The Windows Sockets API is consistent with release 4.3 of the Berkeley
Software Distribution (4.3BSD).
Portions of the Windows Sockets specification are derived from
material which is Copyright (c) 1982-1986 by the Regents of the University
of California. All rights are reserved. The Berkeley Software License Agreement
specifies the terms and conditions for redistribution.
Microsoft Windows and Windows-specific extensions
This API is intended to be usable within all implementations and versions
of Microsoft Windows from Microsoft Windows Version 3.0 onwards. It thus
provides for Windows Sockets implementations and Windows Sockets applications
in both 16 and 32 bit operating environments.
Windows Sockets makes provisions for multithreaded Windows processes.
A process contains one or more threads of execution. In the Windows 3.1
non-multithreaded world, a task corresponds to a process with a single
thread. All references to threads in this document refer to actual "threads"
in multithreaded Windows environments. In non multithreaded environments
(such as Windows 3.0), use of the term thread refers to a Windows process.
The Microsoft Windows extensions included in Windows Sockets are
provided to allow application developers to create software which conforms
to the Windows programming model. It is expected that this will facilitate
the creation of robust and high-performance applications, and will improve
the cooperative multitasking of applications within non-preemptive versions
of Windows. With the exception of WSAStartup() and
WSACleanup() their use is not mandatory.
The Status of this Specification
Windows Sockets is an independent specification which was created and exists
for the benefit of application developers and network vendors and, indirectly,
computer users. Each published (non-draft) version of this specification
represents a fully workable API for implementation by network vendors and
programming use by application developers. Discussion of this specification
and suggested improvements continue and are welcomed. Such discussion occurs
mainly via the Internet electronic mail forum winsock@microdyne.com. Meetings
of interested parties occur on an irregular basis. Details of these meetings
are publicized to the electronic mail forum.
Revision History
Windows Sockets Version 1.0
Windows Sockets Version 1.0 represented the results of considerable work
within the vendor and user community as discussed in " Origins
of Windows Sockets". This version of the specification was released
in order that network software suppliers and application developers could
begin to construct implementations and applications which conformed to
the Windows Sockets standard.
Windows Sockets Version 1.1
Windows Sockets Version 1.1 follows the guidelines and structure laid out
by version 1.0, making changes only where absolutely necessary as indicated
by the experiences of a number of companies that created Windows Sockets
implementations based on the version 1.0 specification. Version 1.1 contains
several clarifications and minor fixes to version 1.0. Additionally, the
following more significant changes were incorporated into version 1.1:
-
Inclusion of the gethostname() routine to simplify
retrieval of the host's name and address.
-
Definition of DLL ordinal values below 1000 as reserved for Windows Sockets
and ordinals above 1000 as unrestricted. This allows Windows Sockets vendors
to include private interfaces to their DLLs without risking that the ordinals
choosen will conflict with a future version of Windows Sockets.
-
Addition of a reference count to WSAStartup() and
WSACleanup(), requiring correspondences between
the calls. This allows applications and third-party DLLs to make use of
a Windows Sockets implementation without being concerned about the calls
to these APIs made by the other.
-
Change of return type of inet_addr() from struct
in_addr to unsigned long. This was required due to different
handling of four-byte structure returns between the Microsoft and Borland
C compilers.
-
Change of WSAAsyncSelect() semantics from "edge-triggerred"
to "level-triggerred". The level-triggerred semantics significantly simplify
an application's use of this routine.
-
Change the ioctlsocket() FIONBIO semantics to
fail if a WSAAsyncSelect() call is outstanding
on the socket.
-
Addition of the TCP_NODELAY socket option for RFC 1122 conformance.
Programming with Sockets
Windows Sockets Stack Installation Checking
To detect the presence of one (or many) Windows Sockets implementations
on a system, an application which has been linked with the Windows Sockets
Import Library may simply call the WSAStartup()
routine. If an application wishes to be a little more sophisticated it
can examine the $PATH environment variable and search for instances of
Windows Sockets implementations (WINSOCK.DLL). For each instance it can
issue a LoadLibrary() call and use the WSAStartup()
routine to discover implementation specific data.
This version of the Windows Sockets specification does not attempt
to address explicitly the issue of multiple concurrent Windows Sockets
implementations. Nothing in the specification should be interpreted as
restricting multiple Windows Sockets DLLs from being present and used concurrently
by one or more Windows Sockets applications.
For further details of where to obtain Windows Sockets components,
see " Windows Sockets Components".
Sockets
The following material is derived from the document "An Advanced 4.3BSD
Interprocess Communication Tutorial" by Samuel J. Leffler, Robert S. Fabry,
William N. Joy, Phil Lapsley, Steve Miller, and Chris Torek.
Basic concepts
The basic building block for communication is the socket. A socket is an
endpoint of communication to which a name may be bound. Each socket in
use has a type and an associated process. Sockets exist within communication
domains. A communication domain is an abstraction introduced to bundle
common properties of threads communicating through sockets. Sockets normally
exchange data only with sockets in the same domain (it may be possible
to cross domain boundaries, but only if some translation process is performed).
The Windows Sockets facilities support a single communication domain: the
Internet domain, which is used by processes which communicate using the
Internet Protocol Suite. (Future versions of this specification may include
additional domains.)
Sockets are typed according to the communication properties visible
to a user. Applications are presumed to communicate only between sockets
of the same type, although there is nothing that prevents communication
between sockets of different types should the underlying communication
protocols support this.
Two types of sockets currently are available to a user. A stream socket
provides for the bi-directional, reliable, sequenced, and unduplicated
flow of data without record boundaries.
A datagram socket supports bi-directional flow of data which is not
promised to be sequenced, reliable, or unduplicated. That is, a process
receiving messages on a datagram socket may find messages duplicated, and,
possibly, in an order different from the order in which it was sent. An
important characteristic of a datagram socket is that record boundaries
in data are preserved. Datagram sockets closely model the facilities found
in many contemporary packet switched networks such as Ethernet.
Client-server model
The most commonly used paradigm in constructing distributed applications
is the client/server model. In this scheme client applications request
services from a server application. This implies an asymmetry in establishing
communication between the client and server.
The client and server require a well-known set of conventions before
service may be rendered (and accepted). This set of conventions comprises
a protocol which must be implemented at both ends of a connection. Depending
on the situation, the protocol may be symmetric or asymmetric. In a symmetric
protocol, either side may play the master or slave roles. In an asymmetric
protocol, one side is immutably recognized as the master, with the other
as the slave. An example of a symmetric protocol is the TELNET protocol
used in the Internet for remote terminal emulation. An example of an asymmetric
protocol is the Internet file transfer protocol, FTP. No matter whether
the specific protocol used in obtaining a service is symmetric or asymmetric,
when accessing a service there is a ``client process'' and a ``server process''.
A server application normally listens at a well-known address for service
requests. That is, the server process remains dormant until a connection
is requested by a client's connection to the server's address. At such
a time the server process ``wakes up'' and services the client, performing
whatever appropriate actions the client requests of it. While connection-based
services are the norm, some services are based on the use of datagram sockets.
Out-of-band data
Note: The following discussion of out-of-band data, also referred to as
TCP Urgent data, follows the model used in the Berkeley software distribution.
Users and implementors should be aware of the fact that there are at present
two conflicting interpretations of RFC 793 (in which the concept is introduced),
and that the implementation of out-of-band data in the Berkeley Software
Distribution does not conform to the Host Requirements laid down in RFC
1122. To minimize interoperability problems, applications writers are advised
not to use out-of-band data unless this is required in order to interoperate
with an existing service. Windows Sockets suppliers are urged to document
the out-of-band semantics (BSD or RFC 1122) which their product implements.
It is beyond the scope of this specification to mandate a particular set
of semantics for out-of-band data handling.
The stream socket abstraction includes the notion of ``out of
band'' data. Out-of-band data is a logically independent transmission channel
associated with each pair of connected stream sockets. Out-of-band data
is delivered to the user independently of normal data. The abstraction
defines that the out-of-band data facilities must support the reliable
delivery of at least one out-of-band message at a time. This message may
contain at least one byte of data, and at least one message may be pending
delivery to the user at any one time. For communications protocols which
support only in-band signaling (i.e. the urgent data is delivered in sequence
with the normal data), the system normally extracts the data from the normal
data stream and stores it separately. This allows users to choose between
receiving the urgent data in order and receiving it out of sequence without
having to buffer all the intervening data. It is possible to ``peek'' at
out-of-band data.
An application may prefer to process out-of-band data "in-line", as
part of the normal data stream. This is achieved by setting the socket
option SO_OOBINLINE (see setsockopt()). In this
case, the application may wish to determine whether any of the unread data
is "urgent" (the term usually applied to in-line out-of-band data). To
facilitate this, the Windows Sockets implementation will maintain a logical
"mark" in the data stream to indicate the point at which the out-of-band
data was sent. An application can use the SIOCATMARK ioctlsocket()
command to determine whether there is any unread data preceding the mark.
For example, it might use this to resynchronize with its peer by ensuring
that all data up to the mark in the data stream is discarded when appropriate.
The WSAAsyncSelect() routine is particularly
well suited to handling notification of the presence of out-of-band-data.
Broadcasting
By using a datagram socket, it is possible to send broadcast packets on
many networks supported by the system. The network itself must support
broadcast: the system provides no simulation of broadcast in software.
Broadcast messages can place a high load on a network, since they force
every host on the network to service them. Consequently, the ability to
send broadcast packets has been limited to sockets which are explicitly
marked as allowing broadcasting. Broadcast is typically used for one of
two reasons: it is desired to find a resource on a local network without
prior knowledge of its address, or important functions such as routing
require that information be sent to all accessible neighbors.
The destination address of the message to be broadcast depends
on the network(s) on which the message is to be broadcast. The Internet
domain supports a shorthand notation for broadcast on the local network,
the address INADDR_BROADCAST. Received broadcast messages contain the senders
address and port, as datagram sockets must be bound before use.
Some types of network support the notion of different types of
broadcast. For example, the IEEE 802.5 token ring architecture supports
the use of link-level broadcast indicators, which control whether broadcasts
are forwarded by bridges. The Windows Sockets specification does not provide
any mechanism whereby an application can determine the type of underlying
network, nor any way to control the semantics of broadcasting.
Byte Ordering
The Intel byte ordering is like that of the DEC VAX, and therefore differs
from the Internet and 68000-type processor byte ordering. Thus care must
be taken to ensure correct orientation.
Any reference to IP addresses or port numbers passed to or from
a Windows Sockets routine must be in network order. This includes the IP
address and port fields of a struct sockaddr_in (but not the sin_family
field).
Consider an application which normally contacts a server on the
TCP port corresponding to the "time" service, but which provides a mechanism
for the user to specify that an alternative port is to be used. The port
number returned by getservbyname() is already
in network order, which is the format required constructing an address,
so no translation is required. However if the user elects to use a different
port, entered as an integer, the application must convert this from host
to network order (using the htons() function) before
using it to construct an address. Conversely, if the application wishes
to display the number of the port within an address (returned via, e.g.,
getpeername()), the port number must be converted
from network to host order (using ntohs()) before
it can be displayed.
Since the Intel and Internet byte orders are different, the conversions
described above are unavoidable. Application writers are cautioned that
they should use the standard conversion functions provided as part of the
Windows Sockets API rather than writing their own conversion code, since
future implementations of Windows Sockets are likely to run on systems
for which the host order is identical to the network byte order. Only applications
which use the standard conversion functions are likely to be portable.
Socket Options
The socket options supported by Windows Sockets are listed in the pages
describing setsockopt() and getsockopt().
A Windows Sockets implementation must recognize all of these options, and
(for getsockopt()) return plausible values for
each. The default value for each option is shown in the following table.
Value Type Meaning Default Note
--------------- --------------- ----------------------- --------------- ----
SO_ACCEPTCON BOOL Socket is listen()ing.
FALSE unless a listen()
has been performed
SO_BROADCAST BOOL Socket is configured FALSE
for the transmission of
broadcast messages.
SO_DEBUG BOOL Debugging is enabled. FALSE (i)
SO_DONTLINGER BOOL If true, the SO_LINGER TRUE
option is disabled..
SO_DONTROUTE BOOL Routing is disabled. FALSE (i)
SO_ERROR int Retrieve error status 0
and clear.
SO_KEEPALIVE BOOL Keepalives are being FALSE
sent.
SO_LINGER struct linger Returns the current l_onoff is 0
FAR * linger options.
SO_OOBINLINE BOOL Out-of-band data is FALSE
being received in the
normal data stream.
SO_RCVBUF int Buffer size for Implementation (i)
receives dependant.
SO_REUSEADDR BOOL The address to which FALSE
this socket is bound
can be used by others.
SO_SNDBUF int Buffer size for sends Implementation (i)
dependant.
SO_TYPE int The type of the socket As created
(e.g. SOCK_STREAM). via socket()
TCP_NODELAY BOOL Disables the Nagle Implementation
algorithm for send dependant.
coalescing.
Notes:
(i) An implementation may silently ignore this option on setsockopt()
and return a constant value for getsockopt(),
or it may accept a value for setsockopt() and
return the corresponding value in getsockopt()
without using the value in any way.
Database Files
The getXbyY() and WSAAsyncGetXByY()
classes of routines are provided for retrieving network specific information.
The getXbyY() routines were originally designed (in the first Berkeley
UNIX releases) as mechanisms for looking up information in text databases.
Although the information may be retrieved by the Windows Sockets implementation
in different ways, a Windows Sockets application requests such information
in a consistent manner through either the getXbyY() or the WSAAsyncGetXByY()
class of routines.
Deviation from Berkeley Sockets
There are a few limited instances where the Windows Sockets API has had
to divert from strict adherence to the Berkeley conventions, usually because
of difficulties of implementation in a Windows environment.
socket data type and error values
select() and FD_*
Error codes -- errno, h_errno
& WSAGetLastError()
Pointers
Renamed functions
Blocking routines &
EINPROGRESS
Maximum number of sockets supported
Include files
Return values on API failure
socket data type and error values
A new data type, SOCKET, has been defined. The definition of this type
was necessary for future enhancements to the Windows Sockets specification,
such as being able to use sockets as file handles in Windows NT. Definition
of this type also facilitates porting of applications to a Win/32 environment,
as the type will automatically be promoted from 16 to 32 bits.
In UNIX, all handles, including socket handles, are small, non-negative
intergers, and some applications make assumptions that this will be true.
Windows Sockets handles have no restrictions, other than that the value
INVALID_SOCKET is not a valid socket. Socket handles may take any value
in the range 0 to INVALID_SOCKET-1.
Because the SOCKET type is unsigned, compiling existing source
code from, for example, a UNIX environment may lead to compiler warnings
about signed/unsigned data type mismatches.
This means, for example, that checking for errors when the socket()
and accept() routines return should not be
done by comparing the return value with -1, or seeing if the value is negative
(both common, and legal, approaches in BSD). Instead, an application should
use the manifest constant INVALID_SOCKET as defined in winsock.h.
For example:
TYPICAL BSD STYLE:
s = socket(...);
if (s == -1) /* or s < 0 */
{...}
PREFERRED STYLE:
s = socket(...);
if (s == INVALID_SOCKET)
{...}
select() and FD_*
Because a SOCKET is no longer represented by the UNIX-style "small non-negative
integer", the implementation of the select() function
was changed in the Windows Sockets API. Each set of descriptors is still
represented by the fd_set type, but instead of being stored as a bitmask
the set is implemented as an array of SOCKETs.. To avoid potential problems,
applications must adhere to the use of the FD_XXX macros to set,
initialize, clear, and check the fd_set structures.
Error codes -- errno, h_errno &
WSAGetLastError()
Error codes set by the Windows Sockets implementation are NOT made
available via the errno variable. Additionally, for the getXbyY()
class of functions, error codes are NOT made available via the h_errno
variable. Instead, error codes are accessed by using the WSAGetLastError()
API. This function is provided in Windows Sockets as a precursor (and eventually
an alias) for the Win32 function GetLastError(). This is intended
to provide a reliable way for a thread in a multi-threaded process to obtain
per-thread error information.
For compatibility with BSD, an application may choose to include
a line of the form:
#define errno WSAGetLastError()
This will allow networking code which was written to use the global errno
to work correctly in a single-threaded environment. There are, obviously,
some drawbacks. If a source file includes code which inspects errno for
both socket and non-socket functions, this mechanism cannot be used. Furthermore,
it is not possible for an application to assign a new value to errno. (In
Windows Sockets the function WSASetLastError()
may be used for this purpose.)
TYPICAL BSD STYLE:
r = recv(...);
if (r == -1
&& errno == EWOULDBLOCK)
{...}
PREFERRED STYLE:
r = recv(...);
if (r == -1 /* (but see below) */
&& WSAGetLastError() == EWOULDBLOCK)
{...}
Although error constants consistent with 4.3 Berkeley Sockets are provided
for compatibility purposes, applications should, where possible, use the
"WSA" error code definitions. For example, a more accurate version of the
above source code fragment is:
r = recv(...);
if (r == -1
&& WSAGetLastError() == WSAEWOULDBLOCK)
{...}
Pointers
All pointers used by applications with Windows Sockets should be FAR. To
facilitate this, data type definitions such as LPHOSTENT are provided.
Renamed functions
In two cases it was necessary to rename functions which are used in Berkeley
Sockets in order to avoid clashes with other APIs.
close() & closesocket()
In Berkeley Sockets, sockets are represented by standard file descriptors,
and so the close() function can be used to close sockets as well
as regular files. While nothing in the Windows Sockets API prevents an
implementation from using regular file handles to identify sockets, nothing
requires it either. Socket descriptors are not presumed to correspond to
regular file handles, and file operations such as read(), write(),
and close() cannot be assumed to work correctly when applied to
socket descriptors.. Sockets must be closed by using the closesocket()
routine. Using the close() routine to close a socket is incorrect
and the effects of doing so are undefined by this specification.
ioctl() & ioctlsocket()
Various C language run-time systems use the ioctl() routine
for purposes unrelated to Windows Sockets. For this reason we have defined
the routine ioctlsocket() which is used to handle
socket functions which in the Berkeley Software Distribution are performed
using ioctl() and fcntl().
Blocking routines & EINPROGRESS
Although blocking operations on sockets are supported under Windows Sockets,
their use is strongly discouraged. Programmers who are constrained to use
blocking mode -- for example, as part of an existing application which
is to be ported -- should be aware of the semantics of blocking operations
in Windows Sockets. See Blocking/Non
blocking & Data Volatility for more details.
Maximum number of sockets supported
The maximum number of sockets supported by a particular Windows Sockets
supplier is implementation specific. An application should make no assumptions
about the availability of a certain number of sockets. This topic is addressed
further in the section on WSAStartup(). However,
independent of the number of sockets supported by a particular implementation
is the issue of the maximum number of sockets which an application can
actually make use of.
The maximum number of sockets which a Windows Sockets application
can make use of is determined at compile time by the manifest constant
FD_SETSIZE. This value is used in constructing the fd_set structures used
in select(). The default value in winsock.h
is 64. If an application is designed to be capable of working with more
than 64 sockets, the implementor should define the manifest FD_SETSIZE
in every source file before including winsock.h.
One way of doing this may be to include the definition within the compiler
options in the makefile, for example adding -DFD_SETSIZE=128 as an option
to the compiler command line for Microsoft C. It must be emphasized that
defining FD_SETSIZE as a particular value has no effect on the actual number
of sockets provided by a Windows Sockets implementation.
Include files
For ease of portability of existing Berkeley sockets based source code,
a number of standard Berkeley include files are supported. However, these
Berkeley header files merely include the winsock.h
include file, and it is therefore sufficient (and recommended) that Windows
Sockets application source files should simply include winsock.h.
Return values on API failure
The manifest constant SOCKET_ERROR is provided for checking API failure.
Although use of this constant is not mandatory, it is recommended. The
following example illustrates the use of the SOCKET_ERROR constant:
TYPICAL BSD STYLE:
r = recv(...);
if (r == -1 /* or r < 0 */
&& errno == EWOULDBLOCK)
{...}
PREFERRED STYLE:
r = recv(...);
if (r == SOCKET_ERROR
&& WSAGetLastError() == WSAEWOULDBLOCK)
{...}
Raw Sockets
The Windows Sockets specification does not mandate that a Windows Sockets
DLL support raw sockets, that is, sockets opened with SOCK_RAW. However,
a Windows Sockets DLL is allowed and encouraged to supply raw socket support.
A Windows Sockets-compliant application that wishes to use raw sockets
should attempt to open the socket with the socket() call (see section
4.1.23), and if it fails either attempt to use another socket type or indicate
the failure to the user.
Windows Sockets in Multithreaded
Versions of Windows
The Windows Sockets interface is designed to work for both single-threaded
versions of Windows (such as Windows 3.1) and future multithreaded versions
of Windows (such as Windows NT). In a multithreaded environment the sockets
interface is basically the same, but the author of a multithreaded application
must be aware that it is the responsibility of the application, not the
Windows Sockets implementation, to synchronize access to a socket between
threads. This is the same rule as applies to other forms of I/O such as
file I/O. Failure to synchronize calls on a socket leads to unpredictable
results; for example if there are two simultaneous calls to send(), there
is no guarantee as to the order in which the data will be sent.
Closing a socket in one thread that has an outstanding blocking call
on the same socket in another thread will cause the blocking call to fail
with WSAEINTR, just as if the operation were cancelled. This also applies
if there is a select() call outstanding and the application
closes one of the sockets being selected.
There is no default blocking hook installed in preemptive multithreaded
versions of Windows. This is because the machine will not be blocked if
a single application is waiting for an operation to complete and hence
not calling PeekMessage() or GetMessage() which cause the application to
yield in nonpremptive Windows. However, for backwards compatibility the
WSASetBlockingHook() call is implemented in multithreaded versions of Windows,
and any application whose behavior depends on the default blocking hook
may install their own blocking hook which duplicates the default hook's
semantics, if desired.
Socket Library Overview
Socket Functions
The Windows Sockets specification includes the following Berkeley-style
socket routines:
accept() An incoming connection is acknowledged
and associated with an immediately created socket. The original socket
is returned to the listening state.
bind() Assign a local name to an unnamed socket.
closesocket() Remove a socket descriptor
from the per-process object reference table. Only blocks if SO_LINGER is
set.
connect() Initiate a connection on the
specified socket.
getpeername() Retrieve the name of
the peer connected to the specified socket descriptor.
getsockname() Retrieve the current
name for the specified socket
getsockopt() Retrieve options associated
with the specified socket descriptor.
htonl() Convert a 32-bit quantity from host
byte order to network byte order.
htons() Convert a 16-bit quantity from host
byte order to network byte order.
inet_addr() Converts a character string
representing a number in the Internet standard ``.'' notation to an Internet
address value.
inet_ntoa() Converts an Internet address
value to an ASCII string in ``.'' notation i.e. ``a.b.c.d''.
ioctlsocket() Provide control for descriptors.
listen() Listen for incoming connections
on a specified socket.
ntohl() Convert a 32-bit quantity from network
byte order to host byte order.
ntohs() Convert a 16-bit quantity from network
byte order to host byte order.
recv()* Receive data from a connected socket.
recvfrom()* Receive data from either a
connected or unconnected socket.
select()* Perform synchronous I/O multiplexing.
send()* Send data to a connected socket.
sendto()* Send data to either a connected
or unconnected socket.
setsockopt() Store options associated
with the specified socket descriptor.
shutdown() Shut down part of a full-duplex
connection.
socket() Create an endpoint for communication
and return a socket descriptor.
* The routine can block if acting on a blocking socket.
Blocking/Non blocking &
Data Volatility
One major issue in porting applications from a Berkeley sockets environment
to a Windows environment involves "blocking"; that is, invoking a function
which does not return until the associated operation is completed. The
problem arises when the operation may take an arbitrarily long time to
complete: an obvious example is a recv() which may
block until data has been received from the peer system. The default behavior
within the Berkeley sockets model is for a socket to operate in a blocking
mode unless the programmer explicitly requests that operations be treated
as non-blocking. It is strongly recommended that programmers use
the nonblocking (asynchronous) operations if at all possible, as they work
significantly better within the nonpreemptive Windows environment. Use
blocking operations only if absolutely necessary, and carefully read and
understand this section if you must use blocking operations.
Even on a blocking socket, some operations (e.g. bind(),
getsockopt(), getpeername())
can be completed immediately. For such operations there is no difference
between blocking and non-blocking operation. Other operations (e.g. recv())
may be completed immediately or may take an arbitrary time to complete,
depending on various transport conditions. When applied to a blocking socket,
these operations are referred to as blocking operations. All routines which
can block are listed with an asterisk in the tables above and below.
Within a Windows Sockets implementation, a blocking operation
which cannot be completed immediately is handled as follows. The DLL initiates
the operation, and then enters a loop in which it dispatches any Windows
messages (yielding the processor to another thread if necessary) and then
checks for the completion of the Windows Sockets function. If the function
has completed, or if WSACancelBlockingCall()
has been invoked, the blocking function completes with an appropriate result.
Refer to WSASetBlockingHook(), for a complete
description of this mechanism, including pseudocode for the various functions.
If a Windows message is received for a process for which a blocking
operation is in progress, there is a risk that the application will attempt
to issue another Windows Sockets call. Because of the difficulty of managing
this condition safely, the Windows Sockets specification does not support
such application behavior. Two functions are provided to assist the programmer
in this situation. WSAIsBlocking() may be called
to determine whether or not a blocking Windows Sockets call is in progress.
WSACancelBlockingCall() may be called
to cancel an in-progress blocking call, if any. Any other Windows Sockets
function which is called in this situation will fail with the error WSAEINPROGRESS.
It should be emphasized that this restriction applies to both blocking
and non-blocking operations.
Although this mechanism is sufficient for simple applications,
it cannot support the complex message-dispatching requirements of more
advanced applications (for example, those using the MDI model). For such
applications, the Windows Sockets API includes the function WSASetBlockingHook(),
which allows the programmer to define a special routine which will be called
instead of the default message dispatch routine described above.
The Windows Sockets DLL will call the blocking hook function only
if all of the following are true: the routine is one which is defined as
being able to block, the specified socket is a blocking socket, and the
request cannot be completed immediately. (A socket is set to blocking by
default, but the IOCTL FIONBIO and WSAAsyncSelect()
both set a socket to nonblocking mode.) If an application uses only non-blocking
sockets and uses the WSAAsyncSelect() and/or
the WSAAsyncGetXByY() routines
instead of select() and the getXbyY()
routines, then the blocking hook will never be called and the application
does not need to be concerned with the reentrancy issues the blocking hook
can introduce.
If an application invokes an asynchronous or non-blocking operation
which takes a pointer to a memory object (e.g. a buffer, or a global variable)
as an argument, it is the responsibility of the application to ensure that
the object is available to the Windows Sockets implementation throughout
the operation. The application must not invoke any Windows function which
might affect the mapping or addressability of the memory involved. In a
multithreaded system, the application is also responsible for coordinating
access to the object using appropriate synchronization mechanisms. A Windows
Sockets implementation cannot, and will not, address these issues. The
possible consequences of failing to observe these rules are beyond the
scope of this specification.
Database Functions
The Windows Sockets specification defines the following "database" routines.
As noted earlier, a Windows Sockets supplier may choose to implement these
in a manner which does not depend on local database files. The pointer
returned by certain database routines such as gethostbyname()
points to a structure which is allocated by the Windows Sockets library.
The data which is pointed to is volatile and is good only until the next
Windows Sockets API call from that thread. Additionally, the application
must never attempt to modify this structure or to free any of its components.
Only one copy of this structure is allocated for a thread, and so the application
should copy any information which it needs before issuing any other Windows
Sockets API calls.
gethostbyaddr()* Retrieve the name(s) and
address corresponding to a network address.
gethostname() Retrieve the name of
the local host.
gethostbyname()* Retrieve the name(s)
and address corresponding to a host name.
getprotobyname()* Retrieve the protocol
name and number corresponding to a protocol name.
getprotobynumber()* Retrieve the
protocol name and number corresponding to a protocol number.
getservbyname()* Retrieve the service
name and port corresponding to a service name.
getservbyport()* Retrieve the service
name and port corresponding to a port.
* The routine can block under some circumstances.
Microsoft Windows-specific Extension
Functions
The Windows Sockets specification provides a number of extensions to the
standard set of Berkeley Sockets routines. Principally, these extended
APIs allow message-based, asynchronous access to network events. While
use of this extended API set is not mandatory for socket-based programming
(with the exception of WSAStartup() and WSACleanup()),
it is recommended for conformance with the Microsoft Windows programming
paradigm.
Asynchronous select() Mechanism
Asynchronous Support Routines
Hooking Blocking Methods
Error Handling
Accessing a Windows Sockets DLL
from an Intermediate DLL
Internal Use of Messages
by Windows Sockets Implementations
Private API Interfaces
WSAAsyncGetHostByAddr() A set of functions
which provide asynchronous
WSAAsyncGetHostByName() versions
of the standard Berkeley
WSAAsyncGetProtoByName() getXbyY()
functions. For example, the
WSAAsyncGetProtoByNumber()
WSAAsyncGetHostByName() function provides an asynchronous message
based
WSAAsyncGetServByName() implementation
of the standard Berkeley
WSAAsyncGetServByPort() gethostbyname()
function.
WSAAsyncSelect() Perform asynchronous
version of select()
WSACancelAsyncRequest() Cancel
an outstanding instance of a WSAAsyncGetXByY() function.
WSACancelBlockingCall() Cancel
an outstanding "blocking" API call
WSACleanup() Sign off from the underlying
Windows Sockets DLL.
WSAGetLastError() Obtain details
of last Windows Sockets API error
WSAIsBlocking() Determine if the underlying
Windows Sockets DLL is already blocking an existing call for this thread
WSASetBlockingHook() "Hook" the
blocking method used by the underlying Windows Sockets implementation
WSASetLastError() Set the error to
be returned by a subsequent WSAGetLastError()
WSAStartup() Initialize the underlying
Windows Sockets DLL.
WSAUnhookBlockingHook() Restore
the original blocking function
Asynchronous select() Mechanism
The WSAAsyncSelect() API allows an application
to register an interest in one or many network events. This API is provided
to supersede the need to do polled network I/O. Any situation in which
select() or non-blocking I/O routines (such as send()
and recv()) are either already used or are being considered
is usually a candidate for the WSAAsyncSelect()
API. When declaring interest in such condition(s), you supply a window
handle to be used for notification. The corresponding window then receives
message-based notification of the conditions in which you declared an interest.
WSAAsyncSelect() allows interest to
be declared in the following conditions for a particular socket:
-
Socket readiness for reading
-
Socket readiness for writing
-
Out-of-band data ready for reading
-
Socket readiness for accepting incoming connection
-
Completion of non-blocking connect()
-
Connection closure
Asynchronous Support Routines
The asynchronous "database" functions allow applications to request information
in an asynchronous manner. Some network implementations and/or configurations
perform network based operations to resolve such requests. The WSAAsyncGetXByY()
functions allow application developers to request services which would
otherwise block the operation of the whole Windows environment if the standard
Berkeley function were used. The WSACancelAsyncRequest()
function allows an application to cancel any outstanding asynchronous request.
Hooking Blocking Methods
As noted in Blocking/Non blocking
& Data Volatility, Windows Sockets implements blocking operations
in such a way that Windows message processing can continue, which may result
in the application which issued the call receiving a Windows message. In
certain situations an application may want to influence or change the way
in which this pseudo-blocking process is implemented. The WSASetBlockingHook()
provides the ability to substitute a named routine which the Windows Sockets
implementation is to use when relinquishing the processor during a "blocking"
operation.
Error Handling
For compatibility with thread-based environments, details of API errors
are obtained through the WSAGetLastError()
API. Although the accepted "Berkeley-Style" mechanism for obtaining socket-based
network errors is via "errno", this mechanism cannot guarantee the integrity
of an error ID in a multi-threaded environment. WSAGetLastError()
allows you to retrieve an error code on a per thread basis.
WSAGetLastError() returns error codes
which avoid conflict with standard Microsoft C error codes. Certain error
codes returned by certain Windows Sockets routines fall into the standard
range of error codes as defined by Microsoft C. If you are NOT using an
application development environment which defines error codes consistent
with Microsoft C, you are advised to use the Windows Sockets error codes
prefixed by "WSA" to ensure accurate error code detection.
Note that this specification defines a recommended set of error
codes, and lists the possible errors which may be returned as a result
of each function. It may be the case in some implementations that other
Windows Sockets error codes will be returned in addition to those listed,
and applications should be prepared to handle errors other than those enumerated
under each API description. However a Windows Sockets implementation must
not return any value which is not enumerated in the table of legal Windows
Sockets errors given in Error Codes.
Accessing a Windows Sockets DLL from
an Intermediate DLL
A Windows Sockets DLL may be accessed both directly from an application
and through an "intermediate" DLL. An example of such an intermediate DLL
would be a virtual network API layer that supports generalized network
functionality for applications and uses Windows Sockets. Such a DLL could
be used by several applications simultaneously, and the DLL must take special
precautions with respect to the WSAStartup() and WSACleanup()
calls to ensure that these routines are called in the context of each
task that will make Windows Sockets calls. This is because the Windows
Sockets DLL will need a call to WSAStartup() for each task in order
to set up task-specific data structures, and a call to WSACleanup()
to free any resources allocated for the task.
There are (at least) two ways to accomplish this. The simplest method
is for the intermediate DLL to have calls similiar to WSAStartup()
and WSACleanup() that applications call as appropriate.
The DLL would then call WSAStartup() or WSACleanup()
from within these routines. Another mechanism is for the intermediate DLL
to build a table of task handles, which are obtained from the GetCurrentTask()
Windows API, and at each entry point into the intermediate DLL check
whether WSAStartup() has been called for the current
task, then call WSAStartup() if necessary.
If a DLL makes a blocking call and does not install its own blocking
hook, then the DLL author must be aware that control may be returned to
the application either by an application-installed blocking hook or by
the default blocking hook. Thus, it is possible that the application will
cancel the DLL's blocking operation via WSACancelBlockingCall().
If this occurs, the DLL's blocking operation will fail with the error code
WSAEINTR, and the DLL must return control to the calling task as quickly
as possible, as the used has likely pressed a cancel or close button and
the task has requested control of the CPU. It is recommended that DLLs
which make blocking calls install their own blocking hooks with WSASetBlockingHook()
to prevent unforeseen interactions between the application and the DLL.
Note that this is not necessary for DLLs in Windows NT because of its
different process and DLL structure. Under Windows NT, the intermediate
DLL could simply call WSAStartup() in its DLL initialization
routine, which is called whenever a new process which uses the DLL starts.
Internal use of Messages by Windows
Sockets Implementations
In order to implement Windows Sockets purely as a DLL, it may be necessary
for the DLL to post messages internally for communication and timing. This
is perfectly legal; however, a Windows Sockets DLL must not post messages
to a window handle opened by a client application except for those messages
requested by the application. A Windows Sockets DLL that needs to use messages
for its own purposes must open a hidden window and post any necessary messages
to the handle for that window.
Private API Interfaces
The winsock.def file lists the ordinals
defined for the Windows Sockets APIs. In addition to the ordinal values
listed, all ordinals 999 and below are reserved for future Windows Sockets
use. It may be convenient for a Windows Sockets implementation to export
additional, private interfaces from the Windows Sockets DLL. This is perfectly
acceptable, as long as the ordinals for these exports are above 1000. Note
that any application that uses a particular Windows Sockets DLL's private
APIs will most likely not work on any other vendor's Windows Sockets implementation.
Only the APIs defined in this document are guaranteed to be present in
every Windows Sockets implementation.
If an application uses private interfaces of a particular vendor's
Windows Sockets DLL, it is recommended that the DLL not be statically linked
with the application but rather dynamically loaded with the Windows routines
LoadLibrary() and GetProcAddress(). This allows the application
to give an informative error message if it is run on a system with a Windows
Sockets DLL that does not support the same set of extended functionality.
Socket Library Reference
Socket Routines
This chapter presents the socket library routines in alphabetical order,
and describes each routine in detail.
In each routine it is indicated that the header file winsock.h
must be included. Header Files lists the Berkeley-compatible
header files which are supported. These are provided for compatibility
purposes only, and each of them will simply include winsock.h.
The Windows header file windows.h is also needed, but winsock.h
will include it if necessary.
accept()
Description
Accept a connection on a socket.
#include <winsock.h>
SOCKET PASCAL FAR accept ( SOCKET s, struct sockaddr
FAR * addr, int FAR * addrlen);
-
s
-
A descriptor identifying a socket which is listening for connections after
a listen().
-
addr
-
An optional pointer to a buffer which receives the address of the connecting
entity, as known to the communications layer. The exact format of the addr
argument is determined by the address family established when the socket
was created.
-
addrlen
-
A optional pointer to an integer which contains the length of the address
addr.
Remarks
This routine extracts the first connection on the queue of pending connections
on s, creates a new socket with the same properties as s
and returns a handle to the new socket. If no pending connections are present
on the queue, and the socket is not marked as non-blocking, accept()
blocks the caller until a connection is present. If the socket is marked
non-blocking and no pending connections are present on the queue, accept()
returns an error as described below. The accepted socket may not be used
to accept more connections. The original socket remains open.
The argument addr is a result parameter that is filled in with
the address of the connecting entity, as known to the communications layer.
The exact format of the addr parameter is determined by the address
family in which the communication is occurring. The addrlen is a
value-result parameter; it should initially contain the amount of space
pointed to by addr; on return it will contain the actual length
(in bytes) of the address returned. This call is used with connection-based
socket types such as SOCK_STREAM. If addr and/or addrlen
are equal to NULL, then no information about the remote address of the
accepted socket is returned.
Return Value
If no error occurs, accept() returns a value of type SOCKET which
is a descriptor for the accepted packet. Otherwise, a value of INVALID_SOCKET
is returned, and a specific error code may be retrieved by calling WSAGetLastError().
The integer referred to by addrlen initially contains the
amount of space pointed to by addr. On return it will contain the
actual length in bytes of the address returned.
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEFAULT
-
The addrlen argument is too small (less than the sizeof a struct
sockaddr).
-
WSAEINTR
-
The (blocking) call was canceled via WSACancelBlockingCall()
-
WSAEINPROGRESS
-
A blocking Windows Sockets call is in progress.
-
WSAEINVAL
-
listen() was not invoked prior to accept().
-
WSAEMFILE
-
The queue is empty upon entry to accept() and there are no descriptors
available.
-
WSAENOBUFS
-
No buffer space is available.
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAEOPNOTSUPP
-
The referenced socket is not a type that supports connection-oriented service.
-
WSAEWOULDBLOCK
-
The socket is marked as non-blocking and no connections are present to
be accepted.
See Also
bind(), connect(),
listen(), select(),
socket(), WSAAsyncSelect().
bind()
Description
Associate a local address with a socket.
#include <winsock.h>
int PASCAL FAR bind ( SOCKET s, const
struct sockaddr FAR * name, int namelen);
-
s
-
A descriptor identifying an unbound socket.
-
name
-
The address to assign to the socket. The sockaddr structure is defined
as follows:
struct sockaddr {
u_short sa_family;
char sa_data[14];
};
-
namelen
-
The length of the name.
Remarks
This routine is used on an unconnected datagram or stream socket, before
subsequent connect()s or listen()s.
When a socket is created with socket(), it exists
in a name space (address family), but it has no name assigned. bind()
establishes the local association (host address/port number) of the socket
by assigning a local name to an unnamed socket.
In the Internet address family, a name consists of several components.
For SOCK_DGRAM and SOCK_STREAM, the name consists of three parts: a host
address, the protocol number (set implicitly to UDP or TCP, respectively),
and a port number which identifies the application. If an application does
not care what address is assigned to it, it may specify an Internet address
equal to INADDR_ANY, a port equal to 0, or both. If the Internet address
is equal to INADDR_ANY, any appropriate network interface will be used;
this simplifies application programming in the presence of multi-homed
hosts. If the port is specified as 0, the Windows Sockets implementation
will assign a unique port to the application with a value between 1024
and 5000. The application may use getsockname()
after bind() to learn the address that has been assigned to it,
but note that getsockname() will not necessarily fill in the Internet
address until the socket is connected, since several Internet addresses
may be valid if the host is multi-homed.
If an application desires to bind to an arbitrary port outside
of the range 1024 to 5000, such as the case of rsh which must bind to any
reserved port, code similar to the following may be used:
SOCKADDR_IN sin;
SOCKET s;
u_short alport = IPPORT_RESERVED;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
for (;;) {
sin.sin_port = htons(alport);
if (bind(s, (LPSOCKADDR)&sin, sizeof (sin)) == 0) {
/* it worked */
}
if ( GetLastError() != WSAEADDRINUSE) {
/* fail */
}
alport--;
if (alport == IPPORT_RESERVED/2 ) {
/* fail--all unassigned reserved ports are */
/* in use. */
}
}
Return Value
If no error occurs, bind() returns 0. Otherwise, it returns SOCKET_ERROR,
and a specific error code may be retrieved by calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEADDRINUSE
-
The specified address is already in use. (See the SO_REUSEADDR socket option
under setsockopt().)
-
WSAEFAULT
-
The namelen argument is too small (less than the size of a struct
sockaddr).
-
WSAEINTR
-
The (blocking) call was canceled via WSACancelBlockingCall()
-
WSAEINPROGRESS
-
A blocking Windows Sockets call is in progress.
-
WSAEAFNOSUPPORT
-
The specified address family is not supported by
-
this protocol.
-
WSAEINVAL
-
The socket is already bound to an address.
-
WSAENOBUFS
-
Not enough buffers available, too many connections.
-
WSAENOTSOCK
-
The descriptor is not a socket.
See Also connect(), listen(),
getsockname(), setsockopt(),
socket(), WSACancelBlockingCall().
closesocket()
Description
Close a socket.
#include <winsock.h>
int FAR PASCAL closesocket ( SOCKET s);
-
s
-
A descriptor identifying a socket.
Remarks
This function closes a socket. More precisely, it releases the socket descriptor
s, so that further references to s will fail with the error
WSAENOTSOCK. If this is the last reference to the underlying socket, the
associated naming information and queued data are discarded.
The semantics of closesocket() are affected by the socket
options SO_LINGER and SO_DONTLINGER as follows:
Option Interval Type of close Wait for close?
--------------- --------------- ----------------------- ----------------
SO_DONTLINGER Don't care Graceful No
SO_LINGER Zero Hard No
SO_LINGER Non-zero Graceful Yes
If SO_LINGER is set (i.e. the l_onoff field of the linger structure
is non-zero; see Socket Options, getsockopt()
and setsockopt()) with a zero timeout interval
(l_linger is zero), closesocket() is not blocked even if
queued data has not yet been sent or acknowledged. This is called a "hard"
close, because the socket is closed immediately, and any unsent data is
lost. Any recv() call on the remote side of the circuit
can fail with WSAECONNRESET.
If SO_LINGER is set with a non-zero timeout interval, the closesocket()
call blocks until the remaining data has been sent or until the timeout
expires. This is called a graceful disconnect. Note that if the socket
is set to non-blocking and SO_LINGER is set to a non-zero timeout, the
call to closesocket() will fail with an error of WSAEWOULDBLOCK.
If SO_DONTLINGER is set on a stream socket (i.e. the l_onoff field
of the linger structure is zero; see Socket
Options, getsockopt() and setsockopt()),
the closesocket() call will return immediately. However, any data
queued for transmission will be sent if possible before the underlying
socket is closed. This is also called a graceful disconnect. Note that
in this case the Windows Sockets implementation may not release the socket
and other resources for an arbitrary period, which may affect applications
which expect to use all available sockets.
Return Value
If no error occurs, closesocket() returns 0. Otherwise, a value
of SOCKET_ERROR is returned, and a specific error code may be retrieved
by calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAEINPROGRESS
-
A blocking Windows Sockets call is in progress.
-
WSAEINTR
-
The (blocking) call was canceled via WSACancelBlockingCall().
-
WSAEWOULDBLOCK
-
The socket is marked as nonblocking and SO_LINGER is set to a nonzero timeout
value.
See Also
accept(), socket(),
ioctlsocket(), setsockopt(),
WSAAsyncSelect().
connect()
Description
Establish a connection to a peer.
#include <winsock.h>
int PASCAL FAR connect ( SOCKET s, const struct
sockaddr FAR * name, int namelen);
-
s
-
A descriptor identifying an unconnected socket.
-
name
-
The name of the peer to which the socket is to be connected.
-
namelen
-
The length of the name.
Remarks
This function is used to create a connection to the specified foreign association.
The parameter s specifies an unconnected datagram or stream socket
If the socket is unbound, unique values are assigned to the local association
by the system, and the socket is marked as bound. Note that if the address
field of the name structure is all zeroes, connect() will
return the error WSAEADDRNOTAVAIL.
For stream sockets (type SOCK_STREAM), an active connection is initiated
to the foreign host using name (an address in the name space of
the socket). When the socket call completes successfully, the socket is
ready to send/receive data.
For a datagram socket (type SOCK_DGRAM), a default destination is set,
which will be used on subsequent send() and recv()
calls.
On a non-blocking socket, if the return value is SOCKET_ERROR
an application should call WSAGetLastError().
If this indicates an error code of WSAEWOULDBLOCK, then your application
can either:
-
Use select() to determine the completion of the connection request
by checking if the socket is writeable, or
-
If your application is using the message-based WSAAsyncSelect()
to indicate interest in connection events, then your application will receive
an FD_CONNECT message when the connect operation is complete.
Return Value
If no error occurs, connect() returns 0. Otherwise, it returns SOCKET_ERROR,
and a specific error code may be retrieved by calling WSAGetLastError().
On a blocking socket, the return value indicates success or failure
of the connection attempt.
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEADDRINUSE
-
The specified address is already in use.
-
WSAEINTR
-
The (blocking) call was canceled via WSACancelBlockingCall()
-
WSAEINPROGRESS
-
A blocking Windows Sockets call is in progress.
-
WSAEADDRNOTAVAIL
-
The specified address is not available from the local machine.
-
WSAEAFNOSUPPORT
-
Addresses in the specified family cannot be used with this socket.
-
WSAECONNREFUSED
-
The attempt to connect was forcefully rejected.
-
WSAEDESTADDREQ
-
A destination address is required.
-
WSAEFAULT
-
The namelen argument is incorrect.
-
WSAEINVAL
-
The socket is not already bound to an address.
-
WSAEISCONN
-
The socket is already connected.
-
WSAEMFILE
-
No more file descriptors are available.
-
WSAENETUNREACH
-
The network can't be reached from this host at this time.
-
WSAENOBUFS
-
No buffer space is available. The socket cannot be connected.
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAETIMEDOUT
-
Attempt to connect timed out without establishing a connection
-
WSAEWOULDBLOCK
-
The socket is marked as non-blocking and the connection cannot be completed
immediately. It is possible to select() the socket
while it is connecting by select()ing it for writing.
See Also
accept(), bind(), getsockname(),
socket(), and WSAAsyncSelect.
getpeername()
Description
Get the address of the peer to which a socket is connected.
#include <winsock.h>
int PASCAL FAR getpeername(SOCKET s, struct sockaddr
FAR * name, int FAR * namelen);
-
s
-
A descriptor identifying a connected socket.
-
name
-
The structure which is to receive the name of the peer.
-
namelen
-
A pointer to the size of the name structure.
Remarks
getpeername() retrieves the name of the peer connected to the socket
s and stores it in the struct sockaddr identified by name.
It is used on a connected datagram or stream socket. On return, the namelen
argument contains the actual size of the name returned in bytes.
Return Value
If no error occurs, getpeername() returns 0. Otherwise, a value
of SOCKET_ERROR is returned, and a specific error code may be retrieved
by calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEFAULT
-
The namelen argument is not large enough.
-
WSAEINPROGRESS
-
A blocking Windows Sockets call is in progress.
-
WSAENOTCONN
-
The socket is not connected.
-
WSAENOTSOCK
-
The descriptor is not a socket.
See Also
bind(), socket(), getsockname().
getsockname()
Description
Get the local name for a socket.
#include <winsock.h>
int PASCAL FAR getsockname(SOCKET s, struct sockaddr
FAR * name, int FAR * namelen);
-
s
-
A descriptor identifying a bound socket.
-
name
-
Receives the address (name) of the socket.
-
namelen
-
The size of the name buffer.
Remarks
getsockname() retrieves the current name for the specified socket
descriptor in name. It is used on a bound and/or connected socket
specified by the s parameter. The local association is returned.
This call is especially useful when a connect()
call has been made without doing a bind() first; this
call provides the only means by which you can determine the local association
which has been set by the system.
On return, the namelen argument contains the actual size
of the name returned in bytes.
If a socket was bound to INADDR_ANY, indicating that any of the host's
IP addresses should be used for the socket, getsockname() will not
necessarily return information about the host IP address, unless the socket
has been connected with connect() or accept().
A Windows Sockets application must not assume that the IP address will
be changed from INADDR_ANY unless the socket is connected. This is because
for a multi-homed host the IP address that will be used for the socket
is unknown unless the socket is connected.
Return Value
If no error occurs, getsockname() returns 0. Otherwise, a value
of SOCKET_ERROR is returned, and a specific error code may be retrieved
by calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEFAULT
-
The namelen argument is not large enough.
-
WSAEINPROGRESS
-
A blocking Windows Sockets operation is in progress.
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAEINVAL
-
The socket has not been bound to an address with bind().
See Also
bind(), socket(), getpeername().
getsockopt()
Description
Retrieve a socket option.
#include <winsock.h>
int PASCAL FAR getsockopt ( SOCKET s, int level,
int optname, char FAR * optval, int FAR * optlen);
-
s
-
A descriptor identifying a socket.
-
level
-
The level at which the option is defined; the only supported levels
are SOL_SOCKET and IPPROTO_TCP.
-
optname
-
The socket option for which the value is to be retrieved.
-
optval
-
A pointer to the buffer in which the value for the requested option is
to be returned.
-
optlen
-
A pointer to the size of the optval buffer.
Remarks
getsockopt() retrieves the current value for a socket option associated
with a socket of any type, in any state, and stores the result in optval.
Options may exist at multiple protocol levels, but they are always present
at the uppermost ``socket'' level. Options affect socket operations, such
as whether an operation blocks or not, the routing of packets, out-of-band
data transfer, etc. The value associated with the selected option is returned
in the buffer optval. The integer pointed to by optlen should
originally contain the size of this buffer; on return, it will be set to
the size of the value returned. For SO_LINGER, this will be the size of
a struct linger; for all other options it will be the size of an integer.
If the option was never set with setsockopt(),
then getsockopt() returns the default value for the option.
The following options are supported for getsockopt(). The
Type identifies the type of data addressed by optval. The
TCP_NODELAY option uses level IPPROTO_TCP, all other options use level
SOL_SOCKET.
Value Type Meaning
--------------- --------------- -----------------------------------------------
SO_ACCEPTCONN BOOL Socket is listen()ing.
SO_BROADCAST BOOL Socket is configured for the transmission of
broadcast messages.
SO_DEBUG BOOL Debugging is enabled.
SO_DONTLINGER BOOL If true, the SO_LINGER option is disabled..
SO_DONTROUTE BOOL Routing is disabled.
SO_ERROR int Retrieve error status and clear.
SO_KEEPALIVE BOOL Keepalives are being sent.
SO_LINGER struct linger Returns the current linger options.
FAR *
SO_OOBINLINE BOOL Out-of-band data is being received in the
normal data stream.
SO_RCVBUF int Buffer size for receives
SO_REUSEADDR BOOL The socket may be bound to an address which
is already in use.
SO_SNDBUF int Buffer size for sends
SO_TYPE int The type of the socket (e.g. SOCK_STREAM).
TCP_NODELAY BOOL Disables the Nagle algorithm for send
coalescing.
BSD options not supported for getsockopt() are:
Value Type Meaning
--------------- --------------- -----------------------------------------------
SO_RCVLOWAT int Receive low water mark
SO_RCVTIMEO int Receive timeout
SO_SNDLOWAT int Send low water mark
SO_SNDTIMEO int Send timeout
IP_OPTIONS Get options in IP header.
TCP_MAXSEG int Get TCP maximum segment size.
Calling getsockopt() with an unsupported option will result in an
error code of WSAENOPROTOOPT being returned from WSAGetLastError().
Return Value
If no error occurs, getsockopt() returns 0. Otherwise, a value of
SOCKET_ERROR is returned, and a specific error code may be retrieved by
calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEFAULT
-
The optlen argument was invalid.
-
WSAEINPROGRESS
-
A blocking Windows Sockets operation is in progress.
-
WSAENOPROTOOPT
-
The option is unknown or unsupported. In particular, SO_BROADCAST is not
supported on sockets of type SOCK_STREAM, while SO_ACCEPTCON, SO_DONTLINGER,
SO_KEEPALIVE, SO_LINGER and SO_OOBINLINE are not supported on sockets of
type SOCK_DGRAM.
-
WSAENOPROTOOPT
-
The option is unknown or unsupported. In particular, SO_BROADCAST is not
supported on sockets of type SOCK_STREAM, while SO_ACCEPTCONN, SO_DONTLINGER,
SO_KEEPALIVE, SO_LINGER and SO_OOBINLINE are not supported on sockets of
type SOCK_DGRAM.
-
WSAENOTSOCK
-
The descriptor is not a socket.
See Also
setsockopt(), WSAAsyncSelect(),
socket().
htonl()
Description
Convert a u_long from host to network byte order.
#include <winsock.h>
u_long PASCAL FAR htonl ( u_long hostlong);
-
hostlong
-
A 32-bit number in host byte order.
Remarks
This routine takes a 32-bit number in host byte order and returns a 32-bit
number in network byte order.
Return Value
htonl() returns the value in network byte order.
See Also
htons(), ntohl(), ntohs().
htons()
Description
Convert a u_short from host to network byte order.
#include <winsock.h>
u_short PASCAL FAR htons ( u_short hostshort );
-
hostshort
-
A 16-bit number in host byte order.
Remarks
This routine takes a 16-bit number in host byte order and returns a 16-bit
number in network byte order.
Return Value
htons() returns the value in network byte order.
See Also
htonl(), ntohl(), ntohs().
inet_addr()
Description
Convert a string containing a dotted address into an in_addr.
#include <winsock.h>
unsigned long PASCAL FAR inet_addr ( const char FAR * cp
);
-
cp
-
A character string representing a number expressed in the Internet standard
``.'' notation.
Remarks
This function interprets the character string specified by the cp
parameter. This string represents a numeric Internet address expressed
in the Internet standard ``.'' notation. The value returned is a number
suitable for use as an Internet address. All Internet addresses are returned
in network order (bytes ordered from left to right). Internet Addresses
Values specified using the ``.'' notation take one of the following
forms:
When four parts are specified, each is interpreted as a byte of data and
assigned, from left to right, to the four bytes of an Internet address.
Note that when an Internet address is viewed as a 32-bit integer quantity
on the Intel architecture, the bytes referred to above appear as ``d.c.b.a''.
That is, the bytes on an Intel processor are ordered from right to left.
Note: The following notations are only used by Berkeley, and nowhere
else on the Internet. In the interests of compatibility with their software,
they are supported as specified.
When a three part address is specified, the last part is interpreted
as a 16-bit quantity and placed in the right most two bytes of the network
address. This makes the three part address format convenient for specifying
Class B network addresses as ``128.net.host''.
When a two part address is specified, the last part is interpreted
as a 24-bit quantity and placed in the right most three bytes of the network
address. This makes the two part address format convenient for specifying
Class A network addresses as ``net.host''.
When only one part is given, the value is stored directly in the
network address without any byte rearrangement.
Return Value
If no error occurs, inet_addr() returns an unsigned long containing
a suitable binary representation of the Internet address given. If the
passed-in string does not contain a legitimate Internet address, for example
if a portion of an ``a.b.c.d'' address exceeds 255, inet_addr() returns
the value INADDR_NONE.
See Also
inet_ntoa().
inet_ntoa()
Description
Convert a network address into a string in dotted format.
#include <winsock.h>
char FAR * PASCAL FAR inet_ntoa ( struct in_addr in
);
-
in
-
A structure which represents an Internet host address.
Remarks
This function takes an Internet address structure specified by the in
parameter. It returns an ASCII string representing the address in ``.''
notation as ``a.b.c.d''. Note that the string returned by inet_ntoa()
resides in memory which is allocated by the Windows Sockets implementation.
The application should not make any assumptions about the way in which
the memory is allocated. The data is guaranteed to be valid until the next
Windows Sockets API call within the same thread, but no longer.
Return Value
If no error occurs, inet_ntoa() returns a char pointer to a static
buffer containing the text address in standard ``.'' notation. Otherwise,
it returns NULL. The data should be copied before another Windows Sockets
call is made.
See Also
inet_addr().
ioctlsocket()
Description
Control the mode of a socket.
#include <winsock.h>
int PASCAL FAR ioctlsocket ( SOCKET s, long cmd,
u_long FAR * argp);
-
s
-
A descriptor identifying a socket.
-
cmd
-
The command to perform on the socket s.
-
argp
-
A pointer to a parameter for cmd.
Remarks
This routine may be used on any socket in any state. It is used to get
or retrieve operating parameters associated with the socket, independent
of the protocol and communications subsystem. The following commands are
supported:
-
FIONBIO
-
Enable or disable non-blocking mode on the socket s. argp
points at an unsigned long, which is non-zero if non-blocking mode
is to be enabled and zero if it is to be disabled. When a socket is created,
it operates in blocking mode (i.e. non-blocking mode is disabled). This
is consistent with BSD sockets.
The WSAAsyncSelect() routine automatically
sets a socket to nonblocking mode. If WSAAsyncSelect() has been
issued on a socket, then any attempt to use ioctlsocket() to set
the socket back to blocking mode will fail with WSAEINVAL. To set the socket
back to blocking mode, an application must first disable WSAAsyncSelect()
by calling WSAAsyncSelect() with the lEvent parameter
equal to 0.
-
FIONREAD
-
Determine the amount of data which can be read atomically from socket s.
argp points at an unsigned long in which ioctlsocket()
stores the result. If s is of type SOCK_STREAM, FIONREAD returns
the total amount of data which may be read in a single recv(); this
is normally the same as the total amount of data queued on the socket.
If s is of type SOCK_DGRAM, FIONREAD returns the size of the first
datagram queued on the socket.
-
SIOCATMARK
-
Determine whether or not all out-of-band data has been read. This applies
only to a socket of type SOCK_STREAM which has been configured for in-line
reception of any out-of-band data (SO_OOBINLINE). If no out-of-band data
is waiting to be read, the operation returns TRUE. Otherwise it returns
FALSE, and the next recv() or recvfrom()
performed on the socket will retrieve some or all of the data preceding
the "mark"; the application should use the SIOCATMARK operation to determine
whether any remains. If there is any normal data preceding the "urgent"
(out of band) data, it will be received in order. (Note that a recv()
or recvfrom() will never mix out-of-band and normal
data in the same call.) argp points at a BOOL in which ioctlsocket()
stores the result.
Compatibility
This function is a subset of ioctl() as used in Berkeley sockets.
In particular, there is no command which is equivalent to FIOASYNC, while
SIOCATMARK is the only socket-level command which is supported.
Return Value
Upon successful completion, the ioctlsocket() returns 0. Otherwise,
a value of SOCKET_ERROR is returned, and a specific error code may be retrieved
by calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEINVAL
-
cmd is not a valid command, or argp is not an acceptable
parameter for cmd, or the command is not applicable to the type
of socket supplied
-
WSAEINPROGRESS
-
A blocking Windows Sockets operation is in progress.
-
WSAENOTSOCK
-
The descriptor s is not a socket.
See Also
socket(), setsockopt(),
getsockopt(), WSAAsyncSelect().
listen()
Description
Establish a socket to listen for incoming connection.
#include <winsock.h>
int PASCAL FAR listen(SOCKET s, int backlog
);
-
s
-
A descriptor identifying a bound, unconnected socket.
-
backlog
-
The maximum length to which the queue of pending connections may grow.
Remarks
To accept connections, a socket is first created with socket(),
a backlog for incoming connections is specified with listen(), and
then the connections are accepted with accept().
listen() applies only to sockets that support connections, i.e.
those of type SOCK_STREAM. The socket s is put into ``passive''
mode where incoming connections are acknowledged and queued pending acceptance
by the process.
This function is typically used by servers that could have more
than one connection request at a time: if a connection request arrives
with the queue full, the client will receive an error with an indication
of WSAECONNREFUSED. listen() attempts to continue to function rationally
when there are no available descriptors. It will accept connections until
the queue is emptied. If descriptors become available, a later call to
listen() or accept() will re-fill the queue
to the current or most recent ``backlog'', if possible, and resume listening
for incoming connections.
Compatibility
backlog is currently limited (silently) to 5. As in 4.3BSD, illegal
values (less than 1 or greater than 5) are replaced by the nearest legal
value.
Return Value
If no error occurs, listen() returns 0. Otherwise, a value of SOCKET_ERROR
is returned, and a specific error code may be retrieved by calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEADDRINUSE
-
An attempt has been made to listen() on an address in use.
-
WSAEINPROGRESS
-
A blocking Windows Sockets operation is in progress.
-
WSAEFAULT
-
An invalid argument was given.
-
WSAEINVAL
-
The socket has not been bound with bind() or is already
connected.
-
WSAEISCONN
-
The socket is already connected.
-
WSAEMFILE
-
No more file descriptors are available.
-
WSAENOBUFS
-
No buffer space is available.
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAEOPNOTSUPP
-
The referenced socket is not of a type that supports the listen()
operation.
See Also
accept(), connect(),
socket().
ntohl()
Description
Convert a u_long from network to host byte order.
#include <winsock.h>
u_long PASCAL FAR ntohl ( u_long netlong);
-
netlong
-
A 32-bit number in network byte order.
Remarks
This routine takes a 32-bit number in network byte order and returns a
32-bit number in host byte order.
Return Value
ntohl() returns the value in host byte order.
See Also
htonl(), htons(), ntohs().
ntohs()
Description
Convert a u_short from network to host byte order.
#include <winsock.h>
u_short PASCAL FAR ntohs ( u_short netshort );
-
netshort
-
A 16-bit number in network byte order.
Remarks
This routine takes a 16-bit number in network byte order and returns a
16-bit number in host byte order.
Return Value
ntohs() returns the value in host byte order.
See Also
htonl(), htons(), ntohl().
recv()
Description
Receive data from a socket.
#include <winsock.h>
int PASCAL FAR recv ( int s, char FAR * buf,
int len, int flags);
-
s
-
A descriptor identifying a connected socket.
-
buf
-
A buffer for the incoming data.
-
len
-
The length of buf.
-
flags
-
Specifies the way in which the call is made.
Remarks
This function is used on connected datagram or stream sockets specified
by the s parameter and is used to read incoming data.
For sockets of type SOCK_STREAM, as much information as is currently
available up to the size of the buffer supplied is returned. If the socket
has been configured for in-line reception of out-of-band data (socket option
SO_OOBINLINE) and out-of-band data is unread, only out-of-band data will
be returned. The application may use the ioctlsocket()
SIOCATMARK to determine whether any more out-of-band data remains to be
read.
For datagram sockets, data is extracted from the first enqueued
datagram, up to the size of the size of the buffer supplied. If the datagram
is larger than the buffer supplied, the excess data is lost, and recv()
returns the error WSAEMSGSIZE. If no incoming data is available at the
socket, the recv() call waits for data to arrive unless the socket
is non-blocking. In this case a value of SOCKET_ERROR is returned with
the error code set to WSAEWOULDBLOCK. The select()
or WSAAsyncSelect() calls may be used to determine
when more data arrives. If the socket is of type SOCK_STREAM and the remote
side has shut down the connection gracefully, a recv() will complete
immediately with 0 bytes received. If the connection has been abortively
disconnected, a recv() will fail with the error WSAECONNRESET.
Flags may be used to influence the behavior of the function invocation
beyond the options specified for the associated socket. That is, the semantics
of this function are determined by the socket options and the flags
parameter. The latter is constructed by or-ing any of the following values:
-
MSG_PEEK
-
Peek at the incoming data. The data is copied into the buffer but is not
removed from the input queue.
-
MSG_OOB
-
Process out-of-band data (See Out of Band
Data for a discussion of this topic.)
Return Value
If no error occurs, recv() returns the number of bytes received.
If the connection has been closed, it returns 0. Otherwise, a value of
SOCKET_ERROR is returned, and a specific error code may be retrieved by
calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAENOTCONN
-
The socket is not connected.
-
WSAEINTR
-
The (blocking) call was canceled via WSACancelBlockingCall()
-
WSAEINPROGRESS
-
A blocking Windows Sockets operation is in progress.
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAEOPNOTSUPP
-
MSG_OOB was specified, but the socket is not of type SOCK_STREAM.
-
WSAESHUTDOWN
-
The socket has been shutdown; it is not possible to recv() on a
socket after shutdown() has been invoked with how
set to 0 or 2.
-
WSAEWOULDBLOCK
-
The socket is marked as non-blocking and the receive operation would block.
-
WSAEMSGSIZE
-
The datagram was too large to fit into the specified buffer and was truncated.
-
WSAEINVAL
-
The socket has not been bound with bind().
-
WSAECONNABORTED
-
The virtual circuit was aborted due to timeout or other failure.
-
WSAECONNRESET
-
The virtual circuit was reset by the remote side.
See Also
recvfrom(), send(),
select(), WSAAsyncSelect(),
socket().
recvfrom()
Description
Receive a datagram and store the source address.
#include <winsock.h>
int PASCAL FAR recvfrom ( int s, char FAR * buf,
int len, int flags, struct sockaddr FAR * from,
int FAR * fromlen );
-
s
-
A descriptor identifying a bound socket.
-
buf
-
A buffer for the incoming data.
-
len
-
The length of buf.
-
flags
-
Specifies the way in which the call is made.
-
from
-
An optional pointer to a buffer which will hold the source address upon
return.
-
fromlen
-
An optional pointer to the size of the from buffer.
Remarks
This function is used to read incoming data on a (possibly connected) socket
and capture the address from which the data was sent.
For sockets of type SOCK_STREAM, as much information as is currently
available up to the size of the buffer supplied is returned. If the socket
has been configured for in-line reception of out-of-band data (socket option
SO_OOBINLINE) and out-of-band data is unread, only out-of-band data will
be returned. The application may use the ioctlsocket()
SIOCATMARK to determine whether any more out-of-band data remains to be
read. The from and fromlen parameters are ignored for SOCK_STREAM
sockets.
For datagram sockets, data is extracted from the first enqueued
datagram, up to the size of the size of the buffer supplied. If the datagram
is larger than the buffer supplied, the buffer is filled with the first
part of the message, the excess data is lost, and recvfrom() returns
the error code WSAEMSGSIZE. If from is non-zero, and the socket
is of type SOCK_DGRAM, the network address of the peer which sent the data
is copied to the corresponding struct sockaddr. The value pointed to by
fromlen is initialized to the size of this structure, and is modified
on return to indicate the actual size of the address stored there.
If no incoming data is available at the socket, the recvfrom()
call waits for data to arrive unless the socket is non-blocking. In this
case a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK.
The select() or WSAAsyncSelect()
calls may be used to determine when more data arrives. If the socket is
of type SOCK_STREAM and the remote side has shut down the connection gracefully,
a recvfrom() will complete immediately with 0 bytes received. If
the connection has been abortively disconnected, a recvfrom() will
fail with the error WSAECONNRESET.
Flags may be used to influence the behavior of the function invocation
beyond the options specified for the associated socket. That is, the semantics
of this function are determined by the socket options and the flags
parameter. The latter is constructed by or-ing any of the following values:
-
MSG_PEEK
-
Peek at the incoming data. The data is copied into the buffer but is not
removed from the input queue.
-
MSG_OOB
-
Process out-of-band data (See Out of Band
Data for a discussion of this topic.)
Return Value
If no error occurs, recvfrom() returns the number of bytes received.
If the connection has been closed, it returns 0. Otherwise, a value of
SOCKET_ERROR is returned, and a specific error code may be retrieved by
calling WSAGetLastError().
Error Codes
-
WSANOTINITIALISED
-
A successful WSAStartup() must occur before using
this API.
-
WSAENETDOWN
-
The Windows Sockets implementation has detected that the network subsystem
has failed.
-
WSAEFAULT
-
The fromlen argument was invalid: the from buffer was too
small to accommodate the peer address.
-
WSAEINTR
-
The (blocking) call was canceled via WSACancelBlockingCall()
-
WSAEINPROGRESS
-
A blocking Windows Sockets operation is in progress.
-
WSAEINVAL
-
The socket has not been bound with bind().
-
WSAENOTCONN
-
The socket is not connected (SOCK_STREAM only).
-
WSAENOTSOCK
-
The descriptor is not a socket.
-
WSAEOPNOTSUPP
-
MSG_OOB was specified, but the socket is not of type SOCK_STREAM.
-
WSAESHUTDOWN
-
The socket has been shutdown; it is not possible to recvfrom() on
a socket after shutdown() has been invoked with
how set to 0 or 2.
-
WSAEWOULDBLOCK
-
The socket is marked as non-blocking and the recvfrom() operation
would block.
-
WSAEMSGSIZE
-
The datagram was too large to fit into the specified buffer and was truncated.
-
WSAECONNABORTED
-
The virtual circuit was aborted due to timeout or other failure.
-
WSCONNRESET
-
The virtual circuit was reset by the remote side.
See Also
recv(), send(), socket(),
WSAAsyncSelect().
select()
Description
Determine the status of one or more sockets, waiting if necessary.
#include <winsock.h>
long PASCAL FAR select ( int nfds, fd_set FAR
* readfds, fd_set FAR * writefds, fd_set FAR
* exceptfds, const struct timeval FAR * timeout );
-
nfds
-
This argument is ig