[C/++] Calcolo dell'indirizzo di broadcast di una rete

« Older   Newer »
  Share  
CAT_IMG Posted on 30/12/2012, 16:39     +1   -1

So implementare gli object

Group:
Admin
Posts:
1,215
Reputazione:
+150

Status:


Questo è dedicato a logix, che a breve dovrà porsi il problema di implementarlo in Java :asd:

L'unico accenno di astrazione che c'è nel codice, è la union IPv4Address, e sinceramente l'ho fatto così solo per comodità, altrimenti l'avrei scritto in modo ancora più sporco :P
Del resto, non sapevo cosa fare...

<pastebin>

Guardatelo su Pastebin, Forumcommunity ha ucciso l'indentazione

CODICE
#define _CRT_SECURE_NO_WARNINGS // Perdonami, Windows :C

#include <stdio.h>
#include <string.h>

typedef unsigned char byte;

typedef union
{
       struct
       {
               byte b1, b2, b3, b4;
       } bytes;

       unsigned int asUInt;
} IPv4Address;

int main(int argc, char* argv[])
{
       IPv4Address IP, SubnetMask, BroadcastAddress;

       // Un po' di pulizia...
       IP.asUInt = SubnetMask.asUInt = BroadcastAddress.asUInt = 0;

       // Il formato di scanf sa tanto di ridicolo XD
       // Giusto per renderlo più leggibile:
       // hh = char, u = unsigned.
       printf("Inserisci un indirizzo IP nel formato a.b.c.d: ");
       scanf("%­hhu.%­hhu.%­hhu.%­hhu",        &(IP.bytes.b1),
                                       &(IP.bytes.b2),
                                       &(IP.bytes.b3),
                                       &(IP.bytes.b4)
                               );

       printf("Inserisci la maschera di sottorete nel formato a.b.c.d: ");
       scanf("%­hhu.%­hhu.%­hhu.%­hhu",        &(SubnetMask.bytes.b1),
                                       &(SubnetMask.bytes.b2),
                                       &(SubnetMask.bytes.b3),
                                       &(SubnetMask.bytes.b4)
                               );

       // E ora, la sacra danza dei bit :O
       BroadcastAddress.asUInt = IP.asUInt | ~SubnetMask.asUInt;

       // Output
       printf("\nIndirizzo di broadcast: %­hhu.%­hhu.%­hhu.%­hhu\n",        BroadcastAddress.bytes.b1,
                                                                       BroadcastAddress.bytes.b2,
                                                                       BroadcastAddress.bytes.b3,
                                                                       BroadcastAddress.bytes.b4
                                                               );

       return 0;
}
 
Top
th1sk
CAT_IMG Posted on 30/12/2012, 20:55     +1   -1




Grazie :asd:
 
Top
1 replies since 30/12/2012, 16:39   202 views
  Share