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
. Since this sample demonstrates a
server application, a well-known port (7777
) is
supplied to the call to MySocketOpen
. The same
well-known port is also used by the device server as part of its
port AutoConnect TCP/IP address information.
3. Listen for incoming connections
Start listening for incoming connection requests by calling the
listen
function.
listen(ListenSocket, 1);
Calling listen
puts the socket into a relatively
inactive mode where it waits for incoming connection requests from
a client. Connection requests are queued until processed in a
"backlog". The size of this backlog is specified in the second
parameter to the call to listen
.
4. Wait to accept an incoming connection
Call accept
to wait for an incoming
connection request.
int ConnectedSocket = accept(ListenSocket,
&AcceptSockAddr,
&AcceptSockAddrLength);
The sample is using blocking sockets, so the call to
accept
will not return until there is at least one
connection request pending in the backlog queue. The socket value
returned from accept
represents the new connection
that was just established. The listening socket remains in the
passive listening mode, and the new connected socket should be used
to communicate with the client.
5. Initiate an AutoConnect
Attach the serial loopback plug to port 1 on the
device server.
Attaching and detaching the serial loopback plug causes the DCD
signal to be raised or lowered (respectively). Since the port was
configured to AutoConnect when carrier is detected (DCD high),
attaching the loopback plug initiates an AutoConnect from the
device server to the sample application.
6. Transmit data
Transmitting data is done by calling
send
.
send(Socket, Data, Length, 0);
After accepting the incoming connection, 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.
7. 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
.
8. Close the connection
Detach the serial loopback plug to port 1 on the
device server.
This causes DCD to be lowered and the device server to properly
close the TCP session it has to the sample application.
9. Close the connected 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
).