public class EchoClient{
Socket sock;
public final int ECHO_PORT=7;
InputStream in;
OutputStream out;
String line;
public EchoClient(String host){
try{
sock=new
Socket(host,ECHO_PORT);
in=sock.getInputStream();
out=sock.getOutputStream();
}
catch(IOException ex){
System.out.println("Error while
connecting to host");
System.out.println("Maybe you
should try another host.");
System.exit(0);
}
line="";
}
public void send_get(){
int len=line.length();
try{
if(!(line.equals(""))){
for(int i=0;i<len;i++)
out.write((int)line.charAt(i));
out.write((int)'\n');
line="";
while((len=in.read())!=(char)'\n')
line+=String.valueOf((char)len);
System.out.print("Response
from server: ");
System.out.println(line);
}
}
catch(IOException e){e.printStackTrace();}
}
public void execute()throws IOException{
DataInputStream dain=new DataInputStream(System.in);
System.out.print("Enter first
word, exit to end >");
System.out.flush();
line=dain.readLine();
while(!(line.equals("exit"))){
send_get();
System.out.print("Enter next
word >");
System.out.flush();
line=dain.readLine();
}
in.close();
out.close();
sock.close();
}
public static void main(String args[]){
if(args.length!=1){
System.out.println("This program
takes the hostname");
System.out.println("as the
only argument.");
System.exit(0);}
String host=args[0];
EchoClient ec=new EchoClient(host);
try{
ec.execute();
}
catch(IOException e){e.printStackTrace();}
}
}