/* Server.c: Implement one TCP/IP server. Support one server for TCP port 10001. Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp) Compile mode: Large Project: Server.c ..\lib\7186el.lib ..\lib\tcp_dm32.lib ..\lib\FW_nnnnn.lib [Dec 18, 2008] by Liam */ #include #include #include "..\lib\vp2k.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" #define _SHOW_IP_ADDRESS_ #define BUFSIZE 1460 /* read/write buffer size */ void do_echo(int skt , int mode); TCP_SERVER TcpServer={ 1500, // port -1, // socket 0, // state 0, // connect do_echo // CallBackFun }; unsigned char MyIp[4]; long LedTime; void ShowIp(void) { static int idx=0; int data; data=MyIp[idx]; idx++; #ifndef _NO_LED5_ Show5DigitLedWithDot(1, idx); Show5DigitLed(5, data%10); data/=10; if(data) { Show5DigitLed(4, data%10); data/=10; if(data) Show5DigitLed(3,data); else Show5DigitLed(3, 16); } else { Show5DigitLed(4, 16); Show5DigitLed(3, 16); } Show5DigitLed(2, 16); #endif if(idx>=4) idx=0; } char *GetModuleName(void) /* User's program must support this function for UDP search */ { return "uPAC-7186EX-FW"; } char *GetAliasName(void) /* User's program must support this function for UDP search */ { return "test1"; } void XS_UserInit(int argc, char *argv[]) { extern int bAcceptBroadcast; extern unsigned long ACKDELAY; /* vaariable in tcp.c */ extern long MAXTXTOUT; void UserLoop(void); char version[20]; InitLib(); bAcceptBroadcast=0; ACKDELAY=200; XS_GetVersion(version); Print("[X-Server library]: version=%s ", version); XS_GetLibDate(version); Print("date=%s\r\n", version); GetTcpipLibDate(version); Print("Tcpip library version:%X, Library Date is %s\r\n", GetTcpipLibVer(), version); GetIp(MyIp); Print("IP=%d.%d.%d.%d\r\n", MyIp[0], MyIp[1], MyIp[2], MyIp[3]); LedTime=GetTimeTicks(); /* for xs_09001.lib ~ xs_09003.lib NEED NOT the next 4 lines. */ XS_AddSystemLoopFun(UserLoop); /* need add this line for the library 0.9.100 date after 2004/06/02 */ XS_AddServer(&TcpServer); //Add a TCP server /* to use TCP/IP function, add the following two lines after XS library 0.9.3.00*/ XS_AddSystemLoopFun(XS_SocketLoopFun); XS_StartSocket(); //Called on xs09300.lib[2006/09/26] or later EnableWDT(); MAXTXTOUT=6000UL; /* function for */ } void XS_UserEnd(void) { XS_StopSocket(); DisableWDT(); } unsigned status_8019as=0; int Socket0=-1; extern int errno; long st,old_dt; unsigned char TestData='0'; void UserLoop(void) { long tmpt; int key,err; RefreshWDT(); if(Kbhit()){ switch((key=Getch())){ case 27: QuitMain=1; break; default: break; } } #ifdef _SHOW_IP_ADDRESS_ tmpt=GetTimeTicks(); if(tmpt-LedTime>=500){ LedTime+=500; LedToggle(); ShowIp(); } #endif } /************************************************************************* * do_echo * * when client is connected, do echo here *************************************************************************/ char buf[BUFSIZE],SendBack[BUFSIZE+2]; void do_echo( int skt ,int mode) { int i, cc, err, Sendlen; if(!mode){ Sendlen=sprintf(SendBack,"Welcome connect to port %u\r\nThere are %d clients connect to this port\r\n", TcpServer.port,TcpServer.connect); goto SendMessage; } else { err = cc = readsocket( skt, buf, sizeof(buf)-1); if (err <= 0) { /* error or disconnected by remote side */ XS_CloseSocket(skt); } else { buf[cc]=0; Print(buf); for( i=0; i < cc; i++ ) { SendBack[i]=buf[i]; } Sendlen=cc; SendMessage: err= writesocket(skt,SendBack,Sendlen); if (err < 0) { /* write error */ Print( "echo write error %d\n\r", err ); XS_CloseSocket(skt); } } } } /* XS 0.9.200 or later must add the function main() */ void main(int argc,char *argv[]) { XS_main(argc,argv); /* call the XS library main function. */ }