Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4.  
  5. public class Client {
  6. public static void main(String[] args) {
  7. Socket connection;
  8. Scanner myInput = new Scanner(System.in);
  9. Object obj;
  10. String theString;
  11. ObjectInputStream inputStream;
  12. Formatter output;
  13. int inputNo = 0;
  14.  
  15. try{
  16. connection = new Socket("localhost", 5888);
  17. inputStream = new ObjectInputStream(connection.getInputStream());
  18. output = new Formatter(connection.getOutputStream());
  19.  
  20. inputNo++;
  21.  
  22. System.out.println("Connected to server");
  23.  
  24. System.out.println("Enter 0 to end program");
  25.  
  26. while(true) {
  27. System.out.println("Enter a String: ");
  28. theString = myInput.nextLine();
  29.  
  30. if (theString.equals("0"))
  31. System.exit(0);
  32.  
  33. output.format("%s\n", theString);
  34. output.flush();
  35.  
  36. System.out.println("Input " + inputNo + " sent to server");
  37. inputNo++;
  38.  
  39. obj = inputStream.readObject();
  40. System.out.println("Object received from server");
  41.  
  42. if (obj instanceof MyString) {
  43. System.out.printf("OUTPUT: %nString: %s%nLength: %d%nReverse: %s%nSubstring: %s%n",
  44. ((MyString) obj).getTheString(), ((MyString) obj).getStringLength(),
  45. ((MyString) obj).reverseTheString(), ((MyString) obj).getSubString());
  46. }
  47. }
  48. }
  49. catch (ClassNotFoundException cnfe){
  50. cnfe.printStackTrace();
  51. }
  52. catch (IOException ioe){
  53. ioe.printStackTrace();
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement