ex_tcp.c

This example program shows how to receive TCP data. This program acts as the server, and listens for any data entering on TCP port 54123. If a TCP message is received, we set the system LED if the first byte of the received message is '1', and clear the system LED if it is '0'

#include "net\tcp.h"

//Create a TCP socket for receiving and sending data
static TCP_SOCKET tcpSocketUser = INVALID_SOCKET;

void tcpExample(void)
{
    BYTE c;
    NODE_INFO tcpServerNode;

    //Create a TCP socket that listens on port 54123
    tcpSocketUser = TCPListen(54123);
    
    //An error occurred during the TCPOpen() function
    if (tcpSocketUser == INVALID_SOCKET) {
        //Add user code here to take action if required!
    }

    //Infinite loop. Check if anything is received on TCP port
    while(1)
    {
        //Has a remote node made connection with the port we are listening on
        if (TCPIsConnected(tcpSocketUser)) {
            //Is there any data waiting for us on the TCP socket?
            //Because of the design of the Modtronix TCP/IP stack we have to
            //consume all data sent to us as soon as we detect it.
            if (TCPIsGetReady(tcpSocketUser)) {

                //We are only interrested in the first byte of the message.
                TCPGet(tcpSocketUser, &c);
            
                if (c == '0') LATB6 = 0;        //Switch system LED off
                else if (c == '1') LATB6 = 1;  //Switch system LED on

                //Discard the socket buffer.
                TCPDiscard(tcpSocketUser);
            }
        }

        //This task performs normal stack task including checking for incoming
        //packet, type of packet and calling appropriate stack entity to
        //process it.
        StackTask();
    }
}

Generated on Wed Feb 3 12:45:33 2010 for SBC65EC Web Server by  doxygen 1.5.8