home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 83win / data1.cab / DLL_Toolkit / Source / HTBTcpip / comdll.cpp next >
Encoding:
C/C++ Source or Header  |  2001-03-02  |  7.6 KB  |  367 lines

  1. /*****************************************
  2. HTBtcpip.dll
  3.  
  4. Copyright TransEra Corporation 1999
  5.  
  6. comdll.cpp
  7. ****************************************/
  8.  
  9. #include "stdafx.h"
  10. #include "comdll.h"
  11. #include "DialogThread.h"
  12. #include "export.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. BEGIN_MESSAGE_MAP(CComdllApp, CWinApp)
  21. END_MESSAGE_MAP()
  22.  
  23. CComdllApp::CComdllApp() 
  24. {
  25. }
  26.  
  27. //************************
  28. // global variables
  29.  
  30. CComdllApp theApp;
  31.  
  32. short g_SendPort = DEFAULT_PORT;
  33. short g_ReceivePort = DEFAULT_PORT;
  34. void *g_BasicData;
  35. short g_Option;
  36. short g_Signal;
  37.  
  38. BOOL g_ShutDownRequest = FALSE;
  39. BOOL g_IsThreadRunning = FALSE;
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. /*
  44.     Function:        CComdllApp::InitInstance
  45.  
  46.     Description:    Initialize at DLL startup
  47.  
  48.     Return type:    BOOL 
  49.  
  50.     Notes:            Initialize at DLL Startup
  51.         
  52. */
  53. BOOL CComdllApp::InitInstance() 
  54. {    
  55.     if (!AfxSocketInit()) 
  56.     {
  57.         AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
  58.         return FALSE;
  59.     }
  60.     return TRUE;
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. /*
  65.     Function:        ListenThread
  66.  
  67.     Description:    listen for packets and handle them    
  68.  
  69.     Return type:    int 
  70.  
  71.     Notes:            listen for packets and handle them    
  72.         
  73. */
  74. int ListenThread() 
  75. {
  76.     char Buffer[BUFFERSIZE] = {NULL}; 
  77.     char *face = NULL; 
  78.     int reg = 0;
  79.     int RetVal; 
  80.     int FromLen; 
  81.     int SocketType = 0; 
  82.     struct sockaddr_in local;
  83.     struct sockaddr_in from; 
  84.     WSADATA wsaData; 
  85.     SOCKET ListenSocket;
  86.     SOCKET Msgsock; 
  87.  
  88.  
  89.     while(TRUE) 
  90.     {    
  91.         
  92.         if (WSAStartup(OFFSET, &wsaData) == SOCKET_ERROR) 
  93.         {    
  94.             AfxMessageBox("WSAStartup failed!");        
  95.             WSACleanup();
  96.             return -1;
  97.         }  
  98.  
  99.         local.sin_family = AF_INET;
  100.         local.sin_addr.s_addr = (!face)?INADDR_ANY:inet_addr(face);        
  101.         local.sin_port = htons(g_ReceivePort);
  102.     
  103.         ListenSocket = socket(AF_INET, SocketType,0); // TCP socket
  104.     
  105.         if (ListenSocket == INVALID_SOCKET)  
  106.         {
  107.             AfxMessageBox("Socket() function call failed"); 
  108.             WSACleanup(); 
  109.             return -1; 
  110.         } 
  111.     
  112.         if (bind(ListenSocket,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR) 
  113.         {                                        
  114.             WSACleanup();
  115.             return -1;         
  116.         }  
  117.         
  118.         if (listen(ListenSocket, L_TIMEOUT) == SOCKET_ERROR) 
  119.         {
  120.             AfxMessageBox("Either listen() or bind() function calls failed"); 
  121.             WSACleanup(); 
  122.             return -1; 
  123.         } 
  124.  
  125.         FromLen = sizeof(from); 
  126.                     
  127.         if (SocketType != SOCK_DGRAM) 
  128.         {
  129.             Msgsock = accept(ListenSocket,(struct sockaddr*)&from, &FromLen); 
  130.             
  131.             if (Msgsock == INVALID_SOCKET)  
  132.             {
  133.                 AfxMessageBox("accept() function call failed");     
  134.                 WSACleanup(); 
  135.                 return -1; 
  136.             } 
  137.         }
  138.         else 
  139.         {    
  140.             Msgsock = ListenSocket;        
  141.         }
  142.  
  143.         strset(Buffer, '\0');
  144.         RetVal = recv(Msgsock,Buffer, sizeof(Buffer),0 ); 
  145.         
  146.         if (RetVal == SOCKET_ERROR) 
  147.         {    
  148.             AfxMessageBox("recv() function call failed");
  149.         }
  150.  
  151.         if (RetVal == 0) 
  152.         {    
  153.             closesocket(Msgsock);
  154.         } 
  155.  
  156.         closesocket(Msgsock);
  157.         closesocket(ListenSocket);        
  158.  
  159.         switch (g_Option) 
  160.         {    
  161.         
  162.         case 0: 
  163.             {                
  164.             short *pStringSize = NULL;                
  165.                 strcpy((char *)g_BasicData, Buffer);    // write buffer to basic memory        
  166.                 pStringSize = (short *)(((char *)g_BasicData) - 2);    // find pointer to string size
  167.                 *pStringSize = strlen(Buffer);            // update string size in basic                
  168.                 break;
  169.             }
  170.  
  171.         case 1:  
  172.             {
  173.                 PutBuffer(g_BasicData, Buffer, 1);        // place in basic buffer                
  174.                 break;
  175.             }
  176.                 
  177.         default:  
  178.             {
  179.                 AfxMessageBox("Invalid option.");            
  180.                 break;
  181.             }
  182.         }
  183.         
  184.         if (g_Signal >= 0 && g_Signal <= BSC_SIG_IT) 
  185.         {            // if valid signal number requested
  186.             Signal(g_Signal);                            // signal basic
  187.         }
  188.  
  189.         WSACleanup();    
  190.         
  191.         if (g_ShutDownRequest) 
  192.         {
  193.             return 0;
  194.         }    
  195.     }        
  196. }
  197.  
  198. /////////////////////////////////////////////////////////////////////////////
  199. /*
  200.     Function:        Senddata
  201.  
  202.     Description:    send data to an IP address        
  203.  
  204.     Return type:    void 
  205.     Argument:        char * ip
  206.     Argument:        char *DataString
  207.  
  208.     Notes:            send data to an IP address
  209.         
  210. */
  211. void Senddata(char * ip, char *DataString) 
  212. {
  213.     struct hostent * pHost;
  214.                                 
  215.     int nSocketType = SOCK_STREAM;                        
  216.     LPCTSTR lpszSocketAddress  = NULL;                    //host IP Address    
  217.     LPCTSTR lpszHostAddress = NULL;                     //destination IP address
  218.     CString lpBuf;                                        //Message of broadcast string
  219.     CString str;
  220.     CString addr;
  221.     char szHostName[HOSTNAMESIZE];
  222.     int nBuflen; 
  223.     int lasterr;
  224.     int i;
  225.     int j;
  226.     int x = 0;
  227.     int nFlags = MSG_OOB;        
  228.  
  229.  
  230.     if (gethostname(szHostName, HOSTNAMESIZE) == 0 ) 
  231.     {
  232.         pHost = gethostbyname(szHostName);
  233.  
  234.         for (i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 
  235.         {
  236.             for(j = 0; j < pHost->h_length; j++ )  
  237.             {
  238.                 if( j > 0 )  {
  239.                     str += ".";
  240.                 }
  241.                 addr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
  242.                 str += addr;
  243.             }                
  244.         }
  245.     }
  246.  
  247.     lpszHostAddress = ip;
  248.     CSocket Big;
  249.     lpBuf = DataString;
  250.     lpszSocketAddress = str;
  251.     nBuflen = strlen(lpBuf) + 1;
  252.     if (nBuflen < PCKT_MIN_SZ) {
  253.         AfxMessageBox("Attemptng to send a string of one character\n may or may not work");
  254.     }
  255.     Big.Create(g_SendPort, nSocketType, lpszSocketAddress);    
  256.     Big.Connect(lpszHostAddress, g_SendPort);    
  257.     Big.Send(lpBuf, nBuflen, nFlags);
  258.     lasterr = Big.GetLastError();    
  259.     Big.Close();
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. /*
  264.     Function:        Startserver
  265.  
  266.     Description:    begin thread to listen
  267.  
  268.     Return type:    void 
  269.     Argument:        void *BasicData
  270.     Argument:        short Option
  271.     Argument:        short Signal
  272.  
  273.     Notes:            begin thread to listen
  274.         
  275. */
  276. void Startserver(void *BasicData, short Option, short Signal)  
  277. {
  278.     if (g_IsThreadRunning) 
  279.     {
  280.         return;
  281.     }
  282.  
  283.     g_IsThreadRunning = TRUE;
  284.     g_ShutDownRequest = FALSE;
  285.     g_BasicData = BasicData;
  286.     g_Option = Option;
  287.     g_Signal = Signal;
  288.     CWinThread * pThread = AfxBeginThread(RUNTIME_CLASS (DialogThread));    // create thread for Recieveing TCP packets
  289. }
  290.  
  291. /////////////////////////////////////////////////////////////////////////////
  292. /*
  293.     Function:        Getaddress
  294.  
  295.     Description:    gets IP address from local machine and returns as a char pointer    
  296.  
  297.     Return type:    char * 
  298.     Argument:        char *LocalAddress
  299.  
  300.     Notes:            gets IP address from local machine and returns as a char pointer
  301.         
  302. */
  303. char * Getaddress(char *LocalAddress) 
  304. {
  305.  
  306. CString LocalStr, LocalAddr;
  307. int i, j;
  308. char szHostName[HOSTNAMESIZE];
  309.  
  310. if( gethostname(szHostName, HOSTNAMESIZE) == 0 ) 
  311. {
  312.         
  313.     struct hostent * pHost;       
  314.     pHost = gethostbyname(szHostName);
  315.  
  316.         for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 
  317.         {
  318.                 
  319.             for( j = 0; j < pHost->h_length; j++ ) {
  320.                          
  321.                 if( j > 0 )
  322.                     LocalStr += ".";
  323.                     LocalAddr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
  324.                     LocalStr += LocalAddr;
  325.             }
  326.         }
  327.         strcpy(LocalAddress, LocalStr);        
  328.     }
  329.     return LocalAddress;
  330. }
  331.  
  332. /////////////////////////////////////////////////////////////////////////////
  333. /*
  334.     Function:        Stopserver
  335.  
  336.     Description:    shut down server thread
  337.  
  338.     Return type:    void 
  339.  
  340.     Notes:            shut down server thread
  341.         
  342. */
  343. void Stopserver() 
  344. {
  345.     g_ShutDownRequest = TRUE;
  346. }
  347.  
  348. /////////////////////////////////////////////////////////////////////////////
  349. /*
  350.     Function:        Setports
  351.  
  352.     Description:     set port numbers to use
  353.  
  354.     Return type:    void 
  355.     Argument:        short SendPort
  356.     Argument:        short ReceivePort
  357.  
  358.     Notes:             set port numbers to use
  359.         
  360. */
  361. void Setports(short SendPort, short ReceivePort) 
  362. {
  363.     g_SendPort = SendPort;
  364.     g_ReceivePort = ReceivePort;
  365. }
  366.  
  367.