// Create and initialize 4 Channels from W3100

for(i = 0 ; i < MAX_SOCK_NUM; i++)

{

s[i] = socket(SOCK_STREAM,80,0); // Create and initialize HTTP Channel

SSTATUS[i] = SOCK_CLOSED; // Initialize channel status varibles

if(INVALID_SOCKET == s[i]) goto PRG_END; // Fail to create channel, terminate program

// Non Blocking Listen Command

if(SOCKET_ERROR == NBlisten(s[i])) goto PRG_END; // If it is error, terminate program

SSTATUS[i] = SOCK_LISTEN; // Change the channel status varible to Listen mode

}// end for

// Check the status of each channel

while (1)

{

for (i = 0; i < MAX_SOCK_NUM; i++)

{

if(SSTATUS[i] == SOCK_ESTABLISHED)

{

Length = select(s[i], SEL_RECV);

if(Length > 0)

{

Length = recv(s[i], Data_Buf, Length);

Con_Type = ParseReq(Data_Buf);

Length = PrintHeader(TX_Buf, Con_Type);

switch (Con_Type)

{

case 'g':

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

break;

case 'j':

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

break;

case 'h':

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

break;

case 'a':

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

break;

case 'c':

PutStringLn("EVB CONTROL");

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

break;

case 'x':

PutStringLn("EXIT");

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

break;

default :

PutStringLn("DEFAULT");

Length += DoHTML(TX_Buf + Length);

Length = send(s[i], TX_Buf, Length);

}

if(Length == SOCKET_ERROR)

{

GetLastError();

goto PRG_END; // Terminate program

}

}

}

// Check the status whether the client try to connect or not,

// if client succeeded, update SSTATUS[I] to SOCK_ESTABLISHED

// and accept to set up network information of client

else if(select(s[i],SEL_CONTROL) == SOCK_ESTABLISHED) // Connect OK?

{

// Accept the client's connection request

if( SOCKET_ERROR == accept(s[i],&ChildAddr))

{

if(SCLOSED == GetLastError())

{

PutString("Before Server accepted client, the client already closed ");

PutHTOA(s[i]);

PutStringLn("th socket");

goto NEXT0;

}

goto PRG_END;

}

SSTATUS[i] = SOCK_ESTABLISHED; // Update socket status variable

}

NEXT0:

// Check the status whether client close the socket or not,

// Release the allocated socket from W3100, and create and initialize a new socket

// for another client and update it to Listen mode

/*else*/ if( select(s[i],SEL_CONTROL) == SOCK_CLOSED ) // If the client closed socket?

{

PutHTOA(i);PutStringLn("th Socket are closed by Client");

// Close the current using socket

if(SOCKET_ERROR == close(s[i])) goto PRG_END;

PutHTOA(s[i]);PutStringLn("th Socket Closed");

// Initialize socket status variable

SSTATUS[i] = SOCK_CLOSED;

// Reallocate a socket

s[i] = socket(SOCK_STREAM,5000,0);

if(s[i] == INVALID_SOCKET) goto PRG_END;

if(SOCKET_ERROR == NBlisten(s[i])) goto PRG_END;

// Update socket status variable to Listen mode

PutHTOA(i) ; PutStringLn("th Socket Listened");

SSTATUS[i] = SOCK_LISTEN;

}