ex_tcp_sendfile.c

This example is contained in the "./tcp/src/main_tcp4.c" file in the "Example TCP/IP projects" source code that can be downloaded from the relevant Ethernet SBC board's product page at www.modtronix.com. For example, for the SBC65EC it can be found at <a href="https://www.modtronix.com/product/sbc65ec

This example program shows how to send a file via TCP. The file can be any length. Files longer then the TCP transmit buffer are automatically sent in multiple TCP packets. The file is sent to socket 10.1.0.201:54124 (IP address 10.1.0.201, port 54124). It shows how to use the sendFileToTcpServer() funtion in a blocking and non blocking way.

The following modules are included in this project:

#include "net\stacktsk.h"
#include "net\tcputils.h"


//TCP Send File State machine
static enum _smSendFile {
    SM_SENDFILE_INIT = 0,
    SM_SENDFILE,
    SM_SENDFILE_DONE
} smSendFile = SM_SENDFILE_INIT;


void main(void)
{
    BYTE filename[12];
    BYTE sendFileRet;
    TCP_FILESEND_INFO tcpFileSendInfo;

    //Initialization code
    ..


    /************************************************/
    /**************** Method 1 begin ****************/
    //This mmethod shows how to send a file with the sendFileToTcpServer()
    //function that blocks until the file has been sent. The while()
    //statement only breaks once the entire file has been
    
    //Initialize TCP_FILESEND_INFO structure
    tcpFileSendInfo.serverNode.IPAddr.v[0]  = 10;       //Server's IP address
    tcpFileSendInfo.serverNode.IPAddr.v[1]  = 1;
    tcpFileSendInfo.serverNode.IPAddr.v[2]  = 0;
    tcpFileSendInfo.serverNode.IPAddr.v[3]  = 201;
    tcpFileSendInfo.port                    = 54124;    //Port to send file to
    tcpFileSendInfo.sm                      = 0;        //Start state
    
    //Initialize filename array with name of file to open, including terminating NULL
    strcpypgm2ram((char*)filename, (ROM char*)"IOVAL.CGI");
    
    //Repetitively call sendFileToTcpServer() until done
    while( (sendFileRet=sendFileToTcpServer(filename, &tcpFileSendInfo)) != 0)
        StackTask();
    if (sendFileRet != 0) {
        //Error during file send, take action!
    }
    /************************************************/
    /***************** Method 1 end *****************/

    //Initialize state machine to 0 to cause file to be sent
    smSendFile = 0;

    //Infinite loop. Check if anything is received on TCP port
    while(1)
    {
        /************************************************/
        /**************** Method 2 begin ****************/
        //This method shows how to send a file with the sendFileToTcpServer()
        //function that does NOT block until the file has been sent. It will
        //return 0xff if the operation is not done yet. This function has to
        //be repetitively called as long as it returns 0xff.
        switch (smSendFile) {
            //Initialize tcpFileSendInfo structure
            case SM_SENDFILE_INIT:
                //Initialize TCP_FILESEND_INFO structure
                tcpFileSendInfo.serverNode.IPAddr.v[0]  = 10;       //Server's IP address
                tcpFileSendInfo.serverNode.IPAddr.v[1]  = 1;
                tcpFileSendInfo.serverNode.IPAddr.v[2]  = 0;
                tcpFileSendInfo.serverNode.IPAddr.v[3]  = 201;
                tcpFileSendInfo.port                    = 54124;    //Port to send file to
                tcpFileSendInfo.sm                      = 0;        //Start state
                
                //Initialize filename array with name of file to open
                strcpypgm2ram((char*)filename, (ROM char*)"AVAL.CGI");

                smSendFile++;
                //break;    //No break
            //Send file. We remain in this state as long as sendFileToTcpServer() returns 0xff
            case SM_SENDFILE:
                if ( (sendFileRet=sendFileToTcpServer(filename, &tcpFileSendInfo)) == 0) {

                    //File successfully sent!
                    //Take action if required
                    
                    smSendFile++;       //File send done, go to done state
                }
                //An error occured during file send, take action if required!
                else if (sendFileRet != 0xff) {
                        
                    //Error while sending file, see sendFileRet for details on type of error
                    
                    smSendFile++;   //File send error, go to done state
                }
                break;
            //File send finished
            case SM_SENDFILE_DONE:
                break;
        }
        /************************************************/
        /***************** Method 2 end *****************/

        //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