1. Create a local socket
The call to create a socket looks like this:
int Socket = socket(AF_INET, Type, Protocol);
Where Type is SOCK_STREAM and
Protocol is IPPROTO_TCP for a
blocking, connection oriented TCP/IP socket.
2. 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 connect, and is used to associate the
currently unnamed socket with a local name.
The SockAddr parameter is the struct
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 socket and
bind are both done in the function
MySocketOpen.
3. Establish a connection
Connections are established by calling the
connect function.
connect(Socket, &SockAddr, sizeof(SockAddr));
Where SockAddr is the struct sockaddr
representation of the peer name to connect to. Because blocking
sockets are being used, the call to connect 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 struct sockaddr
representation.
4. Transmit data
Transmitting data is done by calling
send.
send(Socket, Data, Length, 0);
After verifying the connection completed successfully, the
sample uses the socket to transmit some data.
For consistency the sample provides its own function named
MySocketSend.
MySocketSend(MySocket, SendData, sizeof(SendData));
The 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
sizeof(SendData) which is the number of bytes
SendData points to.
5. Receive data
To retrieve data received at the local address, call
recv.
recv(Socket, Data, Length, 0);
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.
Again for consistency, the sample provides its own function,
MySocketRecv
MySocketRecv(MySocket, RecvData, sizeof(RecvData));
The caller to MySocketRecv must supply the
prerequisite MySocket, a buffer
RecvData to hold the received data, and
sizeof(RecvData) which specifies the length of
RecvData.
6. Close the socket
Accomplish this task by calling the following two functions:
shutdown(Socket, SHUT_RDWR);
close(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 (close).