/* SERVER.c: TCP Server Demo program for i-7188E series A server listen and wait to handle the coming connections Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) Compile mode: large Project: Server.C ..\lib\g4500.lib ..\lib\tcp_dm32.lib Details: Setup a server, wait for connections. While a connection has been established, receive data from clients. [2010/05/18] Modify by yide [30 Apr, 2008] by Liam */ #include #include #include #include #include "..\lib\g4500.h" #include "..\lib\Tcpip32.h" #define SOCKET_NUM 32 // define the maximum number of sockets #define SOCKET_QUEUE_LENGTH SOCKET_NUM -1 // maximum connection queue length #define NUM_OF_SERVICE_PORT 2 unsigned short service_port[NUM_OF_SERVICE_PORT]={10000, 10001}; // define the service port for Server struct timeval seltime={0, 20}; // 2000ns=2ms unsigned ActiveSkt[2]={0, 0}; STOPWATCH swSocket[SOCKET_NUM], swTimer; ulong ulSocketTimeout=86400000UL; // Sets the socket communication timeout value. // If the timeout expires, the connection will be released // Time unit: ms fd_set rfds; // set of socket file descriptors for reading typedef struct tagSocket_State { int active; // socket is active int init; // socket just initiated struct sockaddr_in sin; // client address unsigned port; // port number int be_master_socket; // is master socket } socket_state; socket_state socket_state_table[SOCKET_NUM]; // sockets state table struct ip host_ip; int TCP_server_initial(void) { /* Return: 0: ok. -1: function "llip" error -2: function "Ninit" error -3: function "Portinit" error */ int i, iRet; GetSavedIp((struct ip*) &host_ip); // read the saved IP address and mask from EEPROM of 7188E iRet=lhip("7186EX", &host_ip); iRet=Ninit(); // initiate host environment if(iRet!=NoError) return -2; iRet=Portinit("*"); // initiate all network device if(iRet!=NoError) { Nterm(); return -3; } FD_ZERO(&rfds); for(i=0; i=16) ActiveSkt[1]|=1<<(err-16); else ActiveSkt[0]|=1<=16) ActiveSkt[1]&=~(1<<(current_socket-16)); else ActiveSkt[0]&=~(1<%s\r\n", buf); send(current_socket, buf, strlen(buf), 0); T_StopWatchStart(&swSocket[current_socket]); } void Port10001(int current_socket) { // current_socket: Socket number int iRet; char buf[1024]; iRet=recv(current_socket, buf, 1024, 0); if(iRet<=0) { TCP_close_socket(current_socket); return; } buf[iRet]=0x0; Print("Echo for Client(10001)=>%s\r\n", buf); send(current_socket, buf, strlen(buf), 0); T_StopWatchStart(&swSocket[current_socket]); } void main(void) { int err, i, service_loop_continue; InitLib(); ulSocketTimeout=10000; // socket timeout=10000 ms //Step 1: Initialize TCP Server err=TCP_server_initial(); if(err<0) { if(err==-1) Print("This program must be executed in uPAC-7186EX series.\r\n"); if(err==-2) Print("Ninit() failed! error %d\r\n", err); if(err==-3) Print("Portinit() failed! error %d\r\n", err); } else Print("Initialize TCP Server\r\n"); //Step 2: Create sockets and bound to names then listen for connections for(i=0; i=0) continue; // success if(err==-1) Print("cannot create a socket on port: %d", service_port[i]); if(err==-2) Print("cannot bind to port %d \n\r", service_port[i]); if(err==-3) Print("cannot listen on port %d \n\r", service_port[i]); } //Step 3: Service loop: 3-1: wait on sockets and accept // 3-2: perform service, receive and send service_loop_continue=1; T_StopWatchStart(&swTimer); while(service_loop_continue) // service loop { // check the socket timeout at 50 ms intervals if(T_StopWatchGetTime(&swTimer)>=50) { T_StopWatchStart(&swTimer); if(ulSocketTimeout) { static int iSocket=0; int mask; if(iSocket>=16) mask=ActiveSkt[1]&(1<<(iSocket-16)); else mask=ActiveSkt[0]&(1<=ulSocketTimeout) { Print("Socket timeout, socket#%d closed\r\n", iSocket); TCP_close_socket(iSocket); } iSocket++; if(iSocket>=SOCKET_NUM) iSocket=0; } } YIELD(); //Step 3-1: Wait for activity on sockets and accept a connection err=wait_to_accept(); if(err==-1) { Print("selectsocket error\n\r"); break; } else if(err==-2) continue; //selectsocket() time out else if(err==-3) Print("accept error %d\n\r"); //Step 3-2: perform service, receive and send for(i=0; i