Repost can someone help me with this assignemnt I can write
Repost: can someone help me with this assignemnt? I can write a basic c program to do the following but I dont know how to implement to server and stuff. I need it soon. PLease HELP thanks
Directions: The following programming assignment should be completed in C or C++ using the standard socket libraries that come with your operating system. It is highly recommended that you complete this assignment in Unix/Linux as this is what will be utilized in the solutions (and is also what is used in the textbook). If you are using a Windows machine, then I highly recommend installing Cygwin, which is a utility that allows you to run Linux on top of Windows… not the fasting thing in the world, but it will get the job done.
1. Write a server application that represents an online banking utility. Let your server maintain an array of “bank accounts,” which will be objects of the following struct type:
struct bankaccount
{
int acctnum;
char password[8]; // max password length is 8 characters
double balance;
};
You should create a set of 3 or 4 test accounts with made-up values to populate this array.
Your application should use a server socket to listen in to incoming client connections. As soon as a client logs on, it should present the client with the following prompt:
Welcome to [Choose A Name] Bank:
Please enter an account number:
After an account number is received from the client through the socket connection, the server program should check if the corresponding bank account exists. If not, return an error message to the client and close the connection. If the account does exist, then present the client with the following prompt:
Please enter password:
The server program should then wait until the client responds with a password. Once this occurs, the server should validate the password. If the password is incorrect, return an error to the client and close the connection. If the password is correct, then present the client with the following prompt:
Please enter a transaction:
The server program should then expect a command of on the following forms:
Deposit 123.45 This deposits $123.45 into the selected account
Withdraw 98.76 This withdraws $98.76 from the account (if the amount is available)
Balance This displays the balance
In the case of a deposit, the server should increment the bank account’s balance field by the deposit amount and then return a message to the client stating the new balance. In the case of a withdrawal, assuming sufficient funds exist, the server should decrement the bank account’s balance field by the withdrawal amount and then return a message to the client stating the new balance. If sufficient funds do not exist to cover the withdrawal amount, then the server should return a message to the client indicating this. In the case of “Balance,” the server should simply respond to the client with the balance of the account. In all cases, the socket should be closed immediately after the transaction is complete.
Solution
/****************** SERVER CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include<math.h>
int main(int argc, char *argv[])
{
int welcomeSocket, newSocket1, newSocket2;
int n,option,password,deposite,withdraw;
char welcome[1024],buffer[255], buffer1[1024], buffer2[8];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
struct bankaccount
{ int accnum; char psswrd[8]; double balance;}
struct bankaccount account;
/*The user needs to pass in the port number on which the server will accept connections as an argument.*/
/* This code displays an error message if the user fails to do this. */
if (argc < 2) {
fprintf(stderr,\"ERROR, no port provided\ \");
exit(1);
}
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr(\"127.0.0.1\");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, \'\\0\', sizeof serverAddr.sin_zero);
/*---- Bind the address struct to the socket ----*/
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
/*---- Listen on the socket, with 5 max connection requests queued ----*/
if(listen(welcomeSocket,5)==0)
printf(\"Listening\ \");
else
printf(\"Error\ \");
/*---- Accept call creates a new socket for the incoming connection ----*/
addr_size = sizeof serverStorage;
newSocket1 = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
//newSocket2 = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*---- Send message to the socket of the incoming connection ----*/
strcpy(welcome,\"Welcome to HDFC bank \ Please enter an account number\");
send(newSocket1,welcome,53,0);
bzero(buffer1,20);
account.accnum = read(newSocket1,buffer1,20);
if (strlen(account[accnum].bank_name)<0)
error(\"no account exits\");
else
printf(\"Enter password: %s\ \",buffer2);
bzero(buffer2,8);
account.psswrd = read(password,buffer2,8);
if (!strcmp(password,account[count].psswrd))
error(\"password does not match\");
else
{strcpy(buffer,\"enter your option \ 1.Balance \ 2.cash_deposite \ 3.cash_withdraw\");
send(newSocket1,buffer,17,0);}
n=read(option,buffer,1);
switch(option)
{
case \'1\': send(newSocket1,account.balance,20,0);;
break;
case \'2\': n=read(deposite,buffer,15);
account.balance=account.balance+n;
strcpy(buffer,account.balance);
send(newSocket1,buffer,15,0);
break;
case \'3\': n=read(withdraw,buffer,15);
account.balance=account.balance-n;
strcpy(buffer,account.balance);
send(newSocket1,buffer,15,0);
break;
default : system(\"cls\");
break;
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
/****************** CLIENT CODE ****************/
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(){
int clientSocket, n, option;
char buffer[20],buffer1[8],buffer2[1],password;
struct sockaddr_in serverAddr;
socklen_t addr_size;
/*---- Create the socket. The three arguments are: ----*/
/* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
clientSocket = socket(PF_INET, SOCK_STREAM, 0);
/*---- Configure settings of the server address struct ----*/
/* Address family = Internet */
serverAddr.sin_family = AF_INET;
/* Set port number, using htons function to use proper byte order */
serverAddr.sin_port = htons(7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr(\"127.0.0.1\");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, \'\\0\', sizeof serverAddr.sin_zero);
/*---- Connect the socket to the server using the address struct ----*/
addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
/*---- Read the message from the server into the buffer ----*/
recv(clientSocket, buffer,20, 0);
bzero(buffer,20);
n = write(acnum,buffer,20);
recv(clientSocket, buffer1, 8, 0);
bzero(buffer1,8);
n = write(password,buffer1,8);
recv(clientSocket, buffer2, 1, 0);
bzero(buffer2,1);
n=write(option,buffer2,1);
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------



