/* Demo1 for X-Server Light. Support one client for TCP port 10001 test with XS_09201.LIB */ #include #include #include"..\lib\8000a.h" #include"..\lib\Tcpip32.h" #include"..\lib\MFW.H" #define BUFSIZE 1460 /* read/write buffer size */ int ClientSocket=-1; void do_GetResponse( int skt, int mode); //TCP client's callback function void ShowSocketStatus(void); TCP_CLIENT TcpClient= //Initialize a TCP Client with default IP 10.0.8.157 and port 10001 { "10.0.9.197", // char *ip; 10000, // unsigned port; -1, // int socket; 0, // int tmpState; -2, // int bConnected; 10, // int iConnectTryCount; //連線不成功時重試次數 4000, // long lConnetTimeout; //等連線成功的 timeout 時間。(0 就用預設值 400ms) 0, // long ltmpT; do_GetResponse, //void (*CallBackFun)(int skt); NULL }; /************************************************************************/ char *GetModuleName(void) /* User's program must support this function for UDP search */ { return "i-7188E-XS" ; } /************************************************************************/ 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; /* vaariable in tcp.c */ extern long MAXTXTOUT; void UserLoop(void); char version[20]; char MsgTest[]="command"; int MsgTest_len=sizeof(MsgTest); int i,port; InitLib(); Print("sizeof(MsgTest)=%d\r\n",MsgTest_len); 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]); /* 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 */ //Print("IP=%d.%d.%d.%d\r\n",); 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'){ //Break the TCP connection if(TcpClient.bConnected==1){ XS_CloseSocket(TcpClient.socket); } } else if(Command[1]=='C'){ //Build the TCP connection if(TcpClient.bConnected==-1){ TcpClient.bConnected=-2; } } else if(Command[1]=='0' && Command[2]=='0'){ //Show the socket status ShowSocketStatus(); } else { //unknow command Print("Unsupport command\r\n"); } } //The String that isn't the command will send to client else if(TcpClient.bConnected==1){ 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"); Print("TcpClient.bConnected == %d\r\n",TcpClient.bConnected); } idx=0; } break; } } } /************************************************************************* * do_echo * * when client is connected, do echo here *************************************************************************/ char buf[BUFSIZE],SendBack[BUFSIZE+2]; void do_GetResponse( int skt,int mode) { int i, cc, err, Sendlen; if(!mode){ // mode=0,剛連線成功 if( ClientSocket < 0) ClientSocket=skt; } 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; i < cc; i++ ) { Putch(buf[i]); if(buf[i]=='\r') Putch('\n'); } } } } /************************************************************************/ /* 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. */ } /************************************************************************/ #define ESTABLISHED 1 /* state machine states, see handbook */ #define FINWAIT_1 2 #define FINWAIT_2 3 #define CLOSED_WAIT 4 #define TIMEWAIT 5 #define LAST_ACK 6 #define CLOSED 7 #define SYN_SENT 8 #define SYN_RECEIVED 9 #define LISTEN 16 void ShowSocketStatus(void) { int i; Puts("\r\n"); for(i=0;i