Chat over a TCP connection Assumption: the information required to create a TCP socket (IP address...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Chat over a TCP connection Assumption: the information required to create a TCP socket (IP address and port number of the TCP server) is available and entered by the user via the stdin (cin, getline, etc.) Steps for the client program: 1. Prompt the user for the server's IP address and port number (this step will be replaced by the information received on the UDP connection in phase 2) 2. Create a TCP socket 3. Connect to the server using the information provided in step 1 4. Prompt for a username 5. Call the command $register. The username must be automatically amended to the command before being sent. E.g. $register 6. Receive the server response on the socket. If SV_FULL, close the socket and exit program 7. If SV_SUCCESS, the client can execute commands and send chat messages. Steps for the Server program: 1. Create the TCP listening socket 2. Bind the socket 3. Set the server in listening mode 4. Create the file descriptor sets master set and ready set to multiplex the connected and connecting clients. 5. Call select and return after timeout 1 second (NB: if timeout is null, select will note return unless int rc = select(0, &readySet, NULL, NULL, &timeout) 6. Check if the listening socket is SET in the ready set using the macro FD_ISSET(). If so, accept the connection, add the client to the list of connected clients, and add the new socket to the master set. To keep track of the registered clients, you should store the clients' information (username and socket number) in a data structure. Possible solutions include: array, vector or list* of a struct {username, socket}, or a map 7. Run through the ready set and recv data from the socket. to check if the current socket is the listening socket before you call recv(). typedef struct fd_set { } u_int fd_count; SOCKET fd_array[FD_SETSIZE]; fd_count: The number of sockets in the set. fd_array: An array of sockets that are in the set. 8. Parse the received message, and perform the correspondent action(s): o Handle the $register command: check the chat capacity, send an SV_FULL message if full, or SV_SUCCESS otherwise. In case of SV_FULL, close the socket. Receive "register" command Is capacity full? No Save the username And acknowledge the registration completion Send (SV_SUCCESS) -Yes- Send (SV_FULL) Notify the user and close the connection o Handle a user request for the list of connected users ($getlist) Receive "get list" command Is list empty? No Send the list of connected and registered users as a comma separated string -Yes- Notify the user o Handle a user request for the activity log ($getlog) All commands and messages of all users should be saved on the server in a log file. Upon receiving a get log request, the server should send the file to the corresponding client. Server: 1. Open file on the server 2. Send length of the file first to tell the client how much data to receive. 3. Read line 4. Send line 5. Repeat 3 and 4 until end of file 6. Close file On the client side: 1. Receive length 2. Open a new file 3. Read from socket until all bytes are received 4. Write to file after every recv() call 5. Close file o Handle the user exit ($exit) Notes: Graceful disconnection is invoked by sending the $exit command, which should remove the user from the list, shutdown and close the socket. In case of ungraceful disconnection, e.g. The user closes the window without executing the exit command, the server should remove the user from the list and close the connection. All messages and commands received from the clients must be logged in a .txt file. The following function could be used to open a file locally: FILE *fopen (const char *filename, const char *mode) When initializing the server, open the file in write mode "w" to override it if already created. In the server run, the function should be called in append mode a" Error check must be implemented at every step in the programs to ensure that all possible errors are handled properly. Chat over a TCP connection Assumption: the information required to create a TCP socket (IP address and port number of the TCP server) is available and entered by the user via the stdin (cin, getline, etc.) Steps for the client program: 1. Prompt the user for the server's IP address and port number (this step will be replaced by the information received on the UDP connection in phase 2) 2. Create a TCP socket 3. Connect to the server using the information provided in step 1 4. Prompt for a username 5. Call the command $register. The username must be automatically amended to the command before being sent. E.g. $register 6. Receive the server response on the socket. If SV_FULL, close the socket and exit program 7. If SV_SUCCESS, the client can execute commands and send chat messages. Steps for the Server program: 1. Create the TCP listening socket 2. Bind the socket 3. Set the server in listening mode 4. Create the file descriptor sets master set and ready set to multiplex the connected and connecting clients. 5. Call select and return after timeout 1 second (NB: if timeout is null, select will note return unless int rc = select(0, &readySet, NULL, NULL, &timeout) 6. Check if the listening socket is SET in the ready set using the macro FD_ISSET(). If so, accept the connection, add the client to the list of connected clients, and add the new socket to the master set. To keep track of the registered clients, you should store the clients' information (username and socket number) in a data structure. Possible solutions include: array, vector or list* of a struct {username, socket}, or a map 7. Run through the ready set and recv data from the socket. to check if the current socket is the listening socket before you call recv(). typedef struct fd_set { } u_int fd_count; SOCKET fd_array[FD_SETSIZE]; fd_count: The number of sockets in the set. fd_array: An array of sockets that are in the set. 8. Parse the received message, and perform the correspondent action(s): o Handle the $register command: check the chat capacity, send an SV_FULL message if full, or SV_SUCCESS otherwise. In case of SV_FULL, close the socket. Receive "register" command Is capacity full? No Save the username And acknowledge the registration completion Send (SV_SUCCESS) -Yes- Send (SV_FULL) Notify the user and close the connection o Handle a user request for the list of connected users ($getlist) Receive "get list" command Is list empty? No Send the list of connected and registered users as a comma separated string -Yes- Notify the user o Handle a user request for the activity log ($getlog) All commands and messages of all users should be saved on the server in a log file. Upon receiving a get log request, the server should send the file to the corresponding client. Server: 1. Open file on the server 2. Send length of the file first to tell the client how much data to receive. 3. Read line 4. Send line 5. Repeat 3 and 4 until end of file 6. Close file On the client side: 1. Receive length 2. Open a new file 3. Read from socket until all bytes are received 4. Write to file after every recv() call 5. Close file o Handle the user exit ($exit) Notes: Graceful disconnection is invoked by sending the $exit command, which should remove the user from the list, shutdown and close the socket. In case of ungraceful disconnection, e.g. The user closes the window without executing the exit command, the server should remove the user from the list and close the connection. All messages and commands received from the clients must be logged in a .txt file. The following function could be used to open a file locally: FILE *fopen (const char *filename, const char *mode) When initializing the server, open the file in write mode "w" to override it if already created. In the server run, the function should be called in append mode a" Error check must be implemented at every step in the programs to ensure that all possible errors are handled properly.
Expert Answer:
Related Book For
Computer Networking A Top-Down Approach
ISBN: 978-0136079675
5th edition
Authors: James F. Kurose, Keith W. Ross
Posted Date:
Students also viewed these computer network questions
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
You are the cashier employed at Tastee Limited. On November 30, 2020, you started with a float of $6,500 and at the end of the business day your cash in the cash till were as follows: Notes Coins...
-
Ethics has no place in business-Discuss this statement
-
In Figure a cord runs around two mass less, frictionless pulleys. A canister with mass m = 20 kg hangs from one pulley, and you exert a force F on the free end of the cord.(a) What must be the...
-
List various liquid fuels. State their merits over solid fuels.
-
Carter-Pierce Investments specializes in low-risk government bonds. Identify each of Carter-Pierces transactions as operating (O), investing (I), financing (F), non-cash investing and financing...
-
Question 2 ( 2 points ) Barnes Corp. issues $ 8 0 0 , 0 0 0 of 9 % , 2 0 - year bonds. The bonds are dated January 1 , 2 0 1 1 , and are due in 2 0 years on January 1 , 2 0 3 1 . Interest is paid...
-
Conceptual artists reacted to the expanding art market by: Flooding the market with bad art. Attempting to eliminate the art object. Emphasizing the material value of art. Imitating the mass?produced...
-
The rate constant k for a certain reaction is measured at two different temperatures: temperature 57.0 C 198.0 C E = Assuming the rate constant obeys the Arrhenius equation, calculate the activation...
-
1) For a nation other than the U.S., identify the government entity that is the counterpart of the FAA Aircraft Registry. Civil aircraft registered in the U.S. are identified by the letter N,...
-
A block of mass 54.1 kg rests on a slope having an angle of elevation of 24.3. If pushing downhill on the block with a force just exceeding 162 N and parallel to the slope is sufficient to cause the...
-
Solow model in continuous time. Consider the Solow model in continuous time with pro- duction function y = f(k) satisfying the usual properties, constant savings rate s, depreciation rate 8,...
-
Propane (C3H8) at standard temperature and pressure (298K and 1 atm) is combined with air at 500K and 1 atm in stoichiometric proportions and burned in a control volume (open system). a. (10 points)...
-
Sorry, you lost. SPX won and implemented its proposed strategy. Now the recession of 2015 has knocked down U.S. stock prices by 20%. The value of the Madison portfolio, after paying benefits for...
-
Define deferred revenue. Why is it a liability?
-
In mobile IP, what effect will mobility have on end-to-end delays of data-grams between the source and destination?
-
What does it mean for a signed document to be verifiable and non-forgeable?
-
Define and contrast the following terms: subnet, prefix, and BG? route.
-
Macquarie Manufacturing Ltd prepared the following planned production data for the forthcoming year ending 30 June 2019. Required (a) Prepare a table showing the predetermined factory overhead rate...
-
Beautiful Bottles Pty Ltd, bottle manufacturer for the food industry, has just installed a job order costing system. The company uses machine hours to apply its overhead to work in process. On 1 May...
-
Green Consultants Pty Ltd specialise in consulting on landscape design. The company developed a predetermined charge-out rate based on hours for each of its consultants on 1 July 2019 to assign the...
Study smarter with the SolutionInn App