socket c语言程序 c/s tcp模式 (题目部分)

时间:2021-06-27 03:35:23

题目:

Programming Assignment 1: Distributed Banking System

Problem Description
A distributed banking system consists of a server and some Automated Teller Machines
(ATM). The server manages all users’ account information. A customer can invoke the
following operations at an ATM.
• void deposit(int acnt, int amt): this operation increases the balance of user account
acnt by amt, and returns nothing;
• void withdraw(int acnt, int amt): this operation decreases the balance of user
account acnt by amt, and returns nothing;
• int inquiry(int acnt): this operation returns the balance of user account acnt
For simplicity, in this assignment you do not need to consider the synchronization
problem. The assignment is constructed in two parts. In the first part, you implement your
APIs using a client/server paradigm in which the two processes communicate using BSD
sockets on Unix. The client is the process performing the above operations. The server
receives the operation requests and performs them on behalf of the client. The client and
server may (or may not) be running on separate machines. In the second part, you will use
RMI rather than sockets for communicating between the client and server.
Part I Socket Programming
Implement your banking system in C/C++ using a client/server paradigm in which the two
processes communicate using Unix sockets.
1. Design Requirement
You should write two programs, a server and a client; the two should be compiled
separately, so that they each have their own executables. The two processes will
communicate with each other in a connection-oriented manner using sockets. Your APIs
will now have client side and server side implementations with message passing via
sockets connecting the two. You must decide the request/response message formats as well
as whether to choose a connection-oriented or connection-less protocol for your APIs. You
may also need to create a shutdown routine to free resources. During initialization (either
an explicit call or in the first call to one of your API routines—an implicit initialization) or
for each call (depending on your protocol choice), the client-side API implementation
should connect() to the server process (after creating the socket, of course). The client API
will accept the input parameters, marshal them into a message, send the message to the
server, and await the reply. When the reply is received, it extracts the return parameters
from the message and returns them to the caller. The server process should accept() an
incoming connection request (after creating, binding, and listening on the socket), extract
the parameters from the message and make the requested call.
Additional Notes
1) Note that because of byte ordering conventions ("big-endian" versus "little-endian")
on different machines, your client and server may not correctly interpret the contents of
an exchanged message. If your client and server are running on different machines with
different byte ordering conventions (e.g., one is running on an Intel machine and one is
running on a SUN SPARC), they will not interpret the content of each others’ structures
correctly. You should use the htonl() and ntohl() functions to ensure that all machines
interpret the byte orderings in the same manner.
2) In writing your code, make sure to check for an error return from all system calls. If
there is an error, the system declared global variable, errno, will give you information
about the type of error that occurred:
#include <errno.h>
......
if ( (sockid = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating client socket, error%d/n",errno);
perror("meaning:"); exit(0);
}
See the man pages errno(2) and perror(2) for a description of the error codes and the use
of perror.
3) Make sure you close every socket (file descriptor) that you use in your program. If you
abort your program, the socket may still hang around and the next time you try and bind a
new socket to the port number you previously used (but never closed), you may get an
error. Also, please be aware that port numbers, when bound to sockets, are system-wide
values and thus other students may be using the port number you are trying to use. With
this in mind, I suggest you use the last four digits of your student ID and add it to 10,000
to create your port number. The reason for the addition is that port numbers below 5000
are reserved for system use, and those between 5,000 and 10,000 are used by lots of local
applications. If you follow this rule the likelihood of colliding with another program
using the same port at the same time is very low.
2. Demo
Initially, assume that the Bank server has the following data in its database.
Account balance
100 $1000
Start the banking server on port 15555:
%bankserver 15555
Test the ATM client:
$atmclient cse.unl.edu 15555 inquiry 100
The current balance of user 100 is $1000
$atmclient cse.unl.edu 15555 deposit 100 200
Successfully deposit $200 to account 100!
$atmclient cse.unl.edu 15555 withdraw 100 50
Successfully withdraw $50 from account 100!
$atmclient cse.unl.edu 15555 inquiry 100
The current balance of user 100 is $1150