⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.c

📁 用c++包装好的线程库,直接拿来使用,提高效率.
💻 C
字号:
#include <thread.h>#include <threads/io.h>#include <threads/exception.h>#include <threads/buffer.h>#include <fstream>#include <iostream>extern "C" {# include <dirent.h># include <stdarg.h># include <fcntl.h># include <termios.h># include <unistd.h># include <sys/syslog.h># include <sys/types.h># include <sys/stat.h>};using namespace std;using namespace cpp_threads;class client_connection;struct client {  client_connection*   cc;  cpp_threads::Socket* sd;};class client_connection : public Pthread, public AsynchronousIO {protected:  Semaphore _go;  void got(SocketBuffer& sb_p)  {    string s;    s = sb_p.inputBuf().gets();    if( s == "ls" ) {      struct dirent **namelist;      int n;            s = sb_p.inputBuf().getline();      if( (n = scandir(s.c_str(),&namelist,0,alphasort)) < 0 )	sb_p.write("error");      else {	sb_p.write("\nDirectory listing\n");	for(int i=0;i<n;i++) {	  sb_p.write(string(namelist[i]->d_name)+"\n");	  free(namelist[i]);	}      }    } else if( s == "bye" || s == "exit" ) {      cout << "will close " << (int)sb_p << endl;      sb_p.close();    } else if( s == "type" ) {      s = sb_p.inputBuf().getline();      ifstream fs;      string t;      char ch;      fs.open(s.c_str());      if( fs ) {	t = "";	fs.unsetf( ios::skipws );	while( !fs.eof() ) {	  fs >> ch;	  t += ch;	  if( ch == '\n' ) {	    sb_p.write(t);	    t = "";	  }	}	fs.close();	if( t.length() )	  sb_p.write(t);      }    } else {      s = sb_p.inputBuf().getline();      sb_p.write("unknown command error\n");    }  }  void close(cpp_threads::Socket&)  {    stop();    delete this;  }public:  client_connection(cpp_threads::Socket& s_p)    : cpp_threads::AsynchronousIO(cpp_threads::AsynchronousIO::dupl_deliver_e)  {    cout << "client spawned for " << (int)s_p << endl;    _fd = (SocketBuffer&)s_p;  };  ~client_connection()   {    Pthread::debug("client_connection::~client_connection");  };  void stop()  {    _go.post();    // give the thread time to end, so we don't end up as    // zombies.    while( !terminated() )      usleep(5);  };  int thread(void *)  {    cout << "client connection: " << id() << endl;    if( getpid() == id() )      _go.wait();    return 0;  };};/** * This example, shows a server that can handle numerous * concurrent conections, but only uses one main program * to handle them all. * * Mechanism, to ensure that it only receives one socket * at a time, is handled by the thread_io thread, which * will queue requests, until each serve is finished and * then serve them as the application becomes available. */class server : public PthreadIO {protected:  int    _connected;protected:  void open(cpp_threads::Socket& fd)    {      client_connection* cc;      cc = new client_connection(fd);      registerFD(cc);      cc->run();    }  void close(cpp_threads::Socket& fd)    {      cout << (int)fd <<  ": disconnecting" << endl;      PthreadIO::close(fd);    }  void got(cpp_threads::SocketBuffer& p_fd)    {      string s = p_fd.inputBuf().getline();      p_fd.write("data is lost, retry\n");    }public:  server() : PthreadIO(80,1) { _connected = 0; };  ~server() { PthreadIO::close(fd()); };};intmain(){  PthreadIO *p;  cout << "This is a simple example, of a program using the" << endl;  cout << "thread_io class as a server socket handler." << endl;  cout << endl;  try {    p = new server();    p->join();  } catch( cpp_threads::exception t ) {    cout << t.message() << endl;  }  cout << "server stop." << endl;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -