Simple TCP server.
Program Ethernut with tcps.rom and enter
telnet x.x.x.x 12345
on a command prompt, replacing x.x.x.x with the IP address of your ethernut board. Enter help for a list of available commands.
#include <string.h> #include <dev/nicrtl.h> #include <sys/heap.h> #include <sys/thread.h> #include <sys/timer.h> #include <sys/print.h> #include <netinet/sostream.h> #include <arpa/inet.h> static u_char buff[128]; /* * Process client requests. */ void ProcessRequests(NUTDEVICE *sostream) { int l; strcmp(buff, "TEST"); NutPrintString(sostream, "200 Welcome to tcps. Type help to get help.\r\n"); for(;;) { NutPrintFlush(sostream); if(NutDeviceGetLine(sostream, buff, sizeof(buff)) < 0) break; /* * Ignore blank lines. */ if(buff[0] == 0) continue; /* * Memory info. */ l = strlen(buff); if(strncmp(buff, "memory", l) == 0) { NutPrintFormat(sostream, "210 %u bytes RAM free\r\n", NutHeapAvailable()); continue; } /* * List threads. */ if(strncmp(buff, "threads", l) == 0) { NUTTHREADINFO *tdp; NUTTIMERINFO *tnp; NutPrintString(sostream, "220 List of threads with name,state,prio,stack,mem,timeout follows\r\n"); for(tdp = nutThreadList; tdp; tdp = tdp->td_next) { NutPrintString(sostream, tdp->td_name); NutPrintString(sostream, "\t"); switch(tdp->td_state) { case TDS_TERM: NutPrintString(sostream, "\tTerm\t"); break; case TDS_RUNNING: NutPrintString(sostream, "\tRun\t"); break; case TDS_READY: NutPrintString(sostream, "\tReady\t"); break; case TDS_SLEEP: NutPrintString(sostream, "\tSleep\t"); break; } NutPrintInteger(sostream, tdp->td_priority, 10, 0, 0); NutPrintString(sostream, "\t"); NutPrintInteger(sostream, (u_short)tdp->td_sp - (u_short)tdp->td_memory, 10, 0, 0); if(*((u_long *)tdp->td_memory) != DEADBEEF) NutPrintString(sostream, "\tCorrupted\t"); else NutPrintString(sostream, "\tOK\t"); if((tnp = (NUTTIMERINFO *)tdp->td_timer) != 0) NutPrintInteger(sostream, tnp->tn_ticks_left, 10, 0, 0); else NutPrintString(sostream, "None"); NutPrintString(sostream, "\r\n"); } NutPrintString(sostream, ".\r\n"); continue; } /* * List threads. */ if(strncmp("timers", buff, l) == 0) { NUTTIMERINFO *tnp; NutPrintString(sostream, "221 List of timers with ticks left and interval follows\r\n"); for(tnp = nutTimerList; tnp; tnp = tnp->tn_next) { NutPrintInteger(sostream, tnp->tn_ticks_left, 10, 0, 0); if(tnp->tn_ticks) { NutPrintString(sostream, "\t"); NutPrintInteger(sostream, tnp->tn_ticks, 10, 0, 0); } else NutPrintString(sostream, "\tOneshot"); NutPrintString(sostream, "\r\n"); } NutPrintString(sostream, ".\r\n"); continue; } NutPrintString(sostream, "400 List of commands follows\r\n"); NutPrintString(sostream, "memory\tQueries number of RAM bytes free\r\n"); NutPrintString(sostream, "threads\tLists all created threads\r\n"); NutPrintString(sostream, "timers\tLists all running timers\r\n"); NutPrintString(sostream, ".\r\n"); } } /* * Main application routine. * * Nut/OS automatically calls this entry after initialization. */ THREAD(NutMain, arg) { TCPSOCKET *sock; NUTDEVICE *sostream; /* * Register Realtek controller at address 8300 hex * and interrupt 5. */ NutRegisterDevice(&devEth0, 0x8300, 5); /* * Configure lan interface. * */ NutNetAutoConfig("eth0"); /* * Now loop endless for connections. */ for(;;) { /* * Create a socket. */ sock = NutTcpCreateSocket(); /* * Listen on port 12345. If we return, * we got a client. */ NutTcpAccept(sock, 12345); /* * Create a device from the socket, * so we can use NutPrintFormat() and * other device goodies. */ sostream = NutSoStreamCreate(sock); /* * Process client requests. */ ProcessRequests(sostream); /* * Destroy our device. */ NutSoStreamDestroy(sostream); /* * Close our socket. */ NutTcpCloseSocket(sock); } }