Search for question
Question

Introduction This programming assignment is designed to introduce you to Network programming using the socket interface implementing a client-server application. Minimum knowledge in database is optional, such background may help

in operating on stock transactions including insert, update and delete. If no previous background in databases, plain text files can be used to save, search, retrieve, and update data. Network Sockets: Stock Trading System For this assignment, you will design and implement an online stock trading application using network sockets. You will write both the client and server portions of this application and the communication protocol between them. The client and server processes will communicate using TCP sockets and will implement the protocol discussed below. The Assignment You will write two programs, a server and a client. The server creates a socket in the Internet domain bound to port SERVER_PORT (a constant you should define in both programs, you may use last 4 digits of your UM-ID to get a unique port number). The server receives requests through this socket, acts on those requests, and returns the results to the requester. The client will also create a socket in the Internet domain, send requests to the SERVER_PORT of a computer specified on the command-line, and receive responses through this socket from a server. For this assignment, you just need to allow one active client connect to the server. Your client and server should operate as follows. Your server begins execution by opening a text file or a SqLite database file and that contains two tables(initially empty): One table that holds stock records and the other table holds user records including the user's balance in USD. A good introduction and sample code to work with SQLite in C++ could be found here: https://www.tutorialspoint.com/sqlite/sqlite c cpp.htm Each User record in the users table(File) should have the following fields: ID int NOT NULL AUTO_INCREMENT, email varchar(255) NOT NULL, ● ● first_name varchar(255), last_name varchar(255), user_name varchar(255) NOT NULL, password varchar(255), usd balance DOUBLE NOT NULL, 1 the ID field is the primary key. Your code should check if there are any users, if there is no users, you need to create a user record. The following code can be used to create the Users table: CREATE TABLE IF NOT EXISTS Users ( ID INTEGER PRIMARY KEY AUTOINCREMENT, first_name TEXT, last_name TEXT, user_name TEXT NOT NULL, password TEXT, usd_balance DOUBLE NOT NULL ); Each stock record in the Stocks table should have the following fields: ID int NOT NULL AUTO_INCREMENT, stock_symbol varchar(4) NOT NULL, stock_name varchar(20) NOT NULL, stock_balance DOUBLE, user_id varchar(255), The following code can be used to create the Stocks table: CREATE TABLE IF NOT EXISTS Stocks ID INTEGER PRIMARY KEY AUTOINCREMENT, stock_symbol VARCHAR(4) NOT NULL, stock_name VARCHAR(20) NOT NULL, stock_balance DOUBLE, user_id INTEGER, FOREIGN KEY (user_id) REFERENCES Users (ID) ); Only your server should create, read, write, update, or delete records into/from the text file or SQLite database. Once the server has started, it should check if there is at least one user, 2 if there are no users the server should create one user manually or using code with, say, $100 balance. Then the server should wait for the connection requests from a client. Your client operates by sending one of the following commands: BUY, SELL, BALANCE, LIST, SHUDOWN, QUIT commands to the server. You should create a client that is able to send any of the commands above and allows a user to specify which of the commands the client should send to the server. The details of the protocol depend on the command the client sends to the server. BUY Buy an amount of stocks. A client sends the ASCII string “BUY" followed by a space, followed by a stock_symbol, followed by a space, followed by a stock_amount, followed by a space, followed by the price per stock, followed by a User_ID, and followed by the newline character (i.e., '\n'). When the server receives a BUY command from a client, the server should handle the operation by calculating the stock price and deducting the stock price from the user's balance, a new record in the Stocks table is created or updated if one does exist. If the operation is successful return a message to the client with "200 OK", the new usd_balance and new stock_balance; otherwise, an appropriate message should be displayed, e.g.: Not enough balance, or userl doesn't exist, etc. A client-server interaction with the BUY command thus looks like: c: BUY MSFT 3.4 1.35 1 s: Received: BUY MSFT 3.4 1.35 1 c: 200 OK BOUGHT: New balance: 3.4 MSFT. USD balance $95.41 Where 3.4 is the amount of stocks to buy, $1.35 price per stock, 1 is the user id. SELL SELL an amount of stock. A client sends the ASCII string "SELL" followed by a space, followed by a stock_symbol, followed by a space, followed by stock price, followed by a space, followed by a stock_amount, followed by a space, followed by a User_ID, and followed by the newline character (i.e., '\n'). When the server receives a SELL command from a client, the server should handle the operation by calculating the stock price and deposit the stock price to the user balance. If the 3 operation is successful return a message to the client with "200 OK", the new usd_balance and new stock_balance. A client-server interaction with the SELL command thus looks like: c: SELL APPL 2 1.45 1 s: Received: SELL APPL 2 1.45 1 c: 200 OK SOLD: New balance: 1.4 APPL. USD $98.31 Where stock symbol is APPL, amount to be sold 2, price per stock $1.45, and 1 is the user id. LIST List all records in the Stocks table/file. A client-server interaction with the LIST command looks like: c: LIST s: Received: LIST c: 200 OK The list of records in the Stocks database for user 1: 1 MSFT 3.4 1 2 APPL 51 3 GME 200 1 BALANCE Display the USD balance for user 1 A client-server interaction with the BALANCE command looks like: c: BALANCE s: Received: BALANCE c: 200 OK Balance for user John Doe: $98.31 4 SHUTDOWN The SHUTDOWN command, which is sent from the client to the server, is a single line message that allows a user to shutdown the server. A user that wants to shutdown the server should send the ASCII string "SHUTDOWN" followed by the newline character (i.e., '\n'). Upon receiving the SHUTDOWN command, the server should return the string "200 OK" (terminated with a newline), close all open sockets and database connection, and then terminate. A client-server interaction with the SHUTDOWN command looks like: C: SHUTDOWN s: Received: SHUTDOWN c: 200 OK QUIT Only terminate the client. The client exits when it receives the confirmation message from the server. A client-server interaction with the QUIT command looks like: c: QUIT c: 200 OK Note, "400 invalid command" or "403 message format error" should be returned to the client if a server receives an invalid command or the command in the wrong format along with an appropriate message, e.g.: Not enough MSFT stock balance, not enough USD or user 3 doesn't exist, etc. Format 1. Programming Environment You can use C++ implement the assignments. External libraries for Sockets are not allowed. 2. Requirements The following items are required for full credit (100%): 5