> | # MFC TCP Client Sample ## Introduction This sample demonstrates how to write a TCP client application using the MFC socket architecture to communicate with an SG15. For the purpose of this demonstration, the client application initiates communication with the SG15 by first creating a networking socket. Then using that socket, establishes a TCP connection to an SG15 and attempts to transmit and receive data from the SG15. ## SG15 Setup This sample was designed to operate with the SG15 operating in TCP Server mode. Configure the SG15 1. Use the SG15 configuration tool to access the settings of this specific SG15. 2. Choose the "Network" tab from the Configuration menu. 3. Check "TCP" and "Server" in the Connect Mode section. ## 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. Locate and open the .dsw file for this sample. 3. 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_MySocket.Create();
> In this sample, a 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_MySocket`. `m_MySocket` is unusable until its `Create` member function is called, which is done in the main dialog's `OnInitDialog` overridable function. Since this sample demonstrates using a TCP socket connection (socket type `SOCK_STREAM`) and is a client application which does not require specifying a particular well-known port, the default parameters of the `Create` function are used. 3. Establish a connection Connections are established by calling the socket object's `Connect` member function.
>     m_MySocket.Connect(AddressToConnectTo,
>                        PortToConnectTo);
> Where `AddressToConnectTo` is a string that represents the IP address of the SG15 in dotted number format (e.g. "10.0.0.1"). And `PortToConnectTo` is the port number used by the SG15 for raw TCP. On the SG15, this port number defaults to 9101. Because non-blocking sockets are being used, the call to `Connect` should return immediately, and the socket will receive an `OnConnect` notification when the operation is complete. 4. Transmit data Transmitting data is done by calling the socket's `Send` member functions.
>     m_MySocket.Send(Data, Length);
> After receiving the `OnConnect` notification and verifying the connection completed successfully, the sample uses the socket 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. 5. 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. Just scan a barcode on de SG15, which will be directly sent to this application.
>     m_MySocket.Receive(Data, sizeof(Data));
> The socket is notified that data has arrived for it when it receives the `OnReceive` notification. 6. Close the socket Accomplish this task by calling the following two functions:
>     m_MySocket.ShutDown(CAsyncSocket::both);
>     m_MySocket.Close();
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 MySocket.cpp MySocket.h resource.h StdAfx.cpp StdAfx.h TCPClient.cpp TCPClient.dsp TCPClient.h TCPClient.rc TCPClientDlg.cpp TCPClientDlg.h res\TCPClient.ico res\TCPClient.rc2 ## Keywords CAsyncSocket::Create; CAsyncSocket::Connect; CAsyncSocket::OnConnect; CAsyncSocket::Send; CAsyncSocket::Receive; CAsyncSocket::OnReceive