/* udp_serv.c: Support echo server for UDP Port 10000 and TCP port 10001. Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) Compile mode: Large Project: Client.c ..\lib\7188el.Lib ..\lib\tcpip32.lib ..\lib\FW_nnnnn.LIB Hordware: I-7188EX [Nov 04, 2008] by Liam [Aug,02,2011] by Nicholas */ #include #include #include "..\lib\7188e.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" #define _SHOW_IP_ADDRESS_ void do_echo( int skt , int mode); #define BUFSIZE 1460 /* read/write buffer size */ TCP_SERVER TcpServer={ 10001, // port -1, // socket 0, // state 0, // connect do_echo // CallBackFun }; void DoUdp10K(int skt); UDP_SOCKET Udp10K={ -1, // int socket; 10000, // unsigned MyPort; NULL, // char *RemoteIp; 0, // unsigned RemotePort; 1, // int fNonBlock; 1, // int fEnableBroadcast; 0, // int status; DoUdp10K // void (*CallBackFun)(int skt); }; 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; } /* demo for use command 19 or 23 */ int UserCmd19_23(PTCPREADDATA p) { int skt; unsigned char *buf; int length,i; skt=p->Socket; buf=p->ReadUartChar; length=p->Length; /* Just reply the input command here. */ XS_WriteSocket(skt, buf, length); return 1; } char *GetModuleName(void) /* User's program must support this function for UDP search */ { return "I-7188EX-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 TCP server Port 10001 XS_AddUdpSocket(&Udp10K); //add UDP server Port 10000 XS_AddSystemLoopFun(XS_SocketLoopFun); XS_StartSocket(); /* bNeedPassword=1; // enable password function. pXS_CheckPassword=XS_CheckPassword; // A built-in function that allows the user to use their own password checking function pXS_Port10kCmd[0]=XS_SetPassword_00; // A built-in function which allows the user to set up a password PasswordMsg="Need Password:"; // A message is sent when the connection is established on Port 10000 depending on the user */ pXS_Port10kCmd[33]=XS_LoadFile_33; // Command 33 is used to download files in VCOM3 using port 10000 and is used to download files to modules pXS_Port10kCmd[19]=UserCmd19_23; // For command 19( there is some different with original x-server command 19) pXS_Port10kCmd[23]=UserCmd19_23; // For command 23 pXS_Port10kCmd[17]=XS_Inport_17; // Command 17 is the import command in VCOM3 and can input hardware I/O data. pXS_Port10kCmd[18]=XS_Outport_18; // Command 18 is the output command in VCOM3 and can output hardware I/O data. 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 } // 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 { for(i=0; i0) { buf[len]=0; // here just echo back the received message. Puts(buf); i = sendto(skt, buf, len, 0, (struct sockaddr *) &sockAddrWhoSent, sizeof(sockAddrWhoSent)); if (i<0) { //do nothing - no retry mechanism Print("sendto failed A: err %d(errno=%d)\r\n",i,errno); } } else { Print("Udp read error:%d(srrno=%d)\r\n",len,errno); } } void main(int argc,char *argv[]) { XS_main(argc,argv); /* call the XS library main function. */ }