/* * Copyright (c) 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include main(int, char *argv[]) { SockBuf s(argv[1]); Signal sigint(SIGINT); atSignal(sigint) { cerr << "interrupted" << endl; return 1; } int connection_cnt = 0; while(1) { cout << "accepting connections" << endl; SockIOStream io(s.accept()); connection_cnt++; cout << "connection " << connection_cnt << " opened" << endl; Proc pc; // if(s.capabilities() & Socket::multiplex) // pc.run(); if(! pc.parent()) // i.e. if child or not run at all! { // s.close(Socket::silently); double x = 0; double y = 0; double z; char op = '+'; int req_cnt = 0; #ifdef EXCEPTIONS try { #endif while(true) { req_cnt++; cout << "request " << connection_cnt << "-" << req_cnt << ": " << flush; char *req; io.gets(&req, -1) >> endm >> endt; if(req == NULL || req[0] == '\0') break; istrstream reqs(req); reqs.unsetf(ios::skipws); reqs >> x >> ws >> op >> ws >> y; if(! reqs) { cout << "syntax error: `" << req << "'" << endl; io << "syntax error!" << endm << endt; continue; } cout << x << " " << op << " " << y << flush; switch(op) { case '+': z = x + y; break; case '-': z = x - y; break; case '*': z = x * y; break; case '/': z = x / y; break; default: cout << " -> syntax error!" << endl; io << "syntax error!" << endm << endt; continue; } cout << " = " << z << endl; io << z << endm << endt; } #ifdef EXCEPTIONS } catch(char *x) { cerr << "exception: " << x << endl; } #endif cout << "connection " << connection_cnt << " closed" << endl; if(pc.child()) // i.e. we are a child process return 0; } } }