/* client.c: Implement one TCP/IP client connection. Support one client for 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 [Dec 18, 2008] by Liam [Aug,2,2011] by Nicholas */ #include #include #include "..\lib\7188e.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" #define BUFSIZE 1460 // read/write buffer size void do_GetResponse( int skt,int mode); // TCP client's callback function void ShowSocketStatus(void); TCP_CLIENT TcpClient= { "10.0.9.245", // char *ip; 10001, // unsigned port; -1, // int socket; 0, // int tmpState; -2, // int bConnected; 10, // int iConnectTryCount; // Frequency to reconnect to remote device 4000, // long lConnetTimeout; // Timeout to connect to remote device // 0: default value is 400ms 0, // long ltmpT; (void *) do_GetResponse, // void (*CallBackFun)(int skt); NULL }; 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" ; } unsigned char MyIp[4]; void XS_UserInit(int argc,char *argv[]) { extern int bAcceptBroadcast; extern unsigned long ACKDELAY; // variable in tcp.c extern long MAXTXTOUT; extern long ET_TOUT; void UserLoop(void); char version[20]; int i,port; InitLib(); bAcceptBroadcast=0; ACKDELAY=200; Print("7188E library version: %d \r\n",GetLibVersion()); XS_GetVersion(version); Print("[X-Server library]: version=%s \r\n",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]); //ET_TOUT=200; Print("ET_TOUT =%lu\r\n",ET_TOUT); XS_AddSystemLoopFun(UserLoop); for(i=1; i99 Putch(key); if(key=='\r') { //The terminal character of a string is '\r' (Press 'Enter' key) Command[idx]=0; Putch('\n'); if(Command[0]=='/') { // '/' denotes that the inputted string was a command if(Command[1]=='Q') { if(TcpClient.bConnected==1) XS_CloseSocket(TcpClient.socket); // Break the TCP connection } else if(Command[1]=='C') { if(TcpClient.bConnected==-1) TcpClient.bConnected=-2; // Build the TCP connection } else if(Command[1]=='0' && Command[2]=='0') { ShowSocketStatus(); // Show the socket status } else { Print("Unsupport command\r\n"); // unknow command } } else if(TcpClient.bConnected==1) { // The String that isn't the command will send to client err= writesocket(TcpClient.socket,Command,idx); Print(Command); if(err<0) { // write error Print( "write error %d, close client socket.\r\n", err); XS_CloseSocket(TcpClient.socket); } } else Print("[do nothing]\r\n"); idx=0; } break; } } } char buf[BUFSIZE]; // when client is connected, do echo here void do_GetResponse(int skt, int mode) { int i, cc, err, Sendlen; if(!mode) { if(TcpClient.bConnected==1) { Print("Connected\r\n"); } else if(TcpClient.bConnected==-1) Print("%s connect failed\r\n",TcpClient.ip); } else { err=cc=readsocket(skt, buf, sizeof(buf)-1); if(err<=0) { // error or disconnected by remote side Print("%d TcpClient close skt = %d\r\n", err, skt); XS_CloseSocket(skt); } else { for(i=0; i