Hullo. I made a small webserver just to flex my winsock muscle, but it only works for small files like html files.
Once I have processed the request to get the path of the file, I send the result like this:
ifstream fin;
fin.open(URL, ios_base::binary | ios_base::in);
if (!fin.good() || fin.eof() || !fin.is_open())
{
fin.open("404.html", ios_base::binary | ios_base::in);
}
fin.seekg(0, ios::end);
unsigned int lenFile = (unsigned int)fin.tellg();
char *webpage = new char[lenFile];
fin.seekg(0, ios::beg);
fin.read(webpage, lenFile);
wsabuffer.buf = webpage;
wsabuffer.len = lenFile;
WSASend(socket, &wsabuffer, 1, NULL, flags, &wsaOverlapped, SendComplete);
SleepEx(INFINITE, TRUE);
fin.close();
closesocket(socket);
Like I said, it works with small files like the html files, but when I try to send a larger file like "http://127.0.0.1/gimmemore.mp3" it just freezes.
I previously made a direct-connection 2way file transfer program, in which I created a worker thread that looped, sending 8192 byte chunks until the socket blocked, and then pause to wait for an FD_WRITE event before the loop continued. This didn't work for the web server though:confused:
For the web server, I have overlapped sockets set up, whereas the 2way app was only using the regular send(). So in the web server, I tried this:
int bytesSent = 0;
int chunk;
while(bytesSent <= lenFile)
{
if(lenFile - bytesSent >= CHUNKSIZE)
chunk = CHUNKSIZE;
else
chunk = lenFile - bytesSent;
wsabuffer.buf = buffer;
wsabuffer.len = chunk;
fin.read(buffer, chunk);
WSASend(socket, &wsabuffer, 1, NULL, flags, &wsaOverlapped, SendComplete);
SleepEx(INFINITE, TRUE); //wait for send to complete before next chunk is attempted
bytesSent += chunk;
}
but it hung too:'(
I let it sit for a while once, and when I finally hit stop on the browser like 2 mins later, I had about 20 seconds of the song sitting there.
can anyone help me out? part of the problem is that I don't know how a browser would expect to receive something like this