C Project Code
C Project Code
/**
function : gotoxy
@param input: x and y coordinates
@param output: moves the cursor in specified position of console
*/
void gotoxy(int x,int y)
{
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
int main()
{
FILE *fp, *ft; /// file pointers
char another, choice;
another = 'y';
while(another == 'y') /// if user want to add another record
{
printf("\nEnter name: ");
scanf("%s",e.name);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter basic salary: ");
scanf("%f", &e.bs);
/*
File:- config.h
Desc:- contains all the variable declarations,functions declarations,constans definition...
*/
#include <stdio.h> //Provides the core input and output capabilities of the C language.
#include <conio.h> //for doing console input output
#include <ctype.h> //Contains functions used to classify characters by their types or to convert between
upper and lower case in a way that is independent of the used character set
#include <stdlib.h> //For performing a variety of operations, including conversion, pseudo-random
numbers, memory allocation, process control, environment, signalling, searching, and sorting.
#define _WIN32_WINNT 0x0500 //constant for console resizing (redifinition)
#include <windows.h> //defines a very large number of Windows specific functions that can be used in
C.
#include <string.h> //For manipulating several kinds of strings.
#include <time.h> //For converting between various time and date formats.
#include <winsock2.h> // contains functions for socket programmming
#include <process.h> //contains functions for threading process.
#define DEFAULT_PORT 5000
#define STRLEN 256
#define NUM_CLIENTS 2
#define ESC 27
#define F2 60
FILE *ip;
int computer_id=5;
int service_id=0;;
int log_stat=0;
bool is_member=0; // to see if the current customer is a member or not 1 means member, 2 means
admin, 0 means guest
char username[20];
char password[20];
int tot_cost=0;
char CyberName[20];
int fixed_rate;
int tot_service;
int cost_changed=0;
int check_flag = 0;
//Input and Output Handles
SOCKET Socket;
struct client
{
sockaddr_in c_address;
int c_addr_len;
SOCKET c_socket;
bool connected;
bool turn;
};
struct client c[NUM_CLIENTS]; //struct client c[n]; if multiple clients
struct service {
char name[15];
int rate;
}items[10];
struct ip_info
{
char ip_address[50];
int port_no;
int client_no;
};
struct ip_info ip1;
//Global variables
int i,j,k;
//Utility functions
void gotoxy(int x,int y); //jump to coordinates
void delay(unsigned int mseconds); //create a delay
void console_resize(int console_id);
void reset_console();
void login_screen();
void admin_page();
void show_menu();
void service_menu();
void settings_menu();
void order(int ord);
void chg_compID();
void chg_password();
void credits();
void log_out();
void help_window();
void console_settings();
void reset_variables();
/*
File:- function.h
Desc:- contains all the functions related to menu control,server and client interfacing
*/
#include "config.h"
#include "sock.h"
#include "interface.h"
for(i = 0 ; ; i++)
{
// if(log_stat==0) {break;}
//elapsed = (clock()-now)/CLOCKS_PER_SEC;
if(i%60==0)
{
min=i/60;
d=60*min;
if(min%60==0)
{
hrs=min/60;
f=60*hrs;
}
}
sec=i-d;
MIN=min-f;
if(hrs<10)
{
gotoxy(7,pos);printf("0%d",hrs);
}
else
{
gotoxy(7,pos);printf(":%d",hrs);
}
if(min<10)
{
gotoxy(9,pos);printf(":0%d",MIN);
}
else
{
gotoxy(9,pos);printf(":%2d",MIN);
}
if(sec<10)
{
gotoxy(12,pos);printf(":0%d",sec);
}
else
{
gotoxy(12,pos);printf(":%2d",sec);
}
if(MIN==0 && sec == 2) {
cost_changed = fixed_rate/4;
} else if(MIN ==15 && sec == 0) {
cost_changed = fixed_rate/4;
} else if(MIN == 30 && sec == 0) {
cost_changed = fixed_rate/2;
}
if (log_stat==2) {break;}
if(cost_changed != 0) {
tot_cost += cost_changed;
}
cost_changed = 0;
gotoxy(28,2);printf("Rs.%d",tot_cost);
delay(1000);
}
_endthread();
}
int get_code(void)
{
char key;
if((key=getch())==0)
return(getch());
else if(key=='\r')
return(key);
else if(key==ESC)
return(key);
else if(key==getch())
return (key);
else
return (0);
} //end of get_code()
bool confirm_member() {
char Message[STRLEN],repMessage[STRLEN];
createMessage(Message);
c_send_data(Message);
c_recv_data(repMessage);
if(repMessage[0] == '1') {
for(i=2;repMessage[i]!=':';i++) {
CyberName[i-2] = repMessage[i];
}
CyberName[i-1] = '/0';
i++; char x[4];
for(j=i;repMessage[j]!=':';j++) {
x[j-i] = repMessage[j];
}
x[j-i-1] = '/0';
fixed_rate = atoi(x);
return true;
} else { return false;}
// printf("\n%s",Message);getch();
//creating message to be sent
return Message;
}
int get_service_info() {
char Message[STRLEN],repMessage[STRLEN],item[15],rate[4];
log_stat = 3;
createMessage(Message);
c_send_data(Message);
int a = 0;
while(1) {
c_recv_data(repMessage);
for(i=0;repMessage[i]!=':';i++) {
item[i] = repMessage[i];
}
item[i]='\0';
i++;
strcpy(items[a].name,item);
for(j=i;repMessage[j]!=':';j++) {
rate[j-i] = repMessage[j];
}
rate[j] = '\0';
j++;
items[a].rate = atoi(rate);
void assign_ip_address(void)
FILE *assignIp;
assignIp=fopen("ip_add.dat","rb");
if(ip==NULL)
{
printf("\nCan not open file");
exit(1);
}
while(fread(&ip1,sizeof(ip1),1,assignIp)==1)
{
strcpy(ipaddress,ip1.ip_address);
port_add=ip1.port_no;
computer_id = ip1.client_no;
}
fclose(assignIp);
}
interface.h-------file
GetConsoleWindowT GetConsoleWindow;
GetConsoleWindow = (GetConsoleWindowT)GetProcAddress(hK32Lib,TEXT("GetConsoleWindow"));
return GetConsoleWindow();
}
//Interface Layout
void window(void)
{
int i;
int down=18,left=51,right=131;
for(i=left;i<=81;i++)
{
gotoxy(i,down);printf("\xB2");
}
for(i=101;i<=right;i++)
{
gotoxy(i,down);printf("\xB2");
}
for(i=down;i<=left-3;i++)
{
gotoxy(left,i);printf("\xB2");
}
for(i=left;i<=right;i++)
{
gotoxy(i,left-3);printf("\xB2");
}
for(i=down;i<=left-3;i++)
{
gotoxy(right,i);printf("\xB2");
}
gotoxy(right-8,left-4);printf("C. YBER");
//gotoxy(left+2,left-4);printf("Back : ESC");
}
void login_screen() {
int mid=85,down=18;
console_resize(3);
system("cls");
window();
gotoxy(mid,down);printf("Log In Screen\n");
gotoxy(mid,down+2);printf("C.yber Client\n");
gotoxy(mid,down+4);printf("Cyber XYZ\n");
gotoxy(mid-5,down+7);printf("1. Sign in as Member\n");
gotoxy(mid-5,down+10);printf("2. Sign in as Guest\n");
}
switch (sel) {
case '1':
while(1) {
system("cls");
window();
gotoxy(mid,down); printf("Member Sign In");
gotoxy(mid,down+2);printf("Username::\t");gets(username);
gotoxy(mid,down+4);printf("Password::\t");get_password(password);
//action to check username and password
is_member = 1;
if(confirm_member()) {
break;
} else {
MessageBox(0,"Invalid Username Or Password:: Please Try Again!","Error",0);
strcpy(username,"");
strcpy(password,"");
log_stat = 0;
}
}
break;
case '2':
char Message[STRLEN], repMessage[STRLEN];
is_member = 0;
createMessage(Message);
c_send_data(Message);
c_recv_data(repMessage);
for(i=2;repMessage[i]!=':';i++) {
CyberName[i-2] = repMessage[i];
}
CyberName[i-1] = '/0';
i++; char x[4];
for(j=i;repMessage[j]!=':';j++) {
x[j-i] = repMessage[j];
}
x[j-i-1] = '/0';
fixed_rate = atoi(x);
break;
}
tot_service = get_service_info();
log_stat = 1;
//continue to tray
}
void reset_console() {
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
HWND hWnd = GetConsoleWindowNT();
COORD NewSBSize;
int Status;
/* Resetting the console size and buffer */
NewSBSize.X = 13;
NewSBSize.Y = 2;
Status = MoveWindow(hWnd,0,0,50,50,TRUE);
if (Status == 0)
{
printf("Failed:: reset console size");
}
if (console_id == 1) {
NewSBSize.X = 35;
NewSBSize.Y = 7;
T = max_resY-150;
B = 150;
L = max_resX-300;
R = 300;
} else
if (console_id == 2) {
NewSBSize.X = 35;
NewSBSize.Y = 45;
T = max_resY-600;
B = 600;
L = max_resX-300;
R = 300;
} else if (console_id == 3) {
NewSBSize.X = max_resX/8;
NewSBSize.Y = max_resY/12;
T = 0;
L = 0;
R = max_resX;
B = max_resY;
}
Status = MoveWindow(hWnd,L,T,R,B,TRUE);
if (Status == 0)
{
printf("Failed:: resize console size");
}
//printf("%d,%d,%d,%d--%d,%d",L,T,R,B,NewSBSize.X,NewSBSize.Y); getch();
}
void show_menu() {
flag_menu=0;
console_resize(2);
system("cls");
gotoxy(10,4); printf("%s Menu",CyberName);
gotoxy(10,8);printf("1. Order Service");
gotoxy(10,10);printf("2. Credits");
gotoxy(10,12);printf("3. Log Out");
gotoxy(0,2);printf("Timer:\t\t Price: ");
//gotoxy(5,18);printf("Press Esc to go back to default console");
gotoxy(0,16); printf("Select an Item::");
gotoxy(0,20);printf("ESC -> Back\tT -> Tray Console\nH -> Help");
}
void service_menu() {
flag_menu=1;
system("cls");
printf("C.yber-Client");
gotoxy(10,4); printf("%s Service Menu",CyberName);
gotoxy(5,8);printf("Item Name");
gotoxy(20,8);printf("Rate(Rs.)");
void settings_menu() {
flag_menu=2;
char buffer[STRLEN],a[3],b[2];
intToString(computer_id,a);
intToString(is_member,b);
strcat(buffer,a);strcat(buffer,":");
strcat(buffer,"0");strcat(buffer,":"); //service_id
strcat(buffer,"3");strcat(buffer,":"); //log_stat
strcat(buffer,b);strcat(buffer,":");
c_send_data(buffer);
system("cls");
printf("C.yber-Client");
gotoxy(10,4); printf("Cyber XYZ Settings");
gotoxy(10,8);printf("1. Change Computer ID");
gotoxy(10,10);printf("2. Change Password");
gotoxy(10,12);printf("3. Change Server IP");
gotoxy(0,2);printf("Timer:\t\t Price: ");
//gotoxy(5,18);printf("Press Esc to go back to default console");
gotoxy(0,16); printf("Select an Item::");
gotoxy(0,20);printf("T -> Tray Console\tESC -> Back\nH -> Help");
//accepting service informations and printing the menu
}
void credits() {
flag_menu=3;
system("cls");
printf("C.yber-Client");
gotoxy(10,4); printf("CODDED BY");
gotoxy(8,5);printf("-----------------");
gotoxy(10,6);printf("<BASE 2 />");
gotoxy(6,8);printf("1. Aayush Shrestha");
gotoxy(6,9);printf("2. Ashok Basnet");
gotoxy(6,10);printf("3. Bibek Subedi");
gotoxy(6,11);printf("4. Dinesh Subedi");
gotoxy(2,12);printf("066 BCT , IOE pulchowk campus");
gotoxy(2, 14);printf("No copyright (C) protected: ");
gotoxy(2,16);printf("We love open source");
gotoxy(0,2);printf("Timer:\t\t Price: ");
gotoxy(0,20);printf("T -> Tray Console\tESC -> Back\nH -> Help");
to_tray = 0;
}
void log_out() {
console_resize(1);
log_stat = 2; // 2 means logging out status
service_id = tot_cost;
char Message[STRLEN];
createMessage(Message);
c_send_data(Message);
system("cls");
printf("Logging out");
for(i=0;i<=5;i++) {
delay(300);
printf(".");
}
reset_variables();
}
void help_window() {
flag_menu=9;
system("cls");
gotoxy(2,4);printf("HELP File");
gotoxy(1,5);printf("-------------------------");
gotoxy(2,7);printf("F2 -> To maximize the size of");
gotoxy(2,8);printf(" Console");
gotoxy(2,9);printf("T -> To minimize the size of ");
gotoxy(2,10);printf(" Console");
gotoxy(2,11);printf("H -> For Help");
gotoxy(2,12);printf("Esc -> For back");
gotoxy(0,2);printf("Timer:\t\t Price: ");
gotoxy(0,20);printf("T -> Tray Console\tESC -> Back\nH -> Help");
}
void console_settings() {
EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MINIMIZE , MF_BYCOMMAND);
RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_SIZE , MF_BYPOSITION);
RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MOVE , MF_BYPOSITION);
RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MAXIMIZE , MF_GRAYED);
DrawMenuBar(GetConsoleWindow());
}
Sock.h -------file
/*
File:- sock.h
Desc:- contains the socket related functions
*/
void connect_server(){
sockaddr_in cl_address;
fflush(stdin);
//char ipaddress[50];
int port;
//char ipaddress[50] = "127.0.0.1";
//printf("\nEnter ipAddress of server:- ");
//gets(ipaddress);
// printf("Enter port number:- ");
// scanf("%d",&port);
fflush(stdin);
port = port_add;
cl_address.sin_family=AF_INET;
cl_address.sin_addr.s_addr=inet_addr(ipaddress);
cl_address.sin_port=htons(port);
if(connect(Socket,(sockaddr *) &cl_address,sizeof(cl_address))==SOCKET_ERROR){
printf("\n\aError connecting to server\a\n");
system("pause");
WSACleanup();
//exit(1);
client();
}
else
printf("\nConnected to server\n");
//c_send_data("4:2:0:4:");
//system("pause");
}
void start_socket()
{
WSADATA wsadata; //initiate the use of ws2_32.dll
if(WSAStartup(MAKEWORD(2,0),&wsadata)!=0) //MAKEWORD(2,0) makes request for version 2.0 of
winsock on system
{
printf("Socket Initialization: Error with WSAStartup \n required version snot availabel \n");
system("pause");
WSACleanup(); //release resources
exit(10);
}
//creation of socket
Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
/*
AF_INET- specify the IPv4 address family.
SOCK_STREAM - specify a stream socket.
IPPROTO_TCP - specify the TCP protocol .
*/
if (Socket==INVALID_SOCKET)
{
printf("Socket Initialization: Error creating socket\n");
system("pause");
WSACleanup();
exit(11);
}
else
printf("socket initialised");
}
main.c ---------file
#include "function.h"
int main(){
console_settings();
ip=fopen("ip_add.dat","rb");
if(ip==NULL){
ip=fopen("ip_add.dat","wb");
gotoxy(10,5);printf("Enter IP address : ");
gotoxy(10,7);printf("Enter port number: ");
gotoxy(10,9);printf("Enter client number: ");
gotoxy(29,5);fflush(stdin);gets(ip1.ip_address);
gotoxy(30,7);scanf("%d",&ip1.port_no);
gotoxy(32,9);scanf("%d",&ip1.client_no);
fseek(ip,0,SEEK_END);
fwrite(&ip1,sizeof(ip1),1,ip);
}
fclose(ip);
assign_ip_address();
default:
break;
}
// yaha ko to_tray value service menu bata auchha
if(to_tray == 3) {
help_window();
} else
if(to_tray == 1) {
break;
} else
if(to_tray ==4) {
show_menu();
}
if (log_stat==2) { break;}
}
if(log_stat == 2) { goto start_log;}
if(to_tray==1) {goto start_menu;}
return 0;
}
Server Site===
/*
File:- admin_function.h
Desc:- contains all the functions related to administrator back side
*/
#include "service.h"
for(i=0;i<no_of_clients;i++){
if(c[i].connected){
flag_showLine = false;
gotoxy(60,coun+dis*2);
printf("%d",c[i].id);
for(j=0;j<c[i].r.num_request;j++){
flag_showLine = true;
gotoxy(75,coun+j*2);
printf("%s", getServiceName(c[i].r.service_id[j]));
gotoxy(93,coun+j*2);
printf("%d:%d",c[i].r.reqTime[j].hr,c[i].r.reqTime[j].min);
dis++;
}
if(flag_showLine){
gotoxy(50,coun+dis*2);
printf("-----------------------------------------------------------------------");
}
}
}
// }
}else{
gotoxy(60,30); printf("\a--No Clients Connected--\a");
}
}
void window(void)
{
int i;
for(i=40;i<=70;i++)
{
gotoxy(i,10);printf("\xB2");
}
for(i=90;i<=120;i++)
{
gotoxy(i,10);printf("\xB2");
}
for(i=10;i<=40;i++)
{
gotoxy(40,i);printf("\xB2");
}
for(i=40;i<=120;i++)
{
gotoxy(i,40);printf("\xB2");
}
for(i=10;i<=40;i++)
{
gotoxy(120,i);printf("\xB2");
}
gotoxy(112,39);printf("C. YBER");
gotoxy(42,39);printf("Back : ESC");
}
char* password()
{
char *Password,temp_passP[25],PasswordP[25];
int i=0;
while(1)
{
temp_passP[i]=getch();
if(temp_passP[i]==13){break;}
else if(temp_passP[i]==8)
{
if(i!=0) {
printf("\b \b");
i--;
}else {printf("\a");}
}
else
{
printf("*");
PasswordP[i] = temp_passP[i];
i++;
}
PasswordP[i]='\0';
}
Password=PasswordP;
return Password;
}
void member_settings_signup(void)
{
int leftpos=73;
char passMe[25];
system("cls");
window();
gotoxy(75,10);printf("ADD MEMBERS");
flag_menu=23;
char confirmMe[25];
char ch, temp_user[25];
int i=0,flag=0,j=1;
fflush(stdin);
// gotoxy(13,22);printf("Warning! Password Must Contain six(6) Alphanumeric Digits." );
gotoxy(leftpos,15);printf("Username:");
gotoxy(leftpos,18);printf("Password:");
gotoxy(leftpos,21);printf("Confirm password:");
gotoxy(leftpos+13,15);gets(temp_user);
if(check_username(temp_user)==0)
{
fseek(login,-sizeof(login1),SEEK_CUR);
fwrite(&login1,sizeof(login1),1,login);
fclose(login);
}
}
void member_settings_signin(void)
{
int leftpos=73;
system("cls");
char temp_username[25],pass[25];
int flag = 0;
int i=0;
gotoxy(leftpos,3);printf("Username:");
gotoxy(leftpos,5);printf("Password:");
gotoxy(leftpos+10,3); gets(temp_username);
gotoxy(leftpos+10,5);
strcpy(pass,password());
login=fopen("save\\logins.dat","rb");
while(fread(&login1,sizeof(login1),1,login)==1)
{
if(strcmp(login1.username,temp_username)==0 &&
strcmp(login1.password,pass)&&login1.flag_admin!=1)
{
flag = 1;
}
}
fclose(login);
gotoxy(25,7);
// system("cls");
printf("\n");
if(flag == 1){
printf("LOGIN Successful\n");
// MessageBox(0,"Login Successful","Alert message",0);
}
else{
void settings()
{
int leftpos=73;
system("cls");
window();
flag_menu=2;
gotoxy(76,10);printf("SETTINGS");
gotoxy(leftpos,15);printf("1. Services");
gotoxy(leftpos,18);printf("2. Cost");
gotoxy(leftpos,21);printf("3. Add Members");
gotoxy(leftpos,24);printf("4. Admin Settings");
gotoxy(leftpos,27);printf("Enter Your Choice: ");
}
void cost(void)
{
int leftpos=73;
system("cls");
window();
flag_menu=22;
gotoxy(74,10);printf("COST MANAGING");
gotoxy(leftpos,15);printf("1. Set Cost");
gotoxy(leftpos,18);printf("2. View Cost");
gotoxy(leftpos,21);printf("Enter Your Choice");
void viewcost(void)
{
system("cls");
int i;
window();
flag_menu=222;
gotoxy(76,10);printf("VIEW COST");
gotoxy(62,15);printf("Time ");gotoxy(82,15);printf("Cost");
fcost=fopen("save\\rate.dat","rb");
while(fread(&cost1,sizeof(cost1),1,fcost)==1)
{
gotoxy(62,14+i);printf("Per Hour : %d",cost1.rate);
//gotoxy(34,4+i);printf("Rs.%.2f",cost1.rate[i]);
}
fclose(fcost);
}
void admin_settings_signup()
{
FILE *SInfo;
int leftpos=73;
system("cls");
window();
int flag=0;
char passMe[25],confirmMe[25];
gotoxy(73,10);printf("C.yber Setup");
gotoxy(leftpos,15);printf("Username:");
gotoxy(leftpos,17);printf("Password:");
gotoxy(leftpos,19);printf("Confirm Password:");
gotoxy(leftpos+10,15);fflush(stdin);gets(login1.username);
gotoxy(leftpos+10,17);strcpy(passMe,password());
gotoxy(leftpos+18,19);strcpy(confirmMe,password());
if(strcmp(passMe,confirmMe)==0)
{
char sName[50],sAddress[50],sOwner[20];
strcpy(login1.password,passMe);
gotoxy(leftpos,22);printf("Congratulations !! Your account is created");
gotoxy(leftpos-10,25);printf("Cyber Name:- ");
gotoxy(leftpos-10,27);printf("Address:- ");
gotoxy(leftpos-10,29);printf("Owner:");
gotoxy(leftpos+5,25);fflush(stdin);gets(sName);
gotoxy(leftpos+5,27);fflush(stdin);gets(sAddress);
gotoxy(leftpos+5,29);fflush(stdin);gets(sOwner);
SInfo = fopen("save\\serverInfo.txt","w+");
fprintf(SInfo,"%s\n%s\n%s",sName,sAddress,sOwner);
fclose(SInfo);
gotoxy(leftpos+5,32); printf("Press any key to continue...");
flag=3;
}
else
{
gotoxy(leftpos,20);printf("Password Do not match");
gotoxy(leftpos,21);printf("Try again!!");
getch();
admin_settings_signup();
}
if(flag==3)
{
login=fopen("save\\logins.dat","wb+");
if(login==NULL)
{
printf("cannot open file");
exit(1);
}
login1.flag_admin=1;
fseek(login,-sizeof(login1),SEEK_CUR);
fwrite(&login1,sizeof(login1),1,login);
fclose(login);
getch();
}
}
void admin_settings_signin()
{
int leftpos=73;
system("cls");
window();
char temp_username[25],temp_password[25];
int flag=0;
gotoxy(74,10);printf("Login Process");
gotoxy(leftpos,13);printf("Username: ");
gotoxy(leftpos,15);printf("Password:");
gotoxy(leftpos+10,13);gets(temp_username);
gotoxy(leftpos+10,15);strcpy(temp_password,password());
login=fopen("save\\logins.dat","rb");
if(login==NULL)
{
printf("Unable to open file");
exit(1);
}
while(fread(&login1,sizeof(login1),1,login)==1)
{
if(strcmp(login1.username,temp_username)==0 && strcmp(login1.password,temp_password)==0
&& login1.flag_admin==1)
{
flag=4;
}
}
fclose(login);
if(flag==4)
{
gotoxy(leftpos,17);printf("Login Sucessful");
getch();
}
else
{
gotoxy(leftpos,17);printf("Invalid username or password");
getch();
admin_settings_signin();
}
}
void admin_settings()
{
int leftpos=73;
system("cls");
window();
flag_menu=24;
gotoxy(leftpos+1,10);printf("ADMIN SETTINGS");
gotoxy(leftpos,13);printf("1. Change Password");
gotoxy(leftpos,17);printf(" Enter Your Choice:- ");
}
void change_password()
{
int leftpose1=38;
system("cls");
char old_password[25],new_pass[25],confirm_pass[25];
int flag=0;
window();
flag_menu=24;
gotoxy(leftpose1+32,10);printf("CHANGING PASSWORD");
gotoxy(leftpose1+20,13);printf("Enter old password: ");
gotoxy(leftpose1+20,15);printf("Enter new password:");
gotoxy(leftpose1+20,17);printf("Re-enter new password");
gotoxy(leftpose1+40,13);strcpy(old_password,password());
gotoxy(leftpose1+41,15);strcpy(new_pass,password());
gotoxy(leftpose1+43,17);strcpy(confirm_pass,password());
if(strcmp(new_pass,confirm_pass)==0)
{
flag=5;
}
login=fopen("save\\logins.dat","rb+");
if(login==NULL)
{
printf("Unable to open file");
exit(1);
}
while(fread(&login1,sizeof(login1),1,login)==1)
{
if(strcmp(old_password,login1.password)==0&& login1.flag_admin==1)
{
if(flag==5)
{
strcpy(login1.password,new_pass);
fseek(login,-sizeof(login1),SEEK_CUR);
fwrite(&login1,sizeof(login1),1,login);
fclose(login);
flag=6;
}
}
}
if(flag==6)
{
gotoxy(leftpose1+25,19);printf("The password is successfully changed");
gotoxy(leftpose1+25,20);printf("Press any key to continue");
}
else
{
gotoxy(leftpose1+25,19);printf("Password changing process failed");
gotoxy(leftpose1+25,20);printf("Try again!!");
}
}
/*
File:- config.h
Desc:- contains all the variable declarations,functions declarations,constans definition...
*/
#include <stdio.h> //Provides the core input and output capabilities of the C language.
#include <conio.h> //for doing console input output
#include <ctype.h> //Contains functions used to classify characters by their types or to convert between
upper and lower case in a way that is independent of the used character set
#include <stdlib.h> //For performing a variety of operations, including conversion, pseudo-random
numbers, memory allocation, process control, environment, signalling, searching, and sorting.
#define _WIN32_WINNT 0x0500 //constant for console resizing (redifinition)
#include <windows.h> //defines a very large number of Windows specific functions that can be used in
C.
#include <string.h> //For manipulating several kinds of strings.
#include <time.h> //For converting between various time and date formats.
#include <winsock2.h> // contains functions for socket programmming
#include <process.h>
#define DEFAULT_PORT 5000
#define STRLEN 256
#define NUM_CLIENTS 10
#define MAX_SERVICES 10
#define NUM_MSG 4 //number of fields in protocol
//keys define
#define F1 59 //Client 1
#define F2 60 //Client 2
#define F3 61 //Client 3
#define ESC 37 //Escape key
#define ALT_A 30
int flag_menu=0;
int no_of_clients = 0;
int no_of_msgs = 0;
char recMessage[STRLEN];
char sendMessage[STRLEN];
struct cTime{
int hr,min;
}logIn,logOut,reqTime[MAX_SERVICES];
struct client
{
int id;
sockaddr_in c_address;
int c_addr_len;
SOCKET c_socket;
bool connected;
bool turn;
struct request r;
struct cTime logIn;
struct cTime logOut;
int cost;
bool active;
};
struct client c[NUM_CLIENTS]; //struct client c[n]; if multiple clients
int clients[NUM_CLIENTS];
COORD coord = {0, 0}; // sets coordinates to 0,0
bool SOCKET_START = false; //TRUE if socket is already initialised
bool login_server = false; // TRUE if server is alerady logged in
char *client_ip_address;
char *program_msg[STRLEN];
char cyberName[50];
//Global variables
//Function declarations
void main_menu(); //Main interface of the system
void gotoxy(int x,int y); //jump to coordinates
void delay(unsigned int mseconds); //create a delay
int get_code(void); //get current key code of enterted key
//Utility functions
void readMessage(int msgExtracted[NUM_MSG],char username[20],char password[20],char
msg[STRLEN]); //read message from client and extract
void clearArea(int left,int top,int right,int bottom); //Clears the certain area of screen with whitespaces
void message_display(char* msg[STRLEN],int disType);
int findStrArrLen(char* str[STRLEN]);
void intToString(int n,char str[]); //convert int string
void readTime(int timeExtracted[2],char timeString[10]);
void systemTime(char Ms[20]);
void getTime(int x[2]);
void getDate(char sDate[13]);
void newMessage(char msg[]); //Increments the messages
bool send_to_network(char *buffer,int client_id);
//Server client related functions
void start_socket();
void start_server();
//Threads
//void message_display(void *param,int disType);
void clients_table(void *param);
void thd_start_client_thread(void *param);
void thd_accept_connections(void *param);
void thd_receive_data(void *param);
void thd_program_messages(void *param);
struct cost_mem
{
//int count;
//float time[10];
int rate;
//float rate[10];
};
struct cost_mem cost1;
struct service_mem
{
int item_id;
char item_name[25];
int item_price;
int item_qty;
};
struct service_mem service1;
//decelaration of functions
char* password(void);
void member_settings_signup();
void member_settings_signin(void);
int check_username(char username[25]);
void main_menu(void);
void main_lomenu(void);
void settings(void);
void requests();
void cost(void);
void setcost(void);
void viewcost(void);
void admin_settings();
void admin_settings_signup();
void admin_settings_signin();
void change_password();
void services();
void add_services();
void edit_services();
void view_services();
void delete_services();
void search_services();
int check_item_id(int id);
void window();
char* getServiceName(int s_id);
int getRate();
void requests();
int checkUser(char username[20],char password[20]);
int getNumServices();
void decServiceQty(int ser_id);
//interface.h
void console_settings();
void console_resize(int new_x, int new_y);
void reset_console();
/*
File:- function.h
Desc:- contains all the functions related to menu control,server and client interfacing
*/
#include "config.h"
#include "sock.h"
#include "admin_function.h"
#include "interface.h"
#include "serverThreads.h"
/*
* C.yber Protocol
* Date: September 10,2010
* @author: <base-2> team
* Transfer Rule:- (Computer-id:service_id:status:isMember:username:password) sent by client as a single
string
*/
msgExt[j] = '\0';
if(k==4)
strcpy(username,msgExt);
else if(k==5){
strcpy(password,msgExt);
break;
}
else
msgExtracted[k] = atoi(msgExt);
j = 0; //reset
if(msg[i] != '\0'){
i++; k++;
}
}
else{
i++;
j++;
}
}while(msg[i] !='\0');
int get_code(void)
{
char key;
if((key=getch())==0)
return(getch());
else if(key=='\r')
return(key);
else if(key==ESC)
return(key);
else if(key==getch())
return (key);
else
return (0);
} //end of get_code()
void saveMessagesToFile(){
FILE *fSave;
int i;
char fileLocation[30] = "save\\logs\\";
char fileName[13];
getDate(fileName);
strcat(fileLocation,fileName);
strcat(fileLocation,".txt");
fSave = fopen(fileLocation,"a+");
for(i = 0; i<no_of_msgs ;i++){
fprintf(fSave,"%s\n",*(program_msg+i));
}
fclose(fSave);
}
}
int getRate(){
FILE *fCost;
fCost=fopen("save\\rate.dat","rb");
while(fread(&cost1,sizeof(cost1),1,fCost)==1)
{
return cost1.rate;
}
fclose(fCost);
}
int getNumServices(){
FILE *fService;
int counter = 0;
fService=fopen("save\\service.dat","rb");
while(fread(&service1,sizeof(service1),1,fService)==1)
{
counter++;
}
fclose(fService);
return counter;
}
//Find Size of Array of String
int findStrArrLen(char* str[STRLEN]){
int i;
for(i=0;*(str+i) != NULL;i++);
return i;
}
/*
* interface.h
* Server side Interface related functions and threads
* @author: <base-2>
* Date:- 9-11-2010
*/
void main_menu(){
FILE *sInfo;
sInfo = fopen("save\\serverInfo.txt","r");
flag_menu = 0;
char sDate[13];
int cn = 7,i,count = 9;
system("cls");
for(i=0;i<56;i++){
gotoPrint(90,i,"|");
}
//reset nu of requests
if(flag_reset==true){
for(i=0;i<no_of_clients;i++){
c[i].r.num_request = 0;
}
}
flag_reset = false;
/*if(flag_reset == true){
for(i=0;i<no_of_clients;i++){
if(c[i].connected == false)
no_of_clients--;
}
}
*/
//flag_reset = false;
getDate(sDate);
fscanf(sInfo,"%s",cyberName);
gotoPrint(35,1,cyberName);
gotoxy(60,1); printf("Today:- %s",sDate);
gotoPrint(23,3,"------------------------------------");
gotoPrint(25,4,"1.Requests 2. Settings 3. Exit ");
gotoPrint(23,5,"------------------------------------");
gotoPrint(5,cn,"Clients");
gotoPrint(20,cn,"Requests Pending");
gotoPrint(40,cn,"Login Time");
gotoPrint(55,cn,"Logout Time");
gotoPrint(75,cn,"Cost");
if(no_of_clients>0){
for(i=0;i<no_of_clients;i++){
gotoxy(7,count+i*2);
printf("%d",c[i].id);
gotoxy(30,count+i*2);
printf("%d",c[i].r.num_request);
gotoxy(45,count+i*2);
printf("%d:%d",c[i].logIn.hr,c[i].logIn.min);
if(c[i].active == false){
gotoxy(60,count+i*2);
printf("%d:%d",c[i].logOut.hr,c[i].logOut.min);
gotoxy(75,count+i*2);
printf("Rs. %d",c[i].cost);
}
//}
}
}
flag_newMsg = true;
//gotoPrint(10,10,"No Clients Connected");
}
void console_settings() {
//EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
// RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
//RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MINIMIZE , MF_BYCOMMAND);
//RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_SIZE , MF_BYPOSITION);
//RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MOVE , MF_BYPOSITION);
EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MAXIMIZE , MF_GRAYED);
RemoveMenu(GetSystemMenu(GetConsoleWindow(), FALSE), SC_MAXIMIZE , MF_GRAYED); //disab;e
DrawMenuBar(GetConsoleWindow());
}
GetConsoleWindowT GetConsoleWindow;
GetConsoleWindow = (GetConsoleWindowT)GetProcAddress(hK32Lib,TEXT("GetConsoleWindow"));
if ( GetConsoleWindow == NULL ) {
return NULL;
}
return GetConsoleWindow();
}
void reset_console() {
CONSOLE_SCREEN_BUFFER_INFO SBInfo;
HWND hWnd = GetConsoleWindowNT();
COORD NewSBSize;
int Status;
/* Resetting the console size and buffer */
NewSBSize.X = 13;
NewSBSize.Y = 2;
Status = MoveWindow(hWnd,0,0,50,50,TRUE);
if (Status == 0)
{
printf("Failed:: reset console size");
}
if (console_id == 1) {
NewSBSize.X = 35;
NewSBSize.Y = 7;
T = max_resY-150;
B = 150;
L = max_resX-300;
R = 300;
} else
if (console_id == 2) {
NewSBSize.X = 35;
NewSBSize.Y = 45;
T = max_resY-600;
B = 600;
L = max_resX-300;
R = 300;
} else if (console_id == 3) {
NewSBSize.X = max_resX/8;
NewSBSize.Y = max_resY/12;
T = 0;
L = 0;
R = max_resX;
B = max_resY;
}
Status = MoveWindow(hWnd,L,T,R,B,TRUE);
if (Status == 0)
{
printf("Failed:: resize console size");
}
//printf("%d,%d,%d,%d--%d,%d",L,T,R,B,NewSBSize.X,NewSBSize.Y); getch();
}
/*
* main.c
* Server program for managing clients requests, settings etc.
*/
#include "function.h"
int main(){
main_menu();
start_socket();
start_server();
case 27:
if(flag_menu==2 || flag_menu==1 )
main_menu();
else if(flag_menu==21)
settings();
else if( flag_menu==22)
settings();
else if(flag_menu==23)
settings();
else if(flag_menu==24)
settings();
else if(flag_menu==211)
services();
else if(flag_menu==212)
services();
else if(flag_menu==213)
services();
else if(flag_menu==214)
services();
else if(flag_menu==215)
services();
else if(flag_menu==221 || flag_menu==222)
cost();
break;
default:
break;
}
}
return 0;
}
/*
* serverThreads.h
* Contains all the server related threads
* @author: <base-2>
* Date:- 9-11-2010
*/
main_menu();
}else{
send_to_network("0",j); //no member
}
}
else if(login_status == 1){ //cyber running and client requests service
if(getNumServices() >= ser_id){ //if jpt service send
printf("\a");
char CMsgs[20] = "";
intToString(client_id,str);
strcpy(CMsgs,"Client no. ");
strcat(CMsgs,str);
strcat(CMsgs," requested for a service.");
num_req = c[j].r.num_request;
c[j].r.num_request++;
c[j].r.service_id[num_req] = ser_id;
gotoxy(30,count+j*2);
printf("%d",c[j].r.num_request);
decServiceQty(ser_id);
getTime(clTime);
c[j].r.reqTime[num_req].hr = clTime[0];
c[j].r.reqTime[num_req].min = clTime[1];
strcpy(sendData,"1:");
send_to_network(sendData,j);
newMessage(CMsgs);
}else{ //the item is not available
send_to_network("0",j);
}
}
else if(login_status == 2){ //log outs
c[j].logOut.hr = clTime[0];
c[j].logOut.min = clTime[1];
c[j].cost = cMsg[1];
gotoxy(60,count+j*2);
printf("%d:%d",c[j].logOut.hr,c[j].logOut.min);
gotoxy(75,count+j*2);
printf("Rs. %d",c[j].cost);
disconnect_client(j); //log outs
//newMessage(ClMsgs);
}
else if(login_status == 3){ //give all the service menu to clients
send_services_list(j);
}
}
}
}
}
_endthread();
}
}
_endthread();
}
//MessageBox(0,"Client Connected","Accepted",0);
}
}
_endthread();
}
/*
* service.h
* contains sever related functions setup
*/
void services()
{
int leftpos=73;
system("cls");
window();
flag_menu=21;
gotoxy(76,10);printf("SERVICES");
gotoxy(leftpos,15);printf("1. Add Services ");
gotoxy(leftpos,18);printf("2. View Services");
gotoxy(leftpos,21);printf("3. Edit Services");
gotoxy(leftpos,24);printf("4. Search Services");
gotoxy(leftpos,27);printf("5. Delete Services");
gotoxy(leftpos,30);printf("Enter Your Choice:");
}
void add_services()
{
int leftpos=73;
system("cls");
window();
flag_menu=211;
int temp_id,i=0;char another='y';
gotoxy(74,10);printf("ADD SERVICE");
fp=fopen("save\\service.dat","ab+");
gotoxy(leftpos,15);printf("Item ID: ");
gotoxy(leftpos,18);printf("Item Name: ");
gotoxy(leftpos,21);printf("Item Rate: ");
gotoxy(leftpos,24);printf("Quantity: ");
gotoxy(leftpos+9,15);scanf("%d",&temp_id);
if(check_item_id(temp_id)==0)
{
gotoxy(leftpos,30);printf("The Item ID already exists");
gotoxy(leftpos,31);printf("Try another one");
getch();
add_services();
}
service1.item_id=temp_id;fflush(stdin);
gotoxy(leftpos+13,18);gets(service1.item_name);
gotoxy(leftpos+13,21);scanf("%d",&service1.item_price);
gotoxy(leftpos+13,24);scanf("%d",&service1.item_qty);
fseek(fp,-sizeof(service1),SEEK_CUR);
fwrite(&service1,sizeof(service1),1,fp);
gotoxy(leftpos,31);printf("The data is successfully saved.");
fclose(fp);
}
void view_services()
{
int leftpos1=40;
system("cls");
window();
flag_menu=212;
int i=0;
gotoxy(74,10);printf("VIEW SERVICE");
fp=fopen("save\\service.dat","rb");
gotoxy(leftpos1+10,15);printf("Item ID");gotoxy(leftpos1+20,15);printf("Item
Name");gotoxy(leftpos1+40,15);printf("Price");gotoxy(leftpos1+60,15);printf("Quantity");
while(fread(&service1,sizeof(service1),1,fp)==1)
{
gotoxy(leftpos1+10,18+i);printf("%d",service1.item_id);
gotoxy(leftpos1+20,18+i);printf("%s",service1.item_name);
gotoxy(leftpos1+40,18+i);printf("%d",service1.item_price);
gotoxy(leftpos1+60,18+i);printf("%d",service1.item_qty);
i++;
}
fclose(fp);
}
void search_services()
{
int leftpos=73;
system("cls");
window();
flag_menu=214;
int temp_id ,flag=0;char temp_name[25];
fp=fopen("save\\service.dat","rb");
gotoxy(73,10);printf("SEARCH SERVICE");
gotoxy(leftpos,15);printf("1. Search by ID");
gotoxy(leftpos,18);printf("2. Search by Name");
gotoxy(leftpos,21);printf("Enter your choice: ");
switch(getch())
{
case '1':
{
system("cls");
window();
gotoxy(74,10);printf("SEARCH BY ID");
gotoxy(leftpos,15);printf("Enter the ID to search:");gotoxy(leftpos+25,15);scanf("%d",&temp_id);
while(fread(&service1,sizeof(service1),1,fp)==1)
{
if(temp_id==service1.item_id)
{
flag=9;
gotoxy(leftpos,16);printf("The record is found");
gotoxy(leftpos+2,19);printf("Item ID : %d",service1.item_id);
gotoxy(leftpos+2,22);printf("Item Name: %s",service1.item_name);
gotoxy(leftpos+2,25);printf("Item Price: Rs. %d",service1.item_price);
gotoxy(leftpos+2,28);printf("Quantity: %d",service1.item_qty);
}
}
}
if(flag==0)
{
gotoxy(leftpos,16);printf("Sorry!! No record found");
break;
case '2':
{
system("cls");
rewind(fp);
window();
gotoxy(73,10);printf("SEARCH BY NAME");
gotoxy(leftpos,15);printf("Enter the Name to search:");fflush(stdin);gets(temp_name);
while(fread(&service1,sizeof(service1),1,fp)==1)
{
if(strcmp(temp_name,service1.item_name)==0)
{
flag=10;
gotoxy(leftpos,16);printf("The record is found");
gotoxy(leftpos+2,19);printf("Item ID : %d",service1.item_id);
gotoxy(leftpos+2,22);printf("Item Name: %s",service1.item_name);
gotoxy(leftpos+2,25);printf("Item Price: Rs. %d",service1.item_price);
gotoxy(leftpos+2,28);printf("Quantity: %d",service1.item_qty);
}
}
if(flag==0)
{
gotoxy(leftpos,16);printf("Sorry!! No record found");
}
}
break;
default:
search_services();
break;
}
fclose(fp);
}
void delete_services()
{
int leftpos=73;
system("cls");
FILE *temp_file;
int temp_id,flag=0;
window();
flag_menu=215;
gotoxy(73,0);printf("REMOVE SERVICE");
gotoxy(leftpos,15);printf("Enter the ID to delete: ");gotoxy(leftpos+26,15);scanf("%d",&temp_id);
fp=fopen("save\\service.dat","rb+");
while(fread(&service1,sizeof(service1),1,fp)==1)
{
if(temp_id==service1.item_id)
{
flag=11;
gotoxy(leftpos,16);printf("The record is found");
gotoxy(leftpos+2,19);printf("Item ID : %d",service1.item_id);
gotoxy(leftpos+2,22);printf("Item Name: %s",service1.item_name);
gotoxy(leftpos+2,25);printf("Item Price= Rs. %d",service1.item_price);
gotoxy(leftpos+2,28);printf("Quantity: %d",service1.item_qty);
}
}
if(flag==0)
{
gotoxy(leftpos,16);printf("Sorry!! No record found");
}
if(flag==11)
{
gotoxy(leftpos,32);printf("Are You Sure to Delete?(y/n)");
if(getch()=='y')
{
temp_file=fopen("save\\TEMP.dat","wb");
rewind(fp);
while(fread(&service1,sizeof(service1),1,fp)==1)
{
if(temp_id!=service1.item_id)
{
fseek(fp,0,SEEK_END);
fwrite(&service1,sizeof(service1),1,temp_file);
}
fclose(temp_file);
fclose(fp);
remove("save\\service.dat");
rename("save\\TEMP.dat","save\\service.dat");
fp=fopen("save\\service.dat","rb+");
gotoxy(leftpos,34);printf("The Item is deleted");
}
}
}
void edit_services()
{
int leftpos=73;
system("cls");
int temp_id,flag=0,temp_id2;
fp=fopen("save\\service.dat","rb+");
window();
flag_menu=213;
gotoxy(74,10);printf("EDIT SERVICE");
gotoxy(leftpos,15);printf("Enter the ID to Modify:");fflush(stdin);scanf("%d",&temp_id);
while(fread(&service1,sizeof(service1),1,fp)==1)
{
if(temp_id==service1.item_id)
{
gotoxy(leftpos-1,16);printf("The record is found");
gotoxy(leftpos,18);printf("Enter new ID:");
gotoxy(leftpos,20);printf("Enter new Name:");
gotoxy(leftpos,22);printf("Enter new Price:");
gotoxy(leftpos,24);printf("Enter new quantity:");
gotoxy(leftpos+13,18);scanf("%d",&temp_id2);
if(check_item_id(temp_id2)==0 && temp_id2!=temp_id)
{
gotoxy(leftpos,36);printf("The Item ID already exists");
gotoxy(leftpos,38);printf("Try another one");
getch();
edit_services();
}
service1.item_id=temp_id2;
gotoxy(87,20);fflush(stdin);gets(service1.item_name);
gotoxy(90,22);scanf("%f",&service1.item_price);
fseek(fp,-sizeof(service1),SEEK_CUR);
fwrite(&service1,sizeof(service1),1,fp);
fclose(fp);
flag=12;
}
}
if(flag==12)
{
gotoxy(leftpos,35);printf("The Item is successfully modified");
}
if(flag==0)
{
gotoxy(leftpos,30);printf("Sorry !! No record found");
gotoxy(leftpos,31);printf("Press any key....");
}
}
/*
File:- sock.h
Desc:- contains the socket related functions
*/
return true;
void disconnect_client(int x)
{
if(c[x].connected==true)
{
// closesocket ( c[x].c_socket );
char ClMsgs[30],str[5];
newMessage("A client Logs out.");
/*intToString(client_id,str);
strcpy(ClMsgs,"Client no. ");
strcat(ClMsgs,str);
strcat(ClMsgs," log outs.");
newMessage(ClMsgs);
*/
c[x].active=false;
//gotoPrint(10,30,ClMsgs);
//newMessage(ClMsgs);
//c[x].c_addr_len=-1;
//no_of_clients--;
}
else
printf("\ncouldnt disconnect client:");
}
if(bind(Socket,(sockaddr *) &server_address,sizeof(server_address))==SOCKET_ERROR)
{
newMessage("Failed to bind the socket");
system("pause");
WSACleanup();
exit(11);
}
else{
*(program_msg+no_of_msgs++) = "Socket Binded";
flag_newMsg = true;
}