Question: C++ Programming Here is a bit of program. Currently working on the server portion. It's obviously not done but I need help. #include #include #include

C++ Programming

Here is a bit of program. Currently working on the server portion. It's obviously not done but I need help.

#include #include #include

using namespace std;

const char FILENAME[] = "data.bin"; const char IPADDR[] = "127.0.0.1"; const int PORT = 50000; const int QUERY = 1; const int UPDATE = 2;

void cleanup(SOCKET socket);

int main() { // Add your code here for the server WSADATA wsaData; SOCKET listening; SOCKET acceptSocket; SOCKADDR_IN serverAddr;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { cerr << "ERROR: Problem with WSAStartup "; return 1; }

void openInputFile(ifstream & dataFile, const char FILENAME[]) { dataFile.open(FILENAME); if (dataFile.fail()) { cerr << "ERROR: Data file cannot be opened "; exit(1); }

else { }

//create socket listening = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); //initialize server address with zero //initialize server address servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT);

if (listening == INVALID_SOCKET) { cerr << "ERROR: Socket cannot be created ";

WSACleanup(); return 1; }

if (bind(listening, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) { cerr << "ERROR: Cannot bind to port ";

cleanup(listenSocket); return 1; }

cout << " Waiting for connections... ";

acceptSocket = accept(listen, NULL, NULL);

// For this program, the listen socket is no longer needed so it will be closed closesocket(listening);

cout << "Connected... ";

}

return 0;

}

void cleanup(SOCKET socket) { }

Steps of update process:

  1. When client program is executed, it checks itself to see what version it is. This information is usually recorded in some form of a data file that is used by the client.
  2. Client contacts an update server to check for updates. The address and port number for the server are programmed into the client software or recorded in a data file. The user does not normally have to enter this information.
  3. Server responds back with the version number of the most current copy of the software.
  4. Client compares the version number received from server with it's own version number.
  5. If the version numbers match, then the client software is up-to-date and no other communication is needed with the server. The client program can then proceed operating, doing whatever task(s) it was designed to do.
  6. If the version numbers do NOT match, then the client requests the updated copy from the server.
  7. 6A. The server sends the file to the client, several bytes at a time.
  8. 6B. The client receives the bytes and stores them in into a new file. Small numbers of bytes could be stored temporarily in memory, while large numbers of bytes need to be written to a temporary file.
  9. 6C. Once all bytes have been received, the client deletes the old file and replaces it with the new one. It is important not to delete the old file until all of the bytes have been received. In case there is a problem with the connection, you don't want to leave the client in a non-working state.

Assignment

One program will be the update server and the other will be the update client. Here is how the two programs should interact with each other:

Server

1. the server starts up and reads the version number in the data.bin file and stores it in memory.

2. The server binds to a port and awaits connections.

Client

1. The client starts up and it will first read the version number found within it's own data.bin file.

2. The client will contact the server.

Server

1. The server accepts the incoming connection.

2. Waits for data to be sent.

Client

1. Send to the server, the integer 1. the integer 1 is code that represents a request for the current version number.

2. Wait for a reply from the server.

Server

1. Receive the request from the client.

2. See what the client is requesting. If the integer 1 was received, then the request is for the version number.

3. Send the version number back to the client

4. Close the current connection and wait for a new connection from a new client.

Client

1. Receive the version number from server.

2. Compare received version number to the client's version number.

3. If the version numbers match, then the program proceeds with it's normal operation (see below)

  • If the version numbers don't match:
    • Contact the server (again)

Server

1. Server accepts incoming connection

2. Waits for data to be sent.

Client

1. Send to the server, the integer 2. the integer 2 is code that represents a request for the updated file.

Server

1. Receive the request.

2. See what the client is requesting. If the integer 2 was received, then the request is for the updated file.

3. Open data.bin file

4. Read bytes from the file

5. Send bytes to client

6. Close the current connection and wait for a new connection from a new client

Client

1. Receive the bytes from the server.

2. Open the data.bin file for writing (this action should delete the existing file and then open a new one, ready to write data into).

3. Write the bytes received into the data.bin file.

4. Close the file

5. Proceed with normal operation

once the server has handled a request (either for the version number or for the updated file), it closes the connection and goes back to waiting for a new one.

the server will also display results to the screen to show what's happening. The client program also displays status messages to let the user know what's going on.

Client Normal Operation

This client program simpy adds two numbers together and prints the sum to the screen. The numbers that should be added, are stored in the data.bin file. The data.bin contains three numbers in this order: version, number1, number2. No spaces, line breaks, or any other seperator between the three integers .

1. Cache Requirement----The server needs to cache the version number of the data file. The server should only read the version number from the data file once and store it in a variable in memory. The server should NOT read the version number from the data file everytime a request is made for the version number.

2. Server "Hot Swap" Requirement---- The server does not need to be shut down and restarted. After every 5 requests handled by the server, it should re-read the version number from the data file. By doing this, the data file can be swapped out between client requests. The number "5" should probably be higher in a production server, to benefit more from the cache feature, but we will use 5 for testing purposes.

3. Separate Requests---- If client's data file needs to be updated, the client is required to make TWO INDEPENDENT REQUESTS to the server.The first request is for the version number, and the second request is for the data file. The second request should send the contents of the entire data file including the version number again. Don't assume the client will use the previously sent version number ,it's possible that it could have changed.

here is a little demonstration of the update server and the update client.

This is the update server after being executed in the command prompt:

Update server Current data file version: v1 Running on port number 50000

Waiting for connections... Connection received Request for current version number: v1 Connection closed Total requests handled : 1

This is the update client after being executed in the command prompt:

Checking for updates... No updates found

Sum Calculator Version 1

The sum of 3 and 5 is 8

Can the update server program be written like this? This is a sample chat server program that serves as a template for the update server program. The update server is supposed to setup a network connection and handle communication in the same way as the chat server provided below. This is not the full program but just a little snippet.

#include #include #include using namespace std;

#pragma comment(lib, "Ws2_32.lib")

const int STRLEN = 256;

// Closes the socket and performs the WSACleanup void cleanup(SOCKET socket);

int main() { WSADATA wsaData; SOCKET listenSocket; SOCKET acceptSocket; SOCKADDR_IN serverAddr; int port; char sendMessage[STRLEN]; char recvMessage[STRLEN]; bool done = false;

cout << "Enter port number to use: "; cin >> port; cin.ignore();

// Loads Windows DLL (Winsock version 2.2) used in network programming if ( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR ) { cerr << "ERROR: Problem with WSAStartup "; return 1; }

// Create a new socket to listen for client connections listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if ( listenSocket == INVALID_SOCKET ) { cerr << "ERROR: Cannot create socket "; WSACleanup(); return 1; }

// Setup a SOCKADDR_IN structure which will be used to hold address // and port information. Notice that the port must be converted // from host byte order to network byte order. serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons( port ); inet_pton(AF_INET, "127.0.0.1", &serverAddr.sin_addr);

//serverAddr.sin_addr.s_addr = inet_addr( "127.0.0.1" ); <== deprecated

// Attempt to bind to the port. if ( bind ( listenSocket, (SOCKADDR*) &serverAddr, sizeof(serverAddr) ) == SOCKET_ERROR ) { cerr << "ERROR: Cannot bind to port "; cleanup( listenSocket ); return 1; }

// Start listening for incoming connections if ( listen ( listenSocket, 1 ) == SOCKET_ERROR ) { cerr << "ERROR: Problem with listening on socket "; cleanup( listenSocket ); return 1; }

cout << " Waiting for connections... "; // Accept incoming connection. Program pauses here until // a connection arrives. acceptSocket = accept( listenSocket, NULL, NULL ); // For this program, the listen socket is no longer needed so it will be closed closesocket(listenSocket);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!