Archive

Posts Tagged ‘Socket Programming’

Simple Sockets

December 10th, 2009 Jimmy No comments

Simple Sockets is a cross platform socket library. It is different from other socket libraries which mix the application protocol and transport protocol layer (i.e. TCP sockets, UDP sockets, HTTP sockets, etc). This library focuses specifically on the transport protocols. Its features include blocking/non-blocking sockets, polling, thread safety, and signal safety.

This example creates a socket to a time server and receives the time.

#include <string.h>

#include "ActiveSocket.h"       // Include header for active socket object definition

 

int main(int argc, char **argv)

{

    CActiveSocket socket;       // Instantiate active socket object (defaults to TCP).

    char          time[50];

 

    memset(&time, 0, 50);

 

    //--------------------------------------------------------------------------

    // Initialize our socket object 

    //--------------------------------------------------------------------------

    socket.Initialize();

 

    //--------------------------------------------------------------------------

    // Create a connection to the time server so that data can be sent

    // and received.

    //--------------------------------------------------------------------------

    if (socket.Open((const uint8 *)"time-C.timefreq.bldrdoc.gov", 13))

    {

        //----------------------------------------------------------------------

        // Send a requtest the server requesting the current time.

        //----------------------------------------------------------------------

        if (socket.Send((const uint8 *)"\n", 1))

        {

            //------------------------------------------------------------------

            // Receive response from the server.

            //------------------------------------------------------------------

            socket.Receive(49);

            memcpy(&time, socket.GetData(), 49);

            printf("%s\n", time);

 

            //------------------------------------------------------------------

            // Close the connection.

            //------------------------------------------------------------------

            socket.Close();

        }

    }

 

 

    return 1;

}