I have a problem with my code.
I am trying to write multithread server HTTP on windows. I do not know what I am doing wrong when I started the server and trying to run in browser using my IP, the browser does not show the image which I would like to show.
Please help me.
Here is the entire code
(change your destination where you have some images)
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <fstream>
#ifdef UNICODE
std::wstring path;
#else
std::string path;
#endif
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define MAX_THREAD 5
#define true 1
char index[2000 * 600];
int page_size = 0;
void mainPage(char *index)
{
path = TEXT("d:\\img\\*");
WIN32_FIND_DATAA fileData;
HANDLE fileHandle;
fileHandle = FindFirstFileA("d:\\img\\*", &fileData);
char img_name[255];
WIN32_FIND_DATA data;
HANDLE find = INVALID_HANDLE_VALUE;
TCHAR szDir[MAX_PATH];
strcpy(index, "<html><head></head><body><center>");
find = FindFirstFile(path.c_str(), &data);
cout << "<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
while (FindNextFileA(fileHandle, &fileData) != 0)
{
if (!(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
cout << " w ifie " << endl;
sprintf(img_name, "<img src='img/%s'>", fileData.cFileName);
strncat(index, img_name, strlen(img_name));
}
}
strcat(index, "</center></body></html>");
page_size = strlen(index);
for(int i =0; i< strlen(index); i++)
{
cout << index[i];
}
cout << endl;
}
DWORD generateThread(LPVOID* arg)
{
char buffor[1024], sciezka[255], img2[10];
int i = 6, buffor2 = 1024, retval;
FILE *img = NULL;
fstream plik;
fpos_t rozmiar;
SOCKET connfd = (SOCKET) * arg;
retval = recv(connfd, buffor, sizeof(buffor), 0);
if (retval == 0)
fprintf(stdout, "Blad polaczenia\n");
for (i = 6; i < sizeof(buffor); i++)
{
if (buffor[i]==' ')
{
buffor[i] = 0;
break;
}
}
strcpy(sciezka, buffor + 5);
if ((sciezka[0] == 'i') && (sciezka[1] == 'm') && (sciezka[2] == 'g')) {
if (sciezka[strlen(sciezka) - 4]=='.')
{
strncpy(img2, sciezka + strlen(sciezka) - 3, 3);
img2[3] = 0;
}
else if (sciezka[strlen(sciezka) - 5]=='.')
{
strncpy(img2, sciezka + strlen(sciezka) - 4, 4);
img2[4] = 0;
}
string strPath = sciezka;
strPath = "d:/"+strPath;
cout << "sciezka 2 : " << strPath << endl;
img = fopen(strPath.c_str(), "rb");
FILE *iii = NULL;
if(img == NULL)
{
cout << "Blad czytania pliku" << endl;
}
else
{
cout << "UF<><><>" << endl;
fseek(img, 0, SEEK_END);
fgetpos(img, &rozmiar);
printf("\tWyslano plik: %s\n", sciezka);
strcpy(buffor, "HTTP/1.1 200 OK\n");
strcat(buffor, "Date: Sat, 24 Mar 2012 17:13:05 GMT\n");
strcat(buffor, "Content-Length: ");
sprintf(buffor + strlen(buffor), "%d\n", rozmiar);
strcat(buffor, "Connection: close\n");
strcat(buffor, "Content-Type: image/");
sprintf(buffor + strlen(buffor), "%s\n\n", img2);
retval = send(connfd, buffor, strlen(buffor), 0);
fseek(img, 0, SEEK_SET);
while(!feof(img))
{
rozmiar = fread(buffor, 10, buffor2, img);
retval = send(connfd, buffor, rozmiar, 0);
}
}
}
else
{
strcpy(buffor, "HTTP/1.1 200 OK\n");
strcat(buffor, "Date: Sat, 24 Mar 2012 17:13:05 GMT\n");
strcat(buffor, "Content-Length: ");
sprintf(buffor + strlen(buffor), "%d\n", page_size);
strcat(buffor, "Connection: close\n");
strcat(buffor, "Content-Type: text/html; charset=iso-8859-2\n\n");
retval = send(connfd, buffor, strlen(buffor), 0);
retval = send(connfd, index, page_size, 0);
}
}
int main(int argc, char** argv)
{
SOCKET gniazda[MAX_THREAD]; LPDWORD watki[MAX_THREAD], alive; HANDLE uchwyty[MAX_THREAD]; int i = 0, j = 0, size = 0, which = 0, retval = 0, client_addr_len = 0, server_addr_len = 0;
WSADATA wsaData;
SOCKET listenfd, connfd;
struct sockaddr_in server_addr, client_addr;
memset(&gniazda, NULL, sizeof(gniazda));
memset(&uchwyty, 0, sizeof(uchwyty));
memset(&watki, 0, sizeof(watki));
if (argc != 2)
{
fprintf(stderr, "Invocation: %s <port>\n", argv[0]);
system("pause");
exit(EXIT_FAILURE);
}
retval = WSAStartup(MAKEWORD(2,2), &wsaData);
if (retval != 0)
{
fprintf(stderr, "WSAStartup failed: %d\n", retval);
exit(EXIT_FAILURE);
}
listenfd = socket(PF_INET, SOCK_STREAM, 0);
if (listenfd == INVALID_SOCKET) {
fprintf(stderr, "socket() failed: %d\n", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
server_addr_len = sizeof(server_addr);
if (bind(listenfd, (struct sockaddr*) &server_addr, server_addr_len) == SOCKET_ERROR)
{
fprintf(stderr, "bind() failed: %d\n", WSAGetLastError());
closesocket(listenfd);
WSACleanup();
exit(EXIT_FAILURE);
}
if (listen(listenfd, SOMAXCONN) == SOCKET_ERROR)
{
fprintf(stderr, "listen() failed: %d\n", WSAGetLastError());
closesocket(listenfd);
WSACleanup();
exit(EXIT_FAILURE);
}
fprintf(stdout, "Oczekiwanie na polaczenie...\n");
client_addr_len = sizeof(client_addr);
mainPage(index);
while (true)
{
connfd = accept(listenfd, (struct sockaddr*)&client_addr, &client_addr_len);
if (connfd == INVALID_SOCKET)
{
fprintf(stderr, "accept() failed: %d\n", WSAGetLastError());
closesocket(listenfd);
WSACleanup();
exit(EXIT_FAILURE);
}
j = 0;
while (j < MAX_THREAD)
{
switch((int)uchwyty[j])
{
case 0:
which = j;
gniazda[which] = connfd;
j = MAX_THREAD;
break;
default:
alive = (LPDWORD)WaitForSingleObject(uchwyty[j], 0);
if (alive == 0)
{
which = j;
gniazda[which] = connfd;
j = MAX_THREAD;
}
}
j++;
}
fprintf(stdout, "Polaczenie z: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
uchwyty[which] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&generateThread, &gniazda[which], 0, (LPDWORD)&watki[which]);
}
fprintf(stdout, "Closing listening socket and terminating server...\n");
if (closesocket(listenfd) == SOCKET_ERROR)
fprintf(stderr, "closesocket() failed: %d\n", WSAGetLastError());
if (WSACleanup() == SOCKET_ERROR)
fprintf(stderr, "WSACleanup failed: %d\n", WSAGetLastError());
exit(EXIT_SUCCESS);
}
run appp_name.exe port