Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a2;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class EchoClient {
- public static void main(String [] args) {
- // if(args.length < 2) {
- // throw new IllegalArgumentException("Argument not specified");
- // }
- System.out.println("Attempting to Initialize EchoClient...");
- String localhost = "127.0.0.1";//args[0];
- int port = 30000;
- try(Socket echoClient = new Socket(localhost, port)) {
- InputStream in = echoClient.getInputStream();
- OutputStream out = echoClient.getOutputStream();
- Scanner input = new Scanner(System.in);
- Boolean status = true;
- do {
- byte [] buffer = input.nextLine().getBytes();
- out.write(buffer);
- int numBytesReceived = 0;
- int bytesReceived;
- while(numBytesReceived < buffer.length) {
- if((bytesReceived = in.read(buffer, numBytesReceived, buffer.length - numBytesReceived)) == -1) {
- throw new SocketException("Socket Closed");
- }
- numBytesReceived += bytesReceived;
- }
- System.out.println("Echo from Server: " + new String(buffer));
- if(new String(buffer).equalsIgnoreCase(".")) {
- System.out.println("Exiting Client.");
- status = false;
- input.close();
- echoClient.close();
- }
- }while(status == true);
- input.close();
- }
- catch(Exception e) {
- System.out.println("Unable to Initialize.");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement