Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help to finish the coding as below. package mutu.pos.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * */ public class

Please help to finish the coding as below.

package mutu.pos.client;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket;

/** * */ public class POSClient { /** * @param args the command line arguments */ public static void main(String[] args) { try{ /* Create a socket to connect to server */ Socket _skt = new Socket("127.0.0.1", 12345); /* Create Reader and Writer object to read & write */ BufferedReader _in = new BufferedReader(new InputStreamReader(_skt.getInputStream())); PrintWriter _out = new PrintWriter(_skt.getOutputStream(), true); /* Create Reader object to collect user input */ BufferedReader _usr = new BufferedReader(new InputStreamReader(System.in)); /* Create the ClientDisplayHandler and pass the reader object to it */ ClientDisplayHandler _d = new ClientDisplayHandler(_in); /* Start the ClientDisplayHandler as a thread */ new Thread(_d).start(); String _usrInput = ""; /* An endless loop to get user input continously */ while(true){ /* Get the user input from USER INPUT reader object */ _usrInput = _usr.readLine(); /* Use checkError() of Writer object to check whether connection * to server is closed. If close, break the loop */ if(!_out.checkError()){ /* If no error, send the user input to server via Writer * object. Beware, change the null to "" before sent */ _out.println((_usrInput == null) ? "" : _usrInput); } else { break; } } /* Close all Reader, Writer and Socket object*/ _out.close(); _in.close(); _skt.close(); _usr.close(); }catch(IOException ioe){ System.out.println(ioe.getMessage()); }//end of try-catch }//end of main() }

class ClientDisplayHandler implements Runnable { private final BufferedReader _in; public ClientDisplayHandler(BufferedReader in){ this._in = in; } @Override public void run(){ try{ String _msg = ""; /* An endless loop to get server message continously */ while((_msg = _in.readLine()) != null){ System.out.println(_msg); } _in.close(); }catch(IOException ioe){ System.out.println(ioe.getMessage()); }//end of try-catch } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions

Question

3. Is IBMs program really a mentoring program? Why or why not?

Answered: 1 week ago