Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Client {
- public static void main(String[] args) {
- Socket connection;
- Scanner myInput = new Scanner(System.in);
- Object obj;
- String theString;
- ObjectInputStream inputStream;
- Formatter output;
- int inputNo = 0;
- try{
- connection = new Socket("localhost", 5888);
- inputStream = new ObjectInputStream(connection.getInputStream());
- output = new Formatter(connection.getOutputStream());
- inputNo++;
- System.out.println("Connected to server");
- System.out.println("Enter 0 to end program");
- while(true) {
- System.out.println("Enter a String: ");
- theString = myInput.nextLine();
- if (theString.equals("0"))
- System.exit(0);
- output.format("%s\n", theString);
- output.flush();
- System.out.println("Input " + inputNo + " sent to server");
- inputNo++;
- obj = inputStream.readObject();
- System.out.println("Object received from server");
- if (obj instanceof MyString) {
- System.out.printf("OUTPUT: %nString: %s%nLength: %d%nReverse: %s%nSubstring: %s%n",
- ((MyString) obj).getTheString(), ((MyString) obj).getStringLength(),
- ((MyString) obj).reverseTheString(), ((MyString) obj).getSubString());
- }
- }
- }
- catch (ClassNotFoundException cnfe){
- cnfe.printStackTrace();
- }
- catch (IOException ioe){
- ioe.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement