int main(void)
{
    unsigned char buffer[1024]; // buffer per ricezione
    unsigned long ip_address;
    unsigned short port_number;
    unsigned int num; // variabile per numero da generare
    int n;
    struct in_addr add;
    char *ip_address_string;

// inizializzazione socket con porta UDP numero 23365
    if (UDP_init(23365) < 0)
    {
        printf("Errore inizializzazione socket!\r\n");
        return -1;
    }
    printf("Servizio attivo…\r\n");
    while (1)
    {
        if ((n = UDP_receive( &ip_address, &port_number, buffer, sizeof(buffer))) > 0)
        {
            // ricezione di un datagram e verifica del messaggio
            buffer[n] = '\0'; // terminatore di stringa
            if (strcmp((char*)buffer, "REQ") == 0)
            {
                // richiesta di generazione di un nuovo numero
                num = sequence();
                UDP_send(ip_address, port_number, (void*)&num, sizeof(unsigned int));
                printf("…inviato numero %u.\r\n", num);
                //soluzione

                add.s_addr=htonl( ip_address );

                ip_address_string=inet_ntoa(add);
                printf("... all'indirizzo %s \r\n", ip_address_string);
            }
        }
    }
    UDP_close();
    return 0;
}
Beispiel #2
0
void test01(WORD testNo, char *testName, BYTE tcpNotUdp, DWORD *blockSizes, WORD blockSizesCount)
{
    DWORD start = getTicks();

    //----------
    out_test_header(testNo, testName);          // show test header
    
    //----------
    // find out the largest block size
    int i;
    int maxBlockSize = 0;

    for(i=0; i<blockSizesCount; i++) {
        if(maxBlockSize < (int)blockSizes[i]) {      // if current max block size is smaller than this block size, store it
            maxBlockSize = blockSizes[i];
        }
    }
    //----------
    // open socket
    int handle;
    
    if(tcpNotUdp) {
        handle = TCP_open(SERVER_ADDR, SERVER_PORT_START, 0, maxBlockSize);
    } else {
        handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    }
    
    if(handle < 0) {
        out_result_string(0, "TCP/UDP open() failed");
        return;
    }

    //----------
    // if TCP (not UDP), wait for connected state
    if(tcpNotUdp) {
        // wait until connected
        int res;

        while(1) {
            res = TCP_wait_state(handle, TESTABLISH, 1);
        
            if(res == E_NORMAL) {
                break;
            }

            DWORD now = getTicks();
            if((now - start) > 5*200) {
                out_result_string(0, "TCP_wait_state() timeout");
                goto test01close;
            }
        }
        
        if(res != E_NORMAL) {
            out_result_error_string(0, res, "TCP_wait_state() failed");
            goto test01close;
        }
    }
    
    //---------------------
    int res;
    
    for(i=0; i<blockSizesCount; i++) {
        res = sendAndReceive(tcpNotUdp, blockSizes[i], handle, 1);
    
        if(!res) {                              // if single block-send-and-receive operation failed, quit and close
            goto test01close;
        }
    }
    
    //---------------------
    
    out_result(1);                              // success!
    
test01close:
    if(tcpNotUdp) {
        res = TCP_close(handle, 0, 0);          // close
    } else {
        res = UDP_close(handle);                // close
    }
    
    if(res != E_NORMAL) {
        out_result_error_string(0, res, "TCP/UDP close() failed");
    }
}
Beispiel #3
0
void test0130(void)
{
    int handle, res, ok;
    int a,b,c;
    
    out_test_header(0x0130, "UDP - CNbyte_count() on 3 datagrams");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 3 x 100 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      
        
        out_result_error(ok, res);
        //-----------------------------------
        out_test_header(0x0131, "UDP - CNget_block() on 3 datagrams");
        
                                            // receive it using 3x CNget_block()
        a = CNget_block (handle, rBuf, 100);
        b = CNget_block (handle, rBuf, 100);
        c = CNget_block (handle, rBuf, 100);
        
        ok = (a == 100 && b == 100 && c == 100) ? 1 : 0;
        out_result_error(ok, res);
        //-----------------------------------
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }
    
    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0132, "UDP - CNbyte_count() after partial read");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 300);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            a = CNget_block (handle, rBuf, 100);
            b = CNbyte_count(handle);
            
            ok = (a == 100 && b == 200) ? 1 : 0;
            out_result_error(ok, b);
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }    
    
    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0133, "UDP - get 3 DGRAMs with 1 CNget_block");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            a = CNget_block (handle, rBuf, 300);
            b = CNbyte_count(handle);
            
            ok = (a == 300 && b == 0) ? 1 : 0;
            out_result_error(ok, b);
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }        
    
    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0134, "UDP - get 3 DGRAMs with 3x CNget_NDB");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            NDB *m,*n,*o;
            
            m = CNget_NDB(handle);
            n = CNget_NDB(handle);
            o = CNget_NDB(handle);
            
            ok = (m != NULL && n != NULL && o != NULL) ? 1 : 0;
            
            if(!ok) {
                out_result_string(ok, "some CNget_NDB failed");
            } else {
                ok = (m->len == 100 && n->len == 100 && o->len == 100) ? 1 : 0;
                
                if(!ok) {
                    out_result_string(ok, "length of NDB block wrong");
                } else {
                    out_result(1);
                }
            }
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }        

    //////////////////////////////////////////////////////////////////////////////
    
    out_test_header(0x0135, "UDP - get 3 DGRAMs with CNget_char");
    handle = UDP_open(SERVER_ADDR, SERVER_PORT_START + 4);
    if(handle >= 0) {
        // send 300 bytes
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        (void) UDP_send(handle, wBuf, 100);
        
        sleep(1);
        
        res = CNbyte_count(handle);         // see how many bytes we got for reading on this socket
        ok = (res == 300) ? 1 : 0;      

        b = 1;                              // good for now
        if(!ok) {                           // if not enough data, fail
            out_result_error_string(ok, res, "not enough data");
        } else {
            int i;
            for(i=0; i<300; i++) {
                a = CNget_char(handle);
                
                if(a >= 0) {
                    rBuf[i] = a;
                } else {
                    out_result_error_string(0, a, "error on CNget_char");
                    b = 0;                  // failed
                    break;
                }
            }
        
            if(b) {                         // if good, check data
                a = memcmp(rBuf,       wBuf, 100);
                b = memcmp(rBuf + 100, wBuf, 100);
                c = memcmp(rBuf + 200, wBuf, 100);
                
                ok = (a == 0 && b == 0 && c == 0) ? 1 : 0;
                out_result(ok);
            }
         }
        
        UDP_close(handle);
    } else {
        out_result_string(0, "UDP_open failed");
    }
}
Beispiel #4
0
void  do_some_work()

{
   NDB     *ndb;
   time_t  timeout;
   int     handle, message[2], response, ready;

   if ((handle = UDP_open (0, IP_DIALER_PORT)) < 0) {
        form_alert (1, no_udp);
        return;
      }

   for (;;) {

        timeout = time (NULL) + TIMEOUT;

        do {
             evnt_timer (200, 0);

             if ((ndb = CNget_NDB (handle)) != NULL) {
                  switch (* (int16 *) ndb->ndata) {
                     case IP_DIAL_REQUEST :
                       sprintf (alert, query, ndb->ndata + 4, * (int16 *) (ndb->ndata + 2));
                       if (form_alert (1, alert) == 1)
                            message[0] = IP_DIAL_DONE,  message[1] = 0;
                         else {
                            message[0] = IP_DIAL_ERROR;
                            response = 0;
                            do {
                                 sprintf (alert, number, response);
                                 ready = 0;
                                 switch (form_alert (1, alert)) {
                                    case 1 :   response --;   break;
                                    case 2 :   ready = 1;     break;
                                    case 3 :   response ++;   break;
                                    }
                              } while (! ready);
                            message[1] = response;
                          }
                       break;
                     case IP_DIAL_HANGUP :
                       form_alert (1, hangup);
                       message[0] = IP_DIAL_DONE;
                       message[1] = 0;
                       break;
                     }
                  KRfree (ndb->ptr);
                  KRfree (ndb);
                  UDP_send (handle, (char *) message, 4);
                  UDP_close (handle);

                  if ((handle = UDP_open (0, IP_DIALER_PORT)) < 0) {
                       form_alert (1, no_udp);
                       return;
                     }
                }
          } while (time (NULL) < timeout);

        if (form_alert (1, proceed) != 1)   break;
      }

   UDP_close (handle);
 }