rendered paste body#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <iostream>
#include <fstream>
int help ()
{
csock << "Kommando help eingegeben\nIm Moment implementierte Kommandos: help, test, quit\n";
}
int test ()
{
// csock <<"Komando test eingegeben\n";
}
int news()
{
std::ifstream n;
std::string nout;
n.open("news.dat", std::ios::in);
getline(n, nout);
// csock << nout;
}
int main ( int argc, int argv[] )
{
unsigned int child[10];
unsigned int i = 0;
std::cout << "running....\n";
try
{
// Create the socket
ServerSocket server ( 30000 );
while(1)
{
ServerSocket csock;
server.accept ( csock );
child[i] = fork();
if (child[i] == 0)
{
try
{
std::string data;
std::string command;
unsigned int loc;
//---------------------------------------------------------
csock << "Wilkommen zu meinem Kommandoprotal.\n Tipp help.\n";
while(1)
{
startwhile:
csock <<">";
csock >> data;
std::cout << "data: " << data << std::endl;
if (data.empty())
{
std::cout <<"kein Komando eingegeben\n";
goto startwhile;
}
loc = data.find_first_of(" \r", 0);
if (loc == std::string::npos) command = data;
else command = data.substr(0, loc);
std::cout << "command: " << command << std::endl;
if (command == "test")
{
test();
}
else if ( command == "help")
{
help();
}
else if ( command == "news")
{
news();
}
else if (command == "quit")
{
csock << "bye\n";
break;
}
else csock << "Kommando nicht erkannt\n";
}
std::cout << "aus whileschleife raus\n";
//---------------------------------------------------------
}
catch ( SocketException& ) {}
}
else if(child[i] == -1) std::cout << "Fehler beim child " << i << std::endl;
else
{
std::cout << "Verbindung " << i++ << " aufgebaut\n";
}
}
}
catch ( SocketException& e )
{
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return 0;
}