Digi Homepage Making Device Networking Easy.
 

BSD TCP Server Sample

Introduction

This sample demonstrates how to write a TCP server application using the Berkeley Software Distribution (BSD) standard socket APIs to communicate with a device server.

For the purpose of this demonstration, the server application creates a networking socket it uses to listen for incoming networking connections. A TCP connection is initiated by the device server to the sample application through the use of its AutoConnect feature and a serial loopback plug. Once the connection is established, the sample application attempts to transmit and receive data from the device server.

Device Server Setup

This sample was designed to operate with the first port on the device server, and requires that the port be configured to automatically initiate an TCP connection when carrier is detected (DCD goes high).

Configure the device server
  1. Access the module’s web interface by entering its IP address in a browser’s URL window.
  2. Choose Serial Ports from the Configuration menu.
  3. Configure the module to function as a TCP client by doing the following:
    1. Click the TCP tab.
    2. Check Automatically establish TCP connections.
    3. Specify DCD in the Connect field.
    4. Enter the IP address of the PC running the sample in the Connect To field.
    5. Specify Raw as the Service Type.
    6. Specify 7777 as the TCP port number.
    7. Click Save.

How To Build

This sample has been compiled and tested using GCC 3.2 on Red Hat Linux 8.0. It contains a Makefile that is used for building the application.

To build this sample
  1. Change directory to where the sample source files reside.
  2. Type make.

Step-by-Step

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).

File List

hexdumpdata.cpp
hexdumpdata.h
Makefile
mysocket.cpp
mysocket.h
tcpserver.cpp

Keywords

socket; bind; listen; accept; send; recv; shutdown; close
 
 
Digi International Inc. 11001 Bren Road E. Minnetonka, MN 55343
PH: (952) 912-3444 or 877-912-3444
FX: (952) 912-4952