/* TCP_Server.c: TCP Server Demo program for 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: TCP_Server.C ..\..\lib\7188el.lib ..\..\lib\tcpip32.lib Details: Setup a server, wait for connections. While a connection has been established, receive data from clients. [30 Apr, 2008] by Liam [Aug,3,2011] by Nicholas [March,19,2012] by Nicholas */ #include #include #include #include #include "..\..\lib\7188e.h" #include "..\..\lib\Tcpip32.h" #define MaxClentSocket 32 #define NUM_OF_SERVICE_PORT 2 fd_set rfds,s_fds; // server socket set of socket file descriptors for reading unsigned ActiveSkt[2]={0, 0}; struct timeval seltime={0,20}; unsigned short service_port[NUM_OF_SERVICE_PORT]={10000, 10001}; int socketPort[MaxClentSocket]; void Port10000(int current_socket) { // current_socket: Socket number int iRet; char buf[1024]; iRet=recv(current_socket, buf, 1024, 0); if(iRet<=0) { shutdown(current_socket,2); closesocket(current_socket); FD_CLR(current_socket,&s_fds); socketPort[current_socket]=-1; if(current_socket>=16) ActiveSkt[1]&=~(1<<(current_socket-16)); else ActiveSkt[0]&=~(1<=16) ActiveSkt[1]&=~(1<<(current_socket-16)); else ActiveSkt[0]&=~(1<0) { memset( &clientAdd, 0, sizeof(clientAdd)); clientAddLen = sizeof(clientAdd); c = accept(s[i],(struct sockaddr *)&clientAdd, &clientAddLen); Print("create socket %d \n\r",c); if(c<0) { shutdown(c,2); closesocket(c); } else if(c>0 && c=16) ActiveSkt[1]|=1<<(c-16); else ActiveSkt[0]|=1<0)) { if(socketPort[i]==10000) { Port10000(i); } else if(socketPort[i]==10001) { Port10001(i); } } } } //Press ESC to terminate the program. if(Kbhit() && Getch()==27) { Nterm(); return; } } }