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:
00001 00023 #include "net\stacktsk.h" 00024 #include "net\tcputils.h" 00025 00026 00027 //TCP Send File State machine 00028 static enum _smSendFile { 00029 SM_SENDFILE_INIT = 0, 00030 SM_SENDFILE, 00031 SM_SENDFILE_DONE 00032 } smSendFile = SM_SENDFILE_INIT; 00033 00034 00035 void main(void) 00036 { 00037 BYTE filename[12]; 00038 BYTE sendFileRet; 00039 TCP_FILESEND_INFO tcpFileSendInfo; 00040 00041 //Initialization code 00042 .. 00043 00044 00045 /************************************************/ 00046 /**************** Method 1 begin ****************/ 00047 //This mmethod shows how to send a file with the sendFileToTcpServer() 00048 //function that blocks until the file has been sent. The while() 00049 //statement only breaks once the entire file has been 00050 00051 //Initialize TCP_FILESEND_INFO structure 00052 tcpFileSendInfo.serverNode.IPAddr.v[0] = 10; //Server's IP address 00053 tcpFileSendInfo.serverNode.IPAddr.v[1] = 1; 00054 tcpFileSendInfo.serverNode.IPAddr.v[2] = 0; 00055 tcpFileSendInfo.serverNode.IPAddr.v[3] = 201; 00056 tcpFileSendInfo.port = 54124; //Port to send file to 00057 tcpFileSendInfo.sm = 0; //Start state 00058 00059 //Initialize filename array with name of file to open, including terminating NULL 00060 strcpypgm2ram((char*)filename, (ROM char*)"IOVAL.CGI"); 00061 00062 //Repetitively call sendFileToTcpServer() until done 00063 while( (sendFileRet=sendFileToTcpServer(filename, &tcpFileSendInfo)) != 0) 00064 StackTask(); 00065 if (sendFileRet != 0) { 00066 //Error during file send, take action! 00067 } 00068 /************************************************/ 00069 /***************** Method 1 end *****************/ 00070 00071 //Initialize state machine to 0 to cause file to be sent 00072 smSendFile = 0; 00073 00074 //Infinite loop. Check if anything is received on TCP port 00075 while(1) 00076 { 00077 /************************************************/ 00078 /**************** Method 2 begin ****************/ 00079 //This method shows how to send a file with the sendFileToTcpServer() 00080 //function that does NOT block until the file has been sent. It will 00081 //return 0xff if the operation is not done yet. This function has to 00082 //be repetitively called as long as it returns 0xff. 00083 switch (smSendFile) { 00084 //Initialize tcpFileSendInfo structure 00085 case SM_SENDFILE_INIT: 00086 //Initialize TCP_FILESEND_INFO structure 00087 tcpFileSendInfo.serverNode.IPAddr.v[0] = 10; //Server's IP address 00088 tcpFileSendInfo.serverNode.IPAddr.v[1] = 1; 00089 tcpFileSendInfo.serverNode.IPAddr.v[2] = 0; 00090 tcpFileSendInfo.serverNode.IPAddr.v[3] = 201; 00091 tcpFileSendInfo.port = 54124; //Port to send file to 00092 tcpFileSendInfo.sm = 0; //Start state 00093 00094 //Initialize filename array with name of file to open 00095 strcpypgm2ram((char*)filename, (ROM char*)"AVAL.CGI"); 00096 00097 smSendFile++; 00098 //break; //No break 00099 //Send file. We remain in this state as long as sendFileToTcpServer() returns 0xff 00100 case SM_SENDFILE: 00101 if ( (sendFileRet=sendFileToTcpServer(filename, &tcpFileSendInfo)) == 0) { 00102 00103 //File successfully sent! 00104 //Take action if required 00105 00106 smSendFile++; //File send done, go to done state 00107 } 00108 //An error occured during file send, take action if required! 00109 else if (sendFileRet != 0xff) { 00110 00111 //Error while sending file, see sendFileRet for details on type of error 00112 00113 smSendFile++; //File send error, go to done state 00114 } 00115 break; 00116 //File send finished 00117 case SM_SENDFILE_DONE: 00118 break; 00119 } 00120 /************************************************/ 00121 /***************** Method 2 end *****************/ 00122 00123 //This task performs normal stack task including checking for incoming 00124 //packet, type of packet and calling appropriate stack entity to 00125 //process it. 00126 StackTask(); 00127 } 00128 }
1.5.2