Question
implement a client-server application using sockets. The assignment consists of two console applications: the client application, and the server application. You may use java or
implement a client-server application using sockets. The assignment consists of two console applications: the client application, and the server application. You may use java or any programming language of your choice for the implementation.
Overview The system consists of a client and a server to implement a Simple Coffee-Pot Control Protocol (hereby known as the SCPCP). It is inspired by the HTCPCP protocol and the save -418 movement1.
Background
The Hyper Text Coffee Pot Control Protocol (HTCPCP) is a facetious communication protocol for controlling, monitoring, and diagnosing coffee pots2. It is an extension to the standard HTTP protocol that supports additional verbs and status codes. A real implementation of the HTCPCP protocol is available at https://github.com/stephen/node-htcpcp, implemented by Stephen Wan under MIT license. You may install and run the above project node.js under ubuntu3. Simply open a terminal and run the following: sudo apt install npm npm install htcpcp node ~/node_modules/htcpcp/examples/server.js
While the server is running, you may test the server by opening a browser and going to the following address: http://localhost:8000/
You may alternatively issue the following command on a different terminal:
curl -X POST http://localhost:8000/ --header "Content-Type:application/coffee-pot-command"
1 See: https://save418.com/. 2 It is specified in RFC 2324, published on 1 April 1998 as an April Fools' Day RFC, as part of an April Fools prank. An extension, HTCPCP-TEA, was published as RFC 7168 on 1 April 2014 to support brewing teas, which is also an April Fools' Day RFC (see https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol). 3 Windows user may simply install ubuntu app from windows store. See: https://ubuntu.com/tutorials/install-ubuntu-on-wsl2-on-windows-11-with-gui-support#2-install-wsl
The above is an http request that may be simulated using the following echo command: echo -e "POST / Content-Type: application/coffee-pot-command Content-Length: 0 " | nc localhost 8000 The Content-Length header tells the server that there is no content. In case it is not specified, you will receive a bad request error. To stop the server, simply press Ctrl+C.
The Simple Coffee-Pot Control Protocol (SCPCP)
The client may connect to an available server on the network. The SCPCP server is listening on port 431. The SCPCP protocol is implemented using TCP. The following illustrates a sample "SCPCP session", started by HELLO, and closed with BYE.
SERVER: HELLO CCCS 431 SCPCP Server written by ... READY CLIENT: BREW SERVER: 300 What is the magic word? CLIENT: BREW PLEASE SERVER: 301 Sorry, better start a conversation with greeting. CLIENT: HELLO SERVER: HELO <>, How may I help you? CLIENT: BREW SERVER: 400 ERR Bad Request CLIENT: BREW COFEE SERVER: 200 OK Brewing CLIENT: BREW TEA SERVER: 401 ERR Already brewing CLIENT: STATUS SERVER: 300 OK Hang on (2 more secs) CLIENT: STATUS SERVER: 200 OK Coffee ready CLIENT: BREW TEA SERVER: 301 ERR Running out of tea, May I serve you coffee instead? CLIENT: BYE SERVER: BYE, nice serving you! (followed by closing the connection)
The client connects to the server on port 431 and waits to receive the HELLO message from the server. Every command is a single line command (terminated by a newline character). Every message is also a single line response (terminated with newline). The detailed explanations of the commands are given in the following:
The SCPCP Commands:
HELLO: required for starting the SCPCP session. Upon receiving, it sends a personalized welcome message to the client by indicating the client's IP address. Hint: Use getRemoteSocketAddress() to obtain the client's IP address. The command is only valid if the client is not already in an SCPCP session. If the client has already started a session (already sent a previous HELLO command), this should be considered as an error in protocol and the server may send the following error message to the client. 400 ERROR Already in session. BREW: possible parameters: COFEE or TEA. Only one at a time. STATUS: returns the brewing status. BYE: ends the session. Valid only if SCPCP is in session. The server sends the closing message and ends the remote TCP session.
STEP 1 - Basic Client / Server
In this step, you create a basic TCP listener that upon receiving an incoming connection, it sends the following message to the client following by a new line character:
HELLO CCCS 431 SCPCP Server written by ... READY
The server application continues listening on port 431 unless the user wishes to close the application. Create such a listener and test your program using telnet (or netcat). Create a TCP client application that connects to server (the server name or ip address to be entered by the user in the command line). The client application simply prints out what it received from the server. Upon connecting to the server send a "HELLO" command (followed by a newline character) to the server. At this stage, the server does not respond to the client, however the data is properly received. Close the connection on the client side and terminate the program normally. You may alternatively run the provided code sample and modify the run() method in the GreaterServerProtocol class in com.mcgill.cccs431.example package. You may similarly create a client project and use ClientProtocolFactory class.
Make sure you create two separate projects.
STEP 2 - The full SCPCP Server (The SCPCP Server Side Protocol Implementation)
In this step, you implement the full server protocol. The application server is a java console application that is listening on port 431 implementing the server side of the SCPCP protocol as specified in the previous section. It implements the TCP listener. You may start the server by running this application on the console. Upon running, it displays the following message (on the console), by which the user may decide to end the program by hitting the ENTER key.
CCCS 431 SCPCP Server written by ... running Hit enter to STOP.
Server Implementation Notes:
Note that every client command is given in a single line. Therefore, the server always reads a full line from the input stream until it reaches the end of file (EOF). Upon reading a line, it, then, processes the input command and continues to receive the next command until the client wishes to exit. During the communication if the protocol is not followed by the client (i.e. the client sends an invalid command text, etc.) the server responds with an error message (see the protocol specification in the above). In case the client has already closed a connection (which causes a communication error on the server side) the server immediately closes its connection and writes an error message on the screen. You may test the server implementation using netcat (or telnet client) by manually connecting to the server application. Multi-threading and Simultaneous Connections The server side may receive simultaneous connections. Each client is served by a different socket, running in a different thread. Use netcat (or telnet) to test your server application. Test it with two simultaneous client connections.
STEP 3 - The Client Application
The client application is a java console application that lets the user to do the following. Upon running, it displays a menu and receives user commands in a "command loop" until the user wishes to exit the program. A sample menu is given in the following: Show Help/About: (displays a friendly help / about information) Connect: connects to an existing SCPCP server (receives the IP address or the name of the server computer from the user, connects to the server, and initiates an SCPCP session by sending the HELLO command, displays the welcoming message form the server, and waits for more command from the client to send to the server) Brew: asks the user about the item and sends the brew request to the server. The client must have already been connected to a server. Otherwise, error. Status: Queries the status of the server. The client must have already been connected to a server. Otherwise, error. Exit: safely terminates the client application (sends a BYE command to the connected server and closes the active socket, if connected; see below) Notes: 1. The Exit command "properly" closes the current connection. Proper closure of the connection means sending the BYE command to the server (see the TCP protocol in the previous section). If the client is not connected, it simply exits the program. 2. The Connect command also "properly" closes the current connection (if applicable) and then proceeds with a new connection.
Client Implementation Notes:
The client side starts the communication by connecting to the server. The server must be running. In case of any network errors, the client simply reports the error and terminates the program. The client displays a menu to the user, and upon receiving the menu option, it creates the command, sends is to the server, and displays the result. In case of any errors in the protocol (by the server) the client simply closes the connection, displays the error message on the screen.
Useful links: Java Sockets: https://www.baeldung.com/a-guide-to-java-sockets See the attached socklib. Feel free to use it.
Step by Step Solution
3.41 Rating (151 Votes )
There are 3 Steps involved in it
Step: 1
code for this problem goes here My Server java import javaio import javanet public class MyServer pu...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started