/* Server.c: Implement one TCP/IP server. Support one server for TCP port 1500. Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) Compile mode: Large Project: Server.c ..\lib\7186el.lib ..\lib\tcp_dm32.lib ..\lib\FW_nnnnn.lib Hordware: uPAC-7186EX [Dec 18, 2008] by Liam [Dec 23, 2010] by Nicholas */ #include #include #include "..\lib\7186e.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; /* variable in tcp.c */ extern long MAXTXTOUT; void UserLoop(void); char version[20]; InitLib(); bAcceptBroadcast=0; ACKDELAY=200; GetIp(MyIp); Print("IP=%d.%d.%d.%d\r\n", MyIp[0], MyIp[1], MyIp[2], MyIp[3]); LedTime=GetTimeTicks(); XS_AddSystemLoopFun(UserLoop); XS_AddServer(&TcpServer); //Add a TCP server XS_AddSystemLoopFun(XS_SocketLoopFun); XS_StartSocket(); EnableWDT(); MAXTXTOUT=6000UL; } 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); } } } } void main(int argc,char *argv[]) { XS_main(argc,argv); /* call the XS library main function. */ }