// server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#include <windows.h> // use Sleep function
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Need to link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 2048
#define DEFAULT_PORT 27013
char RecvBuf[DEFAULT_BUFLEN];
char SendBuf[DEFAULT_BUFLEN];
char * TokenPtr = NULL;
struct sockaddr_in ServerCfg;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET;
int cnt = 0;
char RespondInfo[] =
{
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connnection: close\r\n"
"\r\n"
"<!DOCTYPE HTML>\r\n"
"<p>"
"<img src=\"http://www.coocox.org/images/CooCox.gif\" />"
"</p>"
"<html>\r\n"
"<body>\r\n"
"<form name=\"input\" method=\"get\">\r\n"
"ON: \r\n"
"<input type=\"radio\" name=\"LED\" value=\"ON\" checked=\"checked\">\r\n"
"<br />\r\n"
"OFF: \r\n"
"<input type=\"radio\" name=\"LED\" value=\"OFF\">\r\n"
"<br />\r\n"
"<input type =\"submit\" value =\"Submit\">\r\n"
"</form>\r\n"
//"<h1 align=\"center\"><pre>"
//"================= WIFI Shield Dirver===============<br />"
//"Name: TCP Socket Http Webserver Example<br />"
//"PORT : 27013<br />"
//"===================<a href=\"http://www.coocox.org\">"
//"CooCox Team</a>=====================<br />"
//"</h1></pre>"
"</body>\r\n"
"</html>\r\n"
"\r\n"
};
char RespondLED_ON[] =
{
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connnection: close\r\n"
"\r\n"
"<!DOCTYPE HTML>\r\n"
"<p>"
"<img src=\"http://www.coocox.org/images/CooCox.gif\" />"
"</p>"
"<html>\r\n"
"<body>\r\n"
"<p>LED Status</p>"
"<form name=\"input\" method=\"get\">\r\n"
"ON: \r\n"
"<input type=\"radio\" name=\"LED\" value=\"ON\" checked=\"checked\">\r\n"
"<br />\r\n"
"OFF: \r\n"
"<input type=\"radio\" name=\"LED\" value=\"OFF\">\r\n"
"<br />\r\n"
"<input type =\"submit\" value =\"Submit\">\r\n"
"</form>\r\n"
"<h1 align=\"center\"><pre>"
"LED is ON"
"</h1></pre>"
"</body>\r\n"
"</html>\r\n"
"\r\n"
};
char RespondLED_OFF[] =
{
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connnection: close\r\n"
"\r\n"
"<!DOCTYPE HTML>\r\n"
"<p>"
"<img src=\"http://www.coocox.org/images/CooCox.gif\" />"
"</p>"
"<html>\r\n"
"<body>\r\n"
"<p>LED Status</p>"
"<form name=\"input\" method=\"get\">\r\n"
"ON: \r\n"
"<input type=\"radio\" name=\"LED\" value=\"ON\">\r\n"
"<br />\r\n"
"OFF: \r\n"
"<input type=\"radio\" name=\"LED\" value=\"OFF\" checked=\"checked\">\r\n"
"<br />\r\n"
"<input type =\"submit\" value =\"Submit\">\r\n"
"</form>\r\n"
"<h1 align=\"center\"><pre>"
"LED is OFF"
"</h1></pre>"
"</body>\r\n"
"</html>\r\n"
"\r\n"
};
char Respond_ICO[] =
{
"HTTP/1.1 200 OK\r\n"
"Connnection: close\r\n"
"\r\n"
};
int main(void)
{
WSADATA wsaData;
int iResult = 0;
int iSendResult = 0;
//printf(WelcomeInfo);
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
printf("Initial WinSock OK\r\n");
// Create a SOCKET
ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ListenSocket == INVALID_SOCKET)
{
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("Create Socket OK\r\n");
// Setup the TCP listening socket
ServerCfg.sin_family = AF_INET;
ServerCfg.sin_port = htons(DEFAULT_PORT);
ServerCfg.sin_addr.s_addr = htonl(INADDR_ANY);
iResult = bind(ListenSocket,(struct sockaddr *)&ServerCfg,
sizeof(struct sockaddr));
if(iResult == SOCKET_ERROR)
{
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("Bind to PORT OK\r\n");
// Begin listen PORT
iResult = listen(ListenSocket, 5);
if (iResult == SOCKET_ERROR)
{
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("Start Listen PORT\r\n");
printf("Waitting Client ...\r\n");
while(1)
{
// Accept a client socket
ClientSocket = accept(ListenSocket, NULL, NULL);
if (ClientSocket == INVALID_SOCKET)
{
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
printf("\r\n=============================================\r\n");
printf("Client Connect Now : %d\r\n", cnt++);
// No longer need server socket
//closesocket(ListenSocket);
// Get Request
// Receive Full response message
printf("Receive Web bowser request\r\n\r\n");
memset(RecvBuf, '\0', DEFAULT_BUFLEN);
iResult = recv(ClientSocket, RecvBuf, DEFAULT_BUFLEN, 0);
if (iResult < 0)
{
printf("receive failed with error: %d\r\n", WSAGetLastError());
}
else if(iResult > 0)
{
puts(RecvBuf);
}
else
{
printf("Connect Closed!\r\n");
}
TokenPtr = strstr(RecvBuf, "LED");
if(TokenPtr == NULL)
{
TokenPtr = strstr(RecvBuf, "favicon.ico");
if(TokenPtr == NULL)
{
// Send html to client
iResult = send(ClientSocket, RespondInfo, strlen(RespondInfo), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed with error: %d\r\n", WSAGetLastError());
}
printf("Send Get Respond OK\r\n");
}
else
{
// Send html to client
iResult = send(ClientSocket, Respond_ICO, strlen(Respond_ICO), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed with error: %d\r\n", WSAGetLastError());
}
printf("Send ICO Respond OK\r\n");
}
}
else if(*(TokenPtr + 5) == 'F') //OFF
{
// Send html to client
iResult = send(ClientSocket, RespondLED_OFF, strlen( RespondLED_OFF), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed with error: %d\r\n", WSAGetLastError());
}
printf("Send LED_OFF OK\r\n");
}
else
{
// Send html to client
iResult = send(ClientSocket, RespondLED_ON, strlen( RespondLED_ON), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed with error: %d\r\n", WSAGetLastError());
}
printf("Send LED_ON OK\r\n");
}
//Wait 1S to allow boswer to receive information
Sleep(1000);
// shutdown the connection since no more data will be sent
closesocket(ClientSocket);
//WSACleanup();
//printf("WebServer closed! Press Enter to exit\r\n");
//getchar();
//getchar();
}
}