ex_tcp_echo.c

This example program shows how to send and 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 read it and return data to the same socket (IP and port) that just sent us data. It implements a circular buffer for storing the received data.

00001 
00010 #include "net\tcp.h"
00011 
00012 //The following code is for the Microchip MPLAB C18 compiler. For the
00013 //Hi-Tech compiler, the #pragma's are not required
00014 #pragma udata BUFFER1_256BYTES
00015 BYTE tcpRxBuf[256];
00016 #pragma udata   //Return to default section
00017 
00018 //Get and Put pointer for tcpRxBuf circular buffer. If Get pointer = Put
00019 //pointer, the buffer is empty.
00020 BYTE tcpRxBufGet, tcpRxBufPut;
00021 
00022 //Create a TCP socket for receiving and sending data
00023 static TCP_SOCKET tcpSocketUser = INVALID_SOCKET;
00024 
00025 
00026 void main(void)
00027 {
00028     BYTE c;
00029     NODE_INFO tcpServerNode;
00030 
00031     //Set TCP Receive buffer to be empty
00032     tcpRxBufGet = tcpRxBufPut = 0;
00033 
00034     //Configure to listen on port 54123
00035     tcpSocketUser = TCPListen(54123);
00036     
00037     //An error occurred during the TCPListen() function
00038     if (tcpSocketUser == INVALID_SOCKET) {
00039         //Take any additional action that is required when an error occurs
00040     }
00041 
00042     //Infinite loop. Check if anything is received on TCP port
00043     while(1)
00044     {
00045         //Has a remote node made connection with the port we are listening on
00046         if (TCPIsConnected(tcpSocketUser)) {
00047             //Is there any data waiting for us on the TCP socket?
00048             //Because of the design of the Modtronix TCP/IP stack we have to consume
00049             //all data sent to us as soon
00050             //as we detect it. Store all data to a buffer as soon as it is detected
00051             if (TCPIsGetReady(tcpSocketUser)) {
00052 
00053                 //Read all data from socket, and save it to TCP Receive buffer (tcpRxBuf)
00054                 while(TCPGet(tcpSocketUser, &c)) {
00055                     //Store byte read from TCP socket to TCP buffer
00056                     tcpRxBuf[tcpRxBufPut++] = c;
00057             
00058                     //If Put pointer caught up to Get pointer, our buffer is full and we have
00059                     //lost data!
00060                     //Increment Get pointer.
00061                     if (tcpRxBufPut == tcpRxBufGet) {
00062                         tcpRxBufGet++;
00063                     }
00064                 }
00065 
00066                 //Discard the socket buffer.
00067                 TCPDiscard(tcpSocketUser);
00068             }
00069 
00070 
00071             //Does the TCP receive buffer contain any data? If so, we sent this data back
00072             //to the same socket that just send us data. 
00073             if(tcpRxBufPut != tcpRxBufGet) {
00074                 //Checks if there is a transmit buffer ready for accepting data, and that
00075                 //the given socket is valid (not equal to INVALID_SOCKET for example)
00076                 if (TCPIsPutReady(tcpSocketUser)) {
00077             
00078                     //Transmit entire buffer
00079                     while (tcpRxBufGet != tcpRxBufPut) {
00080                         TCPPut(tcpSocketUser, tcpRxBuf[tcpRxBufGet++]);
00081                     }
00082 
00083                     // Now transmit it.
00084                     TCPFlush(tcpSocketUser);
00085                 }
00086             }
00087         }
00088 
00089 
00090         //This task performs normal stack task including checking for incoming packet,
00091         //type of packet and calling appropriate stack entity to process it.
00092         StackTask();
00093     }
00094 }

Generated on Thu Sep 21 20:31:01 2006 for SBC65EC Web Server by  doxygen 1.4.7