Prerequisite: Socket Programming in Java
Networking just doesn’t conclude with a one-way communication between the client and server. For example consider a time telling server which listens to request of the clients and respond with the current time to the client. Real-time applications usually follow a request-response model for communication.
The client usually sends the request object to the server which after processing the request, sends the response back to the client. In simple terms, the client requests for a particular resource available on the server and server responds it resource if it can verify the request. For example, when enter is pressed after entering the desired url, a request is sent to corresponding server which then replies by sending the response in form of a webpage which the browsers are capable of displaying.
In this article, a simple calculator application is implemented wherein the client will send requests to server in form of simple arithmetic equations and server will respond back with the answer to the equation.
The steps involved on client side are as follows-
- Open the socket connection
- Communication: In the communication part, there is a slight change. The difference with the previous article lies in the usage of both the input and output streams to send equations and receive the results to and from the server respectively. DataInputStream and DataOutputStream are used instead of basic InputStream and OutputStream to make it machine independent. Following constructors are used –
- public DataInputStream(InputStream in)
Syntax: public DataInputStream(InputStream in) Parameters: in - The underlying InputStream. Creates a DataInputStream that uses the specified underlying InputStream.
- public DataOutputStream(InputStream in)
Syntax: public DataOutputStream(OutputStream out) Parameters: out - The underlying OutputStream. Creates a DataOutputStream that uses the specified underlying OutputStream.
After creating the input and output streams, we use the readUTF and writeUTF of the created streams methods to receive and send the message respectively.
- public final String readUTF()
Reads the string encoded using UTF8 encoding. Throws: IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs
- public final String writeUTF()
Writes the string encoded using UTF8 encoding. Throws: IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs
- public DataInputStream(InputStream in)
- Closing the connection.
Client Side Implementation
Enter the equation in the form: 'operand operator operand' 5 * 6 Answer=30 Enter the equation in the form: 'operand operator operand' 5 + 6 Answer=11 Enter the equation in the form: 'operand operator operand' 9 / 3 Answer=3
Steps involved on the server side are as follows-
- Establish a socket connection.
- Process the equations coming from client: In server side also we open both the inputStream and outputStream. After receiving the equation, we process it and returns the result back to client by writing on the outputStream of the socket.
- Close the connection.
Server Side Implementation
Equation received:-5 * 6 Sending the result... Equation received:-5 + 6 Sending the result... Equation received:-9 / 3 Sending the result...
Note: In order to test the above programs on the system, please make sure that you run the server program first and then the client one. Make sure you are in the client console and from there enter the equation in the format-“operand1 operator operand2” and press Enter. Answer to the requested equation will be shown in the client console only. Finally to terminate the communication, type “bye” (without quotes) and hit enter.
Simple Calculator using UDP in Java
This article is contributed by Rishabh Mahrsee. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.