1. Initialize Windows Sockets
The following initialization code is found early in the
program's main function.
WSABUF WSABuf = {0};
WSAStartup(WINSOCK_VERSION, &WSAData);
Initializing Windows Sockets is critical. Failing to initialize
Windows Sockets will cause all other socket function calls to
fail.
This sample utilizes Windows Sockets version 2.2. Therefore, the
macro WINSOCK_VERSION is defined in the system header
file WinSock2.h. An application that calls
WSAStartup must call WSACleanup when it
is done using the Windows Socket services (see step 8).
2. Create a local socket
The call to create a socket looks like this:
SOCKET Socket = WSASocket(AF_INET, Type, Protocol,
NULL, 0, 0);
Where Type is SOCK_STREAM and
Protocol is IPPROTO_TCP for a
blocking, connection oriented TCP/IP socket.
3. Bind to a local port
To bind the socket to a local port, call the following
function:
bind(Socket, &SockAddr, sizeof(SockAddr));
Binding to a local port must be done before using the socket to
calls like WSAConnect, and is used to associate the
currently unnamed socket with a local name.
The SockAddr parameter is the SOCKADDR
representation of the local name to bind to. This name consists of
an address family (for TCP/IP, always AF_INET), a host
address, and a port number.
In this sample, the calls to WSASocket and
bind are both done in the function
MySocketOpen.
4. Establish a connection
Connections are established by calling the
WSAConnect function.
WSAConnect(Socket, &SockAddr, sizeof(SockAddr),
NULL, NULL, NULL, NULL);
Where SockAddr is the SOCKADDR
representation of the peer name to connect to. Because blocking
sockets are being used, the call to WSAConnect will
not return until the operation either fails or completes
successfully.
To simplify creating a connection, this sample uses a function
named MySocketConnect.
MySocketConnect(MySocket,
AddressToConnectTo,
PortToConnectTo);
Where MySocket is the socket returned from
the call to MySocketOpen.
AddressToConnectTo is a string that
represents the IP address of the device server in dotted number
format (e.g. "10.0.0.1").
PortToConnectTo is the port number used by
the device server for raw TCP connections to port number one. On
Digi device servers this port number defaults to
2101.
MySocketConnect hides the details of converting the
IP address and port number to SOCKADDR
representation.
5. Transmit data
Transmitting data is done by calling
WSASend.
WSASend(Socket, &WSABuf, 1, Length, 0, NULL, NULL);
After verifying the connection completed successfully, the
sample uses the socket to transmit some data.
Again, to simplify transmitting data, and to hide many of the
details associated with the parameters necessary to call
WSASend, this sample uses its own function named
MySocketSend.
MySocketSend(MySocket, SendData, &SendLength);
The only parameters needed for MySocketSend are:
MySocket which is the socket returned from
MySocketOpen, SendData which is a
pointer to the buffer holding the data to send, and
SendLength which is the number of bytes
SendData points to.
6. Receive data
To retrieve data received at the local address, call
WSARecv.
WSARecv(Socket, &WSABuf, 1, Length, &Flags,
NULL, NULL);
With the device server setup with the loopback plug, the data
transmitted in the step above will be sent directly back to the
sample application.
Like WSASend, WSARecv has a large
number of parameters, providing great flexibility at the price of
adding to its complexity. Also like WSASend, the
sample provides an alternative function,
MySocketRecv that is somewhat easier to
use.
MySocketRecv(MySocket, RecvData, &BytesReceived);
The caller to MySocketRecv need only supply the
prerequisite MySocket, a buffer
RecvData to hold the received data, and
BytesReceived which specifies the length of
RecvData and on return exactly how many bytes were
actually copied into the buffer.
7. Close the socket
Accomplish this task by calling the following two functions:
shutdown(Socket, SD_BOTH);
closesocket(Socket);
When the sample application is done using the socket, the
connection it represents must be terminated properly
(shutdown) and any resources it may be using should be
released (closesocket).
8. Cleanup
Before exiting the program, call:
WSACleanup();
For every successful call to WSAStartup an
application completes, the application must make one call to
WSACleanup.