#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sender.h>

int main(int argc, char* argv[])
{

if (argc < 2)

{

cout << "usage: sendfile filename" << endl;

return 0;

}

CSabulSender* sender = new CSabulSender;

int port;
try
{

if (argc > 2)

port = sender->open(atoi(argv[2]));

else

port = sender->open();

}
catch(...)
{

cout << "Failed to Open New Sabul Server. Program Aborted." << endl;
return 0;

}
cout << "sabul is ready at port: " << port << endl;

try
{

sender->listen();

}
catch(...)
{

cout << "Failed to Open New Sabul Server. Program Aborted." << endl;
return 0;

}

int fd = open(argv[1], O_RDONLY);
struct stat fs;
stat(argv[1], &fs);

//
// This is the only difference with appserver.cpp
//

try
{

sender->sendfile(fd, 0, fs.st_size);

}
catch (...)
{

cout << "connection broken ...\n";
return 0;

}

while (sender->getCurrBufSize())

usleep(10);

sender->close();

return 1;

}