Java client serv -
I basically have to program a Java client / server, where the client asks the server to add or multiply . The client sends an object to the server that contains the requested operation (additional or multiplication) and is a sequence of the server that is to work on the server (addition or multiplication). Numbers are sent from a user-written console.
The server receives the object, calculates and answers. This is the code I have so far. I do not know how to add and add objects, send it to a server, send the number, so that the server can take action on them.
package esercizio3; Import java.io * *; Import Java.net *; Public class client {public static zero principal (string [] args) throws an exception {socket communication = new socket ("localhost", 8888); BufferedReader Feedback = New BufferedReader (New InputStreamReader (communication.getInputStream ())); PrintWriter request = new PrintWriter (communication.getOutputStream ()); BufferedReader stdIn = new BufferedReader (New InputStreamReader (System.in)); Request.println (stdIn.readLine ()); String line = response. Readline (); Println (line); Response.close (); Request.close (); StdIn.close (); Communication.close (); }} Package esercizio3; Import java.io * *; Import Java.net *; Public class server {public static zero major (string [] args) exception {ServerSockets listener throws new server socket (8888); Socket communication = listener.accept (); BufferedReader Request = New BufferedReader (New InputStreamReader (communication.getInputStream ())); PrintWriter Feedback = New Print Vetter (communication.getOutputStream ()); String line; While ((line = request .readLine ()) = "ok" {a = request Response.println ("detto:" + line); } Request.close (); Response.close (); Communication.close (); Listener.close (); }}
customer package esercizio3; Import java.io * *; Import Java.net *; Public class client {public static zero principal (string [] args) throws an exception {socket communication = new socket ("localhost", 8888); BufferedReader Feedback = New BufferedReader (New InputStreamReader (communication.getInputStream ())); PrintWriter request = new PrintWriter (communication.getOutputStream ()); BufferedReader stdIn = new BufferedReader (New InputStreamReader (System.in)); String user input = stdIn.readLine (); If (userInput.equals ("Fine")) // All calculations are finished request.println ("Fine"); } And {stringbuffer sbUserInput = new stringbuffer (); While (! UserInput.equals ("SEND")) // Get the input from the user until the SEND command was received and create a string that you want to process in server sbUserInput.append (userInput) .append (" ); // Add user input which makes our server work easy. UserInput = stdIn.readLine (); } Request.println (sbUserInput.toString ()); // send it to server} string line = response. Readline (); Println (line); Response.close (); Request.close (); StdIn.close (); Communication.close (); }} Server package esercizio3; Import java.io * *; Import Java.net *; Public class server {public static zero major (string [] args) exception {ServerSockets listener throws new server socket (8888); Socket communication = listener.accept (); BufferedReader Request = New BufferedReader (New InputStreamReader (communication.getInputStream ())); PrintWriter Feedback = New Print Vetter (communication.getOutputStream ()); String line; While (line = request.readLine ()) = "ok") {// read each string separated by space in space and this is the number or + or * string [] lineContents = line.split (""); Int results = 0; (String Line Content: Line Content) // Check whether this + or * // take correct action and update "result"} // Respond to client response. Println ("Haiti:" + line); } Request.close (); Response.close (); Communication.close (); Listener.close (); }}
Comments
Post a Comment