NetClass

홈페이지: http://netclass.sourceforge.net

NetClass – a multi-platform C++ library which provides a thin wrapper for socket and thread support. This library is available under the terms of the GNU Public License. Currently, there is support for GNU/Linux and Win32 platforms. Also included in the library is the basic architecture to quickly create a simple generic server.

멀티플랫폼 소켓/ 쓰레드 랩퍼 라이브러리이다.

다운: 0.3.0

예제

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include "netclass.h"

//#define TEST_MODE_UDP
#define TEST_MODE_TCPIP


/* globals */
const int NUMBYTES = 512;
char buf[NUMBYTES] = {0};
char str[NUMBYTES] = {0};

int processRetVal(RetCode ret)
{
   switch(ret)
   {
   case NC_FAILED:
      cout << "The operation failed." << endl;
      return 0;
   case NC_TIMEDOUT:
      cout << "The operation has timed out." << endl;
      return 0;
   case NC_MOREDATA:
      cout << "The operation has returned that more data is available." << endl;
      return 0;
   }
   return 1;
}

ncCallBackRetType printstuff(void *ptr)
{
   cout << "Function callback for client connection called." << endl;

#ifdef TEST_MODE_UDP
   udpData *udpdata = ((udpData *)ptr);
   if (udpdata)
   {
      cout << udpdata->getData() << endl;
      delete udpdata;
   }
#else
   ncSocket *client = ((ncSocket *)ptr);
   
   if (client)
   {
      memset(buf,0,NUMBYTES);
      int numActualBytes = 0;

      /* read incoming bytes, leaving a null-terminator */
      switch(client->readData(buf,NUMBYTES-1,&numActualBytes))
      {
      case NC_OK:
         cout << numActualBytes << " bytes read." << endl;
         cout << buf << endl;
         break;
      case NC_FAILED:
         cout << "printstuff readData failed." << endl;
         break;
      case NC_MOREDATA:
         cout << "NC_MOREDATA | READ ONLY RETURNED " << 
            numActualBytes << " BYTES." << endl;
         break;
      }
      cout << "Deleting client" << endl;
      delete client;
   }
#endif

   return (ncCallBackRetType)0;
}

ncCallBackRetType threadfunc1(void *ptr)
{
#ifdef TEST_MODE_UDP
   ncSocketListener ncsl(1234,SOCKTYPE_UDP);
#else
   ncSocketListener ncsl(1234,SOCKTYPE_TCPIP);
#endif

   /*
     Start the non threaded server;
     printstuff will be called with each new incoming connection.
   */
   cout << "Non-threaded Server starting" << endl;
   ncsl.startListening(printstuff,NC_NONTHREADED);

   /* OR Start the threaded server */
   //cout << "Threaded Server starting" << endl;
   //ncsl.startListening(printstuff,NC_THREADED);
   return (ncCallBackRetType)0;
}

ncCallBackRetType threadfunc2(void *ptr)
{
   static ncSocket sock;
   ncThread *thread1 = (ncThread *)ptr;
   int bytesWritten = 0;

   ncSleep(1000);
   
   /* fill (null-terminated) string with repeating upper case letters */
   for(int i = 0; i < 511; i++)
   {
      str[i] = (char)((i % 26)+65);
   }

   while(1)
   {
      bytesWritten = 0;

      /* attempt the connect */
#ifdef TEST_MODE_UDP
      if (!processRetVal(sock.connect("localhost",1234,SOCKTYPE_UDP)))
#else
      if (!processRetVal(sock.connect("localhost",1234,SOCKTYPE_TCPIP)))
#endif
      {
         cout << "Connect failed" << endl;
         goto cleanup;
      }
      else
      {
         cout << "Connected." << endl;
      }
      
      /* write the string data to the server (w/o null terminator) */
      if (!processRetVal(sock.writeData((char *)str,511,&bytesWritten)))
      {
         cout << "write data failed" << endl;
         break;
      }
      else
      {
         cout << "Bytes written = " << bytesWritten << endl;
      }

      sock.close();
      ncSleep(1000);
   }

  cleanup:
   if (thread1)
   {
      cout << "Cancelling thread 1" << endl;
      thread1->stop(1);
   }
   cout << "Thread 2 exiting" << endl;
   return (ncCallBackRetType)0;
}


int main(int argc,char **argv)
{
   netClass nc;
   ncThread thread1;
   ncThread thread2;

   /* only required under win32 (initializes winsock) */
   nc.initialize();

   if (thread1.start(threadfunc1,(void *)0) == NC_FAILED)
   {
      cout << "Thread 1 creation failed." << endl;
   }
   if (thread2.start(threadfunc2,(void *)&thread1) == NC_FAILED)
   {
      cout << "Thread 2 creation failed." << endl;
   }
   if (thread1.detach() == NC_FAILED)
   {
      cout << "Thread 1 join failed." << endl;
   }
   if (thread2.join() == NC_FAILED)
   {
      cout << "Thread 2 join failed." << endl;
   }
   return 0;
}

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다


This site uses Akismet to reduce spam. Learn how your comment data is processed.