Digi Homepage Making Device Networking Easy.
 

MFC TCP Server Sample

Introduction

This sample demonstrates how to write a TCP server application using the MFC socket architecture 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 written and tested with Microsoft Visual C++® 6.0. It contains a Developer Studio project file (.dsp), that can be opened in the development environment for editing and compiling.

To build this sample from within Microsoft Visual C++® 6.0
  1. Select File > Open Workspace... from the main menu.
  2. Change Files of type: to Projects (.dsp).
  3. Locate and open the .dsp file for this sample.
  4. Select Build > Rebuild All menu items to compile the sample.

Step-by-Step

1.  Initialize Windows Sockets

The following initialization code should be in the main CWinApp derived object's overridable InitInstance function.

if (!AfxSocketInit())
{
   AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
   return FALSE;
}

Initializing Windows Sockets is critical. Failing to initialize Windows Sockets will cause all other socket function calls to fail.

2.  Create a local socket

The call to create a socket looks like this:

m_ListenSocket.Create(7777);

In this sample, the listening socket is represented by the CAsyncSocket derived class CMySocket. An object of CMySocket type is declared as a private member variable of the main dialog class and named m_ListenSocket.

m_ListenSocket is unusable until its Create member function is called, which is done in the main dialog's OnInitDialog overridable function. Since this sample demonstrates a server application, a well-known port (7777) is supplied to the call to Create. The same well-known port is also used by the device server as part of its port AutoConnect TCP/IP address information. The remaining default parameters of the Create function are sufficient for the application's purposes.

3.  Listen for incoming connections

Start listening for incoming connection requests by calling the Listen member function.

m_ListenSocket.Listen(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 single parameter to the call to Listen.

The socket object is notified of incoming connection requests via its OnAccept overridable notification function.

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

5.  Accept incoming connection

When notified of an incoming connection request, call Accept.

m_ListenSocket.Accept(m_ConnectedSocket);

By calling Accept, you accept the first incoming connection request in the backlog queue and establish a TCP session to the client, The new connection is represented by the socket object passed in as the first argument (m_ConnectedSocket). The listening socket remains in the passive listening mode, and the connected socket object is used to communicate with the client.

6.  Transmit data

Transmitting data is done by calling the connected socket's Send member functions.

m_ConnectedSocket.Send(Data, Length);

Once the incoming connection request has been accepted, and the new connected socket object is valid, the sample uses it to transmit some data.

The sample is not concerned about sending out-of-band data or routing of the TCP packet, so the only parameters needed for the Send function are: Data which is a pointer to a buffer containing the data to transmit, and Length which is the number of bytes of data in the buffer.

7.  Receive data

To retrieve data, the socket's Receive member function is called. In the function call the sample provides a buffer (Data) to store the incoming data, and the length (sizeof(Data)) of that buffer in bytes.

m_ConnectedSocket.Receive(Data, sizeof(Data));

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. The socket is notified that data has arrived for it when it receives the OnReceive notification.

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:

m_ConnectedSocket.ShutDown(CAsyncSocket::both);
m_ConnectedSocket.Close();

The sample application's connected socket will be notified that the connection has been closed by the client when it receives the OnClose notification. The socket should then be terminated properly (ShutDown) and any resources it may be using should be released (Close).

File List

HexDumpData.cpp
HexDumpData.h
MySocket.cpp
MySocket.h
resource.h
StdAfx.cpp
StdAfx.h
TCPServer.cpp
TCPServer.dsp
TCPServer.h
TCPServer.rc
TCPServerDlg.cpp
TCPServerDlg.h
res\TCPServer.ico
res\TCPServer.rc2

Keywords

CAsyncSocket::Create; CAsyncSocket::Connect; CAsyncSocket::OnConnect; CAsyncSocket::Send; CAsyncSocket::Receive; CAsyncSocket::OnReceive
 
 
Digi International Inc. 11001 Bren Road E. Minnetonka, MN 55343
PH: (952) 912-3444 or 877-912-3444
FX: (952) 912-4952