client.c

来自「用c++包装好的线程库,直接拿来使用,提高效率.」· C语言 代码 · 共 72 行

C
72
字号
#include <threads/io.h>#include <threads/buffer.h>#include <threads/signal_num.h>#include <iostream>extern "C" {# 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 : public PthreadIO {protected:  void close(cpp_threads::Socket& fd_p)  {    cout << "Connection closed by peer" << endl;    PthreadIO::close(fd_p);    /* Doing an exit here, will not terminate the application       as we are actually being called as the server process. */    Pthread::_main()->signal(s_terminate);  }  void got(cpp_threads::SocketBuffer& p_fd)    {      while( !p_fd.inputBuf().empty() )	cout << p_fd.inputBuf().getline() << endl;    }public:  client() : PthreadIO("localhost",80) { };  ~client() { close(fd()); };};intmain(){  char buf[80];  PthreadIO *iop;  cout << "This is an example client program, written in threads C++" << endl;  cout << "Enter text, at the '?' prompt, and press enter.  Enter '.'" << endl;  cout << "to close the connection, and end the program." << endl << endl;  cout << "Remember, no bad language." << endl;  iop = new client;  do {    usleep(10);    cout << "?";    cout.flush();    cin.getline(buf,80);    if( cin ) {      if( buf[0] != '.' && buf[0] != '\0' )	iop->put( buf );    } else       cin.clear();  } while( buf[0] != '.' );  if ( !cin )    cout << "Input error: " << cin.rdstate() << endl;  delete iop;  usleep(10);}

⌨️ 快捷键说明

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