언어/c언어

네트워크에 쓸 코드

파아랑새 2016. 7. 29. 01:11

# include <stdio.h>

# include <stdlib.h>

# include <string.h>

# include <windows.h>


# define SIZE 30

# define TRUE 1

// ======================================================

typedef struct _IpAddress {

char ip_address[SIZE];

char subNetMask[SIZE];

char gateWay[SIZE];

char type[SIZE];

char NetWork[SIZE];

int computerCount;

char tempIp[SIZE];

}IpAddress, *pointerIpAddress;

// ======================================================


// function prototype ===================================

void error(char* errorMessage);

void initiallize(IpAddress** stu);

void subNetInput(IpAddress** stu);

void ipInput(IpAddress** stu);

void comCount(IpAddress** stu);

// ======================================================


// main function ========================================

int main (void) {

pointerIpAddress stu = NULL;

initiallize(&stu);

subNetInput(&stu);

ipInput(&stu);

comCount(&stu);

return 0;

} // end of main function


// [1] ==================================================

void error(char* errorMessage) {

fprintf(stdout, "%s\n", errorMessage);

} // end of error function


// [2] ==================================================

void initiallize(IpAddress** stu) {

(*stu) = (pointerIpAddress)malloc(sizeof(IpAddress));

if ( (*stu) == NULL) {

error("malloc error");

} else { //(*stu) != NULL

memset( (*stu), 0, sizeof(char)*SIZE);

}

} // end of initiallize function


// [3] ==================================================

void subNetInput(IpAddress** stu) {

fprintf(stdout, "%s \n"," ===============================================");

fprintf(stdout, "%s \n", "Type [A]=> 255.0.0.0");

fprintf(stdout, "%s \n", "Type [B]=> 255.255.0.0");

fprintf(stdout, "%s \n", "Type [C]=> 255.255.255.0");

fprintf(stdout, "%s ", "subNetMask : ");

while (TRUE) {

gets((** stu).subNetMask);

fflush(stdin);

fprintf(stdout, "%s \n"," ===============================================");

if (! strcmp((** stu).subNetMask, "255.0.0.0")) {

fprintf(stdout, "%s\n", "type [A]");

strcpy((**stu).subNetMask, "255.0.0.0");

strcpy((**stu).type, "A");

break;

}

else if (! strcmp((** stu).subNetMask, "255.255.0.0")) {

fprintf(stdout, "%s\n", "type [B]");

strcpy((**stu).subNetMask, "255.255.0.0");

strcpy((**stu).type, "B");

break;

}

else if (! strcmp((** stu).subNetMask, "255.255.255.0")){

fprintf(stdout, "%s\n", "type [C]");

strcpy((**stu).subNetMask, "255.255.255.0");

strcpy((**stu).type, "C");

break;

}

else {

fprintf(stdout, "%s \n", "miss input");

}

}

fprintf( stdout, "[subNetMask] => %s \n", (**stu).subNetMask );

fprintf( stdout, "[type] => %s \n", (**stu).type );

Sleep(3000);

system("cls");

} // end of subNetInput function


// [4] ==================================================

void ipInput(IpAddress** stu) {

fprintf( stdout, "%s \n","============================================= \n");

fprintf( stdout, "[1] [subNetMask] => %s \n", (**stu).subNetMask );

fprintf( stdout, "[2] [type] => %s \n", (**stu).type );

fprintf( stdout, "%s \n","============================================= \n");


fprintf( stdout, "%s", "NetWork input : ");

gets((** stu).NetWork);

} // end of ipInput function


// [5] ==================================================

void comCount(IpAddress** stu) {

int i; //

int count = 0;

int position = 0;

char* tempIp = NULL; 

fprintf(stdout, "%s", "computer count input : ");

scanf("%d", &((**stu).computerCount));

for ( i = 0 ; i < strlen((** stu).NetWork) ; i++ ) {

if ( (** stu).NetWork[i] == '.' )  {

++count;

if (count == 3) {

position = i;

break;

}

}

}

for (i = 0; i<= position; i++) {

(** stu).tempIp[i] = (** stu).NetWork[i];

}

for ( i = 0 ; i < (**stu).computerCount ; i++ ) {

if(i != 1) {

printf("%s", (** stu).tempIp);

printf("%d", i+1);

printf("\n");

}

} // end of comCount function