home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / samples / sample9 / server.cpp < prev    next >
Text File  |  1998-03-10  |  2KB  |  104 lines

  1. #define INCL_OOL_PROCESS
  2. #include "ool.h"
  3.  
  4. #include <process.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. XString semName;
  9.  
  10. //we create a thread
  11. class MyServer: public XThread
  12. {
  13.       XNamedPipeServer server;
  14.       int number;
  15.    public:
  16.       MyServer(int nr);
  17.       void Start( );
  18.       virtual void Init(void);
  19. };
  20.  
  21.  
  22. //run the thread
  23. MyServer :: MyServer(int n)
  24. {
  25.    number = n;
  26.    Run();
  27. }
  28.  
  29.  
  30. //method is called automaticaly from the thread
  31. void MyServer::Init(void)
  32. {
  33.    LONG res;
  34.    if( (res = server.Open( "Sample9Pipe", XNPIPE_ACCESS_DUPLEX, XNPIPE_WAIT|XNPIPE_TYPE_BYTE|XNPIPE_READMODE_BYTE, 2 )) != 0)
  35.    {
  36.       printf( "cannot open server: %i", res);
  37.       exit(0);
  38.    }
  39.  
  40.    //now enter the loop for message-handling
  41.    Start();
  42. }
  43.  
  44.  
  45. //start this server
  46. void MyServer :: Start( )
  47. {
  48.    printf("server %i started, id = %i\n", number, number * 10);
  49.    LONG res;
  50.  
  51.    //wait for a connect
  52.    server.Connect();
  53.  
  54.    while(1)
  55.    {
  56.       //client is killed?
  57.       if( server.GetState() == XNPIPE_STATE_CLOSING)
  58.       {
  59.          //clear connection
  60.          server.DisConnect();
  61.          //wait for the next client
  62.          if( (res = server.Connect()) != 0)
  63.          {
  64.             printf( "cannot connect: %i id=%i", res, number*10);
  65.             exit(0);
  66.          }
  67.       }
  68.  
  69.       //we have a client
  70.       if( server.GetState() == XNPIPE_STATE_CONNECTED)
  71.       {
  72.          //write some data
  73.          LONG i;
  74.          for(i=0; i < 10000; i++)
  75.             server.Write( i + number*10);
  76.          server.ResetBuffer();
  77.       }
  78.    }
  79. }
  80.  
  81.  
  82. void main ( void)
  83. {
  84.    LONG res;
  85.    printf( "press Ctrl+C to stop the server\n");
  86.  
  87.    //in this case we start two servers
  88.    try
  89.    {
  90.  
  91.       MyServer * server1 = new MyServer(1);
  92.       MyServer * server2 = new MyServer(2);
  93.  
  94.       //wait until the user kills this process
  95.       //Sleep(0) gives control to other threads/processes
  96.       while(1)
  97.          XProcess::Sleep(0);
  98.    }
  99.    catch( XException& e)
  100.    {
  101.       e.PrintError();
  102.    }
  103. }
  104.