#include "net\tcp.h" //The following code is for the Microchip MPLAB C18 compiler. For the //Hi-Tech compiler, the #pragma's are not required #pragma udata BUFFER1_256BYTES BYTE tcpRxBuf[256]; #pragma udata //Return to default section //Get and Put pointer for tcpRxBuf circular buffer. If Get pointer = Put //pointer, the buffer is empty. BYTE tcpRxBufGet, tcpRxBufPut; //Create a TCP socket for receiving and sending data static TCP_SOCKET tcpSocketUser = INVALID_SOCKET; void main(void) { BYTE c; NODE_INFO tcpServerNode; //Set TCP Receive buffer to be empty tcpRxBufGet = tcpRxBufPut = 0; //Configure to listen on port 54123 tcpSocketUser = TCPListen(54123); //An error occurred during the TCPListen() function if (tcpSocketUser == INVALID_SOCKET) { //Take any additional action that is required when an error occurs } //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. Store all data to a buffer as soon as it is detected if (TCPIsGetReady(tcpSocketUser)) { //Read all data from socket, and save it to TCP Receive buffer (tcpRxBuf) while(TCPGet(tcpSocketUser, &c)) { //Store byte read from TCP socket to TCP buffer tcpRxBuf[tcpRxBufPut++] = c; //If Put pointer caught up to Get pointer, our buffer is full and we have //lost data! //Increment Get pointer. if (tcpRxBufPut == tcpRxBufGet) { tcpRxBufGet++; } } //Discard the socket buffer. TCPDiscard(tcpSocketUser); } //Does the TCP receive buffer contain any data? If so, we sent this data back //to the same socket that just send us data. if(tcpRxBufPut != tcpRxBufGet) { //Checks if there is a transmit buffer ready for accepting data, and that //the given socket is valid (not equal to INVALID_SOCKET for example) if (TCPIsPutReady(tcpSocketUser)) { //Transmit entire buffer while (tcpRxBufGet != tcpRxBufPut) { TCPPut(tcpSocketUser, tcpRxBuf[tcpRxBufGet++]); } // Now transmit it. TCPFlush(tcpSocketUser); } } } //This task performs normal stack task including checking for incoming packet, //type of packet and calling appropriate stack entity to process it. StackTask(); } }